| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2007 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG USB | 
| Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #include "sysdeps.h" | 
|  | 20 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <dirent.h> | 
|  | 22 | #include <errno.h> | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 23 | #include <linux/usb/ch9.h> | 
|  | 24 | #include <linux/usb/functionfs.h> | 
|  | 25 | #include <stdio.h> | 
|  | 26 | #include <stdlib.h> | 
|  | 27 | #include <string.h> | 
|  | 28 | #include <sys/ioctl.h> | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 29 | #include <sys/mman.h> | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 30 | #include <sys/types.h> | 
|  | 31 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 |  | 
| Josh Gao | ae72b5a | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 33 | #include <algorithm> | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 34 | #include <atomic> | 
| Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 35 | #include <chrono> | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 36 | #include <condition_variable> | 
|  | 37 | #include <mutex> | 
| Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 38 | #include <thread> | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 39 |  | 
|  | 40 | #include <android-base/logging.h> | 
| Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 41 | #include <android-base/properties.h> | 
| Josh Gao | ae72b5a | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 42 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | #include "adb.h" | 
| Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 44 | #include "adbd/usb.h" | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 45 | #include "transport.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 |  | 
| Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 47 | using namespace std::chrono_literals; | 
|  | 48 |  | 
| Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 49 | #define MAX_PACKET_SIZE_FS 64 | 
|  | 50 | #define MAX_PACKET_SIZE_HS 512 | 
|  | 51 | #define MAX_PACKET_SIZE_SS 1024 | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 52 |  | 
| Jerry Zhang | 55205a5 | 2017-01-04 12:34:38 -0800 | [diff] [blame] | 53 | #define USB_FFS_BULK_SIZE 16384 | 
| Josh Gao | ae72b5a | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 54 |  | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 55 | // Number of buffers needed to fit MAX_PAYLOAD, with an extra for ZLPs. | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 56 | #define USB_FFS_NUM_BUFS ((4 * MAX_PAYLOAD / USB_FFS_BULK_SIZE) + 1) | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 57 |  | 
| Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 58 | #define cpu_to_le16(x) htole16(x) | 
|  | 59 | #define cpu_to_le32(x) htole32(x) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 |  | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 61 | static unique_fd& dummy_fd = *new unique_fd(); | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 62 |  | 
| Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 63 | struct func_desc { | 
|  | 64 | struct usb_interface_descriptor intf; | 
|  | 65 | struct usb_endpoint_descriptor_no_audio source; | 
|  | 66 | struct usb_endpoint_descriptor_no_audio sink; | 
|  | 67 | } __attribute__((packed)); | 
|  | 68 |  | 
| Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 69 | struct ss_func_desc { | 
|  | 70 | struct usb_interface_descriptor intf; | 
|  | 71 | struct usb_endpoint_descriptor_no_audio source; | 
|  | 72 | struct usb_ss_ep_comp_descriptor source_comp; | 
|  | 73 | struct usb_endpoint_descriptor_no_audio sink; | 
|  | 74 | struct usb_ss_ep_comp_descriptor sink_comp; | 
|  | 75 | } __attribute__((packed)); | 
|  | 76 |  | 
| Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 77 | struct desc_v1 { | 
|  | 78 | struct usb_functionfs_descs_head_v1 { | 
|  | 79 | __le32 magic; | 
|  | 80 | __le32 length; | 
|  | 81 | __le32 fs_count; | 
|  | 82 | __le32 hs_count; | 
|  | 83 | } __attribute__((packed)) header; | 
|  | 84 | struct func_desc fs_descs, hs_descs; | 
|  | 85 | } __attribute__((packed)); | 
|  | 86 |  | 
|  | 87 | struct desc_v2 { | 
| Christopher Ferris | c49f51c | 2015-01-23 17:09:56 -0800 | [diff] [blame] | 88 | struct usb_functionfs_descs_head_v2 header; | 
|  | 89 | // The rest of the structure depends on the flags in the header. | 
|  | 90 | __le32 fs_count; | 
|  | 91 | __le32 hs_count; | 
| Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 92 | __le32 ss_count; | 
| Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 93 | __le32 os_count; | 
| Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 94 | struct func_desc fs_descs, hs_descs; | 
| Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 95 | struct ss_func_desc ss_descs; | 
| Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 96 | struct usb_os_desc_header os_header; | 
|  | 97 | struct usb_ext_compat_desc os_desc; | 
| Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 98 | } __attribute__((packed)); | 
|  | 99 |  | 
| Elliott Hughes | 3edd54b | 2015-05-05 18:26:10 -0700 | [diff] [blame] | 100 | static struct func_desc fs_descriptors = { | 
| Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 101 | .intf = { | 
|  | 102 | .bLength = sizeof(fs_descriptors.intf), | 
|  | 103 | .bDescriptorType = USB_DT_INTERFACE, | 
|  | 104 | .bInterfaceNumber = 0, | 
|  | 105 | .bNumEndpoints = 2, | 
|  | 106 | .bInterfaceClass = ADB_CLASS, | 
|  | 107 | .bInterfaceSubClass = ADB_SUBCLASS, | 
|  | 108 | .bInterfaceProtocol = ADB_PROTOCOL, | 
|  | 109 | .iInterface = 1, /* first string from the provided table */ | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 110 | }, | 
| Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 111 | .source = { | 
|  | 112 | .bLength = sizeof(fs_descriptors.source), | 
|  | 113 | .bDescriptorType = USB_DT_ENDPOINT, | 
|  | 114 | .bEndpointAddress = 1 | USB_DIR_OUT, | 
|  | 115 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | 
|  | 116 | .wMaxPacketSize = MAX_PACKET_SIZE_FS, | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 117 | }, | 
| Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 118 | .sink = { | 
|  | 119 | .bLength = sizeof(fs_descriptors.sink), | 
|  | 120 | .bDescriptorType = USB_DT_ENDPOINT, | 
|  | 121 | .bEndpointAddress = 2 | USB_DIR_IN, | 
|  | 122 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | 
|  | 123 | .wMaxPacketSize = MAX_PACKET_SIZE_FS, | 
|  | 124 | }, | 
|  | 125 | }; | 
|  | 126 |  | 
| Elliott Hughes | 3edd54b | 2015-05-05 18:26:10 -0700 | [diff] [blame] | 127 | static struct func_desc hs_descriptors = { | 
| Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 128 | .intf = { | 
|  | 129 | .bLength = sizeof(hs_descriptors.intf), | 
|  | 130 | .bDescriptorType = USB_DT_INTERFACE, | 
|  | 131 | .bInterfaceNumber = 0, | 
|  | 132 | .bNumEndpoints = 2, | 
|  | 133 | .bInterfaceClass = ADB_CLASS, | 
|  | 134 | .bInterfaceSubClass = ADB_SUBCLASS, | 
|  | 135 | .bInterfaceProtocol = ADB_PROTOCOL, | 
|  | 136 | .iInterface = 1, /* first string from the provided table */ | 
|  | 137 | }, | 
|  | 138 | .source = { | 
|  | 139 | .bLength = sizeof(hs_descriptors.source), | 
|  | 140 | .bDescriptorType = USB_DT_ENDPOINT, | 
|  | 141 | .bEndpointAddress = 1 | USB_DIR_OUT, | 
|  | 142 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | 
|  | 143 | .wMaxPacketSize = MAX_PACKET_SIZE_HS, | 
|  | 144 | }, | 
|  | 145 | .sink = { | 
|  | 146 | .bLength = sizeof(hs_descriptors.sink), | 
|  | 147 | .bDescriptorType = USB_DT_ENDPOINT, | 
|  | 148 | .bEndpointAddress = 2 | USB_DIR_IN, | 
|  | 149 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | 
|  | 150 | .wMaxPacketSize = MAX_PACKET_SIZE_HS, | 
| Zhuang Jin Can | d6ee9f2 | 2014-09-02 13:04:44 +0800 | [diff] [blame] | 151 | }, | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 152 | }; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 153 |  | 
| Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 154 | static struct ss_func_desc ss_descriptors = { | 
|  | 155 | .intf = { | 
|  | 156 | .bLength = sizeof(ss_descriptors.intf), | 
|  | 157 | .bDescriptorType = USB_DT_INTERFACE, | 
|  | 158 | .bInterfaceNumber = 0, | 
|  | 159 | .bNumEndpoints = 2, | 
|  | 160 | .bInterfaceClass = ADB_CLASS, | 
|  | 161 | .bInterfaceSubClass = ADB_SUBCLASS, | 
|  | 162 | .bInterfaceProtocol = ADB_PROTOCOL, | 
|  | 163 | .iInterface = 1, /* first string from the provided table */ | 
|  | 164 | }, | 
|  | 165 | .source = { | 
|  | 166 | .bLength = sizeof(ss_descriptors.source), | 
|  | 167 | .bDescriptorType = USB_DT_ENDPOINT, | 
|  | 168 | .bEndpointAddress = 1 | USB_DIR_OUT, | 
|  | 169 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | 
|  | 170 | .wMaxPacketSize = MAX_PACKET_SIZE_SS, | 
|  | 171 | }, | 
|  | 172 | .source_comp = { | 
|  | 173 | .bLength = sizeof(ss_descriptors.source_comp), | 
|  | 174 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | 
| Jerry Zhang | b5a34a2 | 2017-02-10 17:43:00 -0800 | [diff] [blame] | 175 | .bMaxBurst = 4, | 
| Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 176 | }, | 
|  | 177 | .sink = { | 
|  | 178 | .bLength = sizeof(ss_descriptors.sink), | 
|  | 179 | .bDescriptorType = USB_DT_ENDPOINT, | 
|  | 180 | .bEndpointAddress = 2 | USB_DIR_IN, | 
|  | 181 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | 
|  | 182 | .wMaxPacketSize = MAX_PACKET_SIZE_SS, | 
|  | 183 | }, | 
|  | 184 | .sink_comp = { | 
|  | 185 | .bLength = sizeof(ss_descriptors.sink_comp), | 
|  | 186 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | 
| Jerry Zhang | b5a34a2 | 2017-02-10 17:43:00 -0800 | [diff] [blame] | 187 | .bMaxBurst = 4, | 
| Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 188 | }, | 
|  | 189 | }; | 
|  | 190 |  | 
| Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 191 | struct usb_ext_compat_desc os_desc_compat = { | 
|  | 192 | .bFirstInterfaceNumber = 0, | 
|  | 193 | .Reserved1 = cpu_to_le32(1), | 
|  | 194 | .CompatibleID = {0}, | 
|  | 195 | .SubCompatibleID = {0}, | 
|  | 196 | .Reserved2 = {0}, | 
|  | 197 | }; | 
|  | 198 |  | 
|  | 199 | static struct usb_os_desc_header os_desc_header = { | 
|  | 200 | .interface = cpu_to_le32(1), | 
|  | 201 | .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)), | 
|  | 202 | .bcdVersion = cpu_to_le32(1), | 
|  | 203 | .wIndex = cpu_to_le32(4), | 
|  | 204 | .bCount = cpu_to_le32(1), | 
|  | 205 | .Reserved = cpu_to_le32(0), | 
|  | 206 | }; | 
|  | 207 |  | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 208 | #define STR_INTERFACE_ "ADB Interface" | 
|  | 209 |  | 
|  | 210 | static const struct { | 
|  | 211 | struct usb_functionfs_strings_head header; | 
|  | 212 | struct { | 
|  | 213 | __le16 code; | 
|  | 214 | const char str1[sizeof(STR_INTERFACE_)]; | 
|  | 215 | } __attribute__((packed)) lang0; | 
|  | 216 | } __attribute__((packed)) strings = { | 
|  | 217 | .header = { | 
|  | 218 | .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC), | 
|  | 219 | .length = cpu_to_le32(sizeof(strings)), | 
|  | 220 | .str_count = cpu_to_le32(1), | 
|  | 221 | .lang_count = cpu_to_le32(1), | 
|  | 222 | }, | 
|  | 223 | .lang0 = { | 
|  | 224 | cpu_to_le16(0x0409), /* en-us */ | 
|  | 225 | STR_INTERFACE_, | 
|  | 226 | }, | 
|  | 227 | }; | 
|  | 228 |  | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 229 | static void aio_block_init(aio_block* aiob, unsigned num_bufs) { | 
|  | 230 | aiob->iocb.resize(num_bufs); | 
|  | 231 | aiob->iocbs.resize(num_bufs); | 
|  | 232 | aiob->events.resize(num_bufs); | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 233 | aiob->num_submitted = 0; | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 234 | for (unsigned i = 0; i < num_bufs; i++) { | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 235 | aiob->iocbs[i] = &aiob->iocb[i]; | 
|  | 236 | } | 
| Jerry Zhang | d8c1ae9 | 2018-05-15 16:30:10 -0700 | [diff] [blame] | 237 | memset(&aiob->ctx, 0, sizeof(aiob->ctx)); | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 238 | if (io_setup(num_bufs, &aiob->ctx)) { | 
| Jerry Zhang | d8c1ae9 | 2018-05-15 16:30:10 -0700 | [diff] [blame] | 239 | D("[ aio: got error on io_setup (%d) ]", errno); | 
|  | 240 | } | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
|  | 243 | static int getMaxPacketSize(int ffs_fd) { | 
|  | 244 | usb_endpoint_descriptor desc; | 
|  | 245 | if (ioctl(ffs_fd, FUNCTIONFS_ENDPOINT_DESC, reinterpret_cast<unsigned long>(&desc))) { | 
|  | 246 | D("[ could not get endpoint descriptor! (%d) ]", errno); | 
|  | 247 | return MAX_PACKET_SIZE_HS; | 
|  | 248 | } else { | 
|  | 249 | return desc.wMaxPacketSize; | 
|  | 250 | } | 
|  | 251 | } | 
|  | 252 |  | 
| Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 253 | static bool init_functionfs(struct usb_handle* h) { | 
| Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 254 | LOG(INFO) << "initializing functionfs"; | 
|  | 255 |  | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 256 | ssize_t ret; | 
| Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 257 | struct desc_v1 v1_descriptor; | 
|  | 258 | struct desc_v2 v2_descriptor; | 
|  | 259 |  | 
|  | 260 | v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2); | 
|  | 261 | v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor)); | 
| Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 262 | v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC | | 
| Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 263 | FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC; | 
| Christopher Ferris | c49f51c | 2015-01-23 17:09:56 -0800 | [diff] [blame] | 264 | v2_descriptor.fs_count = 3; | 
|  | 265 | v2_descriptor.hs_count = 3; | 
| Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 266 | v2_descriptor.ss_count = 5; | 
| Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 267 | v2_descriptor.os_count = 1; | 
| Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 268 | v2_descriptor.fs_descs = fs_descriptors; | 
|  | 269 | v2_descriptor.hs_descs = hs_descriptors; | 
| Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 270 | v2_descriptor.ss_descs = ss_descriptors; | 
| Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 271 | v2_descriptor.os_header = os_desc_header; | 
|  | 272 | v2_descriptor.os_desc = os_desc_compat; | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 273 |  | 
| Siqi Lin | 8c40762 | 2016-05-26 13:04:52 -0700 | [diff] [blame] | 274 | if (h->control < 0) { // might have already done this before | 
| Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 275 | LOG(INFO) << "opening control endpoint " << USB_FFS_ADB_EP0; | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 276 | h->control.reset(adb_open(USB_FFS_ADB_EP0, O_WRONLY)); | 
| Siqi Lin | 8c40762 | 2016-05-26 13:04:52 -0700 | [diff] [blame] | 277 | if (h->control < 0) { | 
| Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 278 | PLOG(ERROR) << "cannot open control endpoint " << USB_FFS_ADB_EP0; | 
| Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 279 | goto err; | 
|  | 280 | } | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 281 |  | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 282 | ret = adb_write(h->control.get(), &v2_descriptor, sizeof(v2_descriptor)); | 
| Siqi Lin | 8c40762 | 2016-05-26 13:04:52 -0700 | [diff] [blame] | 283 | if (ret < 0) { | 
|  | 284 | v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC); | 
|  | 285 | v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor)); | 
|  | 286 | v1_descriptor.header.fs_count = 3; | 
|  | 287 | v1_descriptor.header.hs_count = 3; | 
|  | 288 | v1_descriptor.fs_descs = fs_descriptors; | 
|  | 289 | v1_descriptor.hs_descs = hs_descriptors; | 
|  | 290 | D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno); | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 291 | ret = adb_write(h->control.get(), &v1_descriptor, sizeof(v1_descriptor)); | 
| Siqi Lin | 8c40762 | 2016-05-26 13:04:52 -0700 | [diff] [blame] | 292 | if (ret < 0) { | 
|  | 293 | D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno); | 
|  | 294 | goto err; | 
|  | 295 | } | 
|  | 296 | } | 
|  | 297 |  | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 298 | ret = adb_write(h->control.get(), &strings, sizeof(strings)); | 
| Siqi Lin | 8c40762 | 2016-05-26 13:04:52 -0700 | [diff] [blame] | 299 | if (ret < 0) { | 
|  | 300 | D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno); | 
|  | 301 | goto err; | 
|  | 302 | } | 
| Badhri Jagan Sridharan | 43fd1a4 | 2017-03-07 17:30:03 -0800 | [diff] [blame] | 303 | //Signal only when writing the descriptors to ffs | 
|  | 304 | android::base::SetProperty("sys.usb.ffs.ready", "1"); | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 305 | } | 
|  | 306 |  | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 307 | h->bulk_out.reset(adb_open(USB_FFS_ADB_OUT, O_RDONLY)); | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 308 | if (h->bulk_out < 0) { | 
| Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 309 | PLOG(ERROR) << "cannot open bulk-out endpoint " << USB_FFS_ADB_OUT; | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 310 | goto err; | 
|  | 311 | } | 
|  | 312 |  | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 313 | h->bulk_in.reset(adb_open(USB_FFS_ADB_IN, O_WRONLY)); | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 314 | if (h->bulk_in < 0) { | 
| Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 315 | PLOG(ERROR) << "cannot open bulk-in endpoint " << USB_FFS_ADB_IN; | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 316 | goto err; | 
|  | 317 | } | 
|  | 318 |  | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 319 | h->read_aiob.fd = h->bulk_out; | 
|  | 320 | h->write_aiob.fd = h->bulk_in; | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 321 | h->reads_zero_packets = true; | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 322 | return true; | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 323 |  | 
|  | 324 | err: | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 325 | h->bulk_in.reset(); | 
|  | 326 | h->bulk_out.reset(); | 
|  | 327 | h->control.reset(); | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 328 | return false; | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 329 | } | 
|  | 330 |  | 
| Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 331 | static void usb_ffs_open_thread(usb_handle *usb) { | 
| Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 332 | adb_thread_setname("usb ffs open"); | 
|  | 333 |  | 
| Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 334 | while (true) { | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 335 | // wait until the USB device needs opening | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 336 | std::unique_lock<std::mutex> lock(usb->lock); | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 337 | while (!usb->open_new_connection) { | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 338 | usb->notify.wait(lock); | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 339 | } | 
|  | 340 | usb->open_new_connection = false; | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 341 | lock.unlock(); | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 342 |  | 
| Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 343 | while (true) { | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 344 | if (init_functionfs(usb)) { | 
| Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 345 | LOG(INFO) << "functionfs successfully initialized"; | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 346 | break; | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 347 | } | 
| Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 348 | std::this_thread::sleep_for(1s); | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 349 | } | 
|  | 350 |  | 
| Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 351 | LOG(INFO) << "registering usb transport"; | 
| Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 352 | register_usb_transport(usb, nullptr, nullptr, 1); | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 353 | } | 
|  | 354 |  | 
|  | 355 | // never gets here | 
| Josh Gao | d9db09c | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 356 | abort(); | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 357 | } | 
|  | 358 |  | 
| Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 359 | static int usb_ffs_write(usb_handle* h, const void* data, int len) { | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 360 | D("about to write (fd=%d, len=%d)", h->bulk_in.get(), len); | 
| Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 361 |  | 
| Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 362 | const char* buf = static_cast<const char*>(data); | 
| Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 363 | int orig_len = len; | 
| Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 364 | while (len > 0) { | 
| Jerry Zhang | 99499f1 | 2018-03-14 14:57:31 -0700 | [diff] [blame] | 365 | int write_len = std::min(USB_FFS_BULK_SIZE, len); | 
| Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 366 | int n = adb_write(h->bulk_in, buf, write_len); | 
|  | 367 | if (n < 0) { | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 368 | D("ERROR: fd = %d, n = %d: %s", h->bulk_in.get(), n, strerror(errno)); | 
| Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 369 | return -1; | 
|  | 370 | } | 
|  | 371 | buf += n; | 
|  | 372 | len -= n; | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 373 | } | 
| Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 374 |  | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 375 | D("[ done fd=%d ]", h->bulk_in.get()); | 
| Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 376 | return orig_len; | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 377 | } | 
|  | 378 |  | 
| Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 379 | static int usb_ffs_read(usb_handle* h, void* data, int len) { | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 380 | D("about to read (fd=%d, len=%d)", h->bulk_out.get(), len); | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 381 |  | 
| Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 382 | char* buf = static_cast<char*>(data); | 
| Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 383 | int orig_len = len; | 
| Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 384 | while (len > 0) { | 
| Jerry Zhang | 99499f1 | 2018-03-14 14:57:31 -0700 | [diff] [blame] | 385 | int read_len = std::min(USB_FFS_BULK_SIZE, len); | 
| Josh Gao | 0b19540 | 2015-12-16 11:00:08 -0800 | [diff] [blame] | 386 | int n = adb_read(h->bulk_out, buf, read_len); | 
| Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 387 | if (n < 0) { | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 388 | D("ERROR: fd = %d, n = %d: %s", h->bulk_out.get(), n, strerror(errno)); | 
| Elliott Hughes | 8fcd8bc | 2015-08-25 10:59:45 -0700 | [diff] [blame] | 389 | return -1; | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 390 | } | 
| Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 391 | buf += n; | 
|  | 392 | len -= n; | 
| Elliott Hughes | 8fcd8bc | 2015-08-25 10:59:45 -0700 | [diff] [blame] | 393 | } | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 394 |  | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 395 | D("[ done fd=%d ]", h->bulk_out.get()); | 
| Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 396 | return orig_len; | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 397 | } | 
|  | 398 |  | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 399 | static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) { | 
|  | 400 | aio_block* aiob = read ? &h->read_aiob : &h->write_aiob; | 
|  | 401 | bool zero_packet = false; | 
|  | 402 |  | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 403 | int num_bufs = len / h->io_size + (len % h->io_size == 0 ? 0 : 1); | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 404 | const char* cur_data = reinterpret_cast<const char*>(data); | 
|  | 405 | int packet_size = getMaxPacketSize(aiob->fd); | 
|  | 406 |  | 
|  | 407 | if (posix_madvise(const_cast<void*>(data), len, POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED) < | 
|  | 408 | 0) { | 
|  | 409 | D("[ Failed to madvise: %d ]", errno); | 
|  | 410 | } | 
|  | 411 |  | 
|  | 412 | for (int i = 0; i < num_bufs; i++) { | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 413 | int buf_len = std::min(len, static_cast<int>(h->io_size)); | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 414 | io_prep(&aiob->iocb[i], aiob->fd, cur_data, buf_len, 0, read); | 
|  | 415 |  | 
|  | 416 | len -= buf_len; | 
|  | 417 | cur_data += buf_len; | 
|  | 418 |  | 
|  | 419 | if (len == 0 && buf_len % packet_size == 0 && read) { | 
|  | 420 | // adb does not expect the device to send a zero packet after data transfer, | 
|  | 421 | // but the host *does* send a zero packet for the device to read. | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 422 | zero_packet = h->reads_zero_packets; | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 423 | } | 
|  | 424 | } | 
|  | 425 | if (zero_packet) { | 
|  | 426 | io_prep(&aiob->iocb[num_bufs], aiob->fd, reinterpret_cast<const void*>(cur_data), | 
|  | 427 | packet_size, 0, read); | 
|  | 428 | num_bufs += 1; | 
|  | 429 | } | 
|  | 430 |  | 
| Jerry Zhang | 5ba6802 | 2018-03-19 17:54:39 -0700 | [diff] [blame] | 431 | while (true) { | 
|  | 432 | if (TEMP_FAILURE_RETRY(io_submit(aiob->ctx, num_bufs, aiob->iocbs.data())) < num_bufs) { | 
|  | 433 | PLOG(ERROR) << "aio: got error submitting " << (read ? "read" : "write"); | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 434 | return -1; | 
|  | 435 | } | 
| Jerry Zhang | 5ba6802 | 2018-03-19 17:54:39 -0700 | [diff] [blame] | 436 | if (TEMP_FAILURE_RETRY(io_getevents(aiob->ctx, num_bufs, num_bufs, aiob->events.data(), | 
|  | 437 | nullptr)) < num_bufs) { | 
|  | 438 | PLOG(ERROR) << "aio: got error waiting " << (read ? "read" : "write"); | 
|  | 439 | return -1; | 
|  | 440 | } | 
|  | 441 | if (num_bufs == 1 && aiob->events[0].res == -EINTR) { | 
|  | 442 | continue; | 
|  | 443 | } | 
| Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 444 | int ret = 0; | 
| Jerry Zhang | 5ba6802 | 2018-03-19 17:54:39 -0700 | [diff] [blame] | 445 | for (int i = 0; i < num_bufs; i++) { | 
|  | 446 | if (aiob->events[i].res < 0) { | 
|  | 447 | errno = -aiob->events[i].res; | 
|  | 448 | PLOG(ERROR) << "aio: got error event on " << (read ? "read" : "write") | 
|  | 449 | << " total bufs " << num_bufs; | 
|  | 450 | return -1; | 
|  | 451 | } | 
| Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 452 | ret += aiob->events[i].res; | 
| Jerry Zhang | 5ba6802 | 2018-03-19 17:54:39 -0700 | [diff] [blame] | 453 | } | 
| Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 454 | return ret; | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 455 | } | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 456 | } | 
|  | 457 |  | 
|  | 458 | static int usb_ffs_aio_read(usb_handle* h, void* data, int len) { | 
|  | 459 | return usb_ffs_do_aio(h, data, len, true); | 
|  | 460 | } | 
|  | 461 |  | 
|  | 462 | static int usb_ffs_aio_write(usb_handle* h, const void* data, int len) { | 
|  | 463 | return usb_ffs_do_aio(h, data, len, false); | 
|  | 464 | } | 
|  | 465 |  | 
| Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 466 | static void usb_ffs_kick(usb_handle* h) { | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 467 | int err; | 
|  | 468 |  | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 469 | err = ioctl(h->bulk_in.get(), FUNCTIONFS_CLEAR_HALT); | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 470 | if (err < 0) { | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 471 | D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in.get(), errno); | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 472 | } | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 473 |  | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 474 | err = ioctl(h->bulk_out.get(), FUNCTIONFS_CLEAR_HALT); | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 475 | if (err < 0) { | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 476 | D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out.get(), errno); | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 477 | } | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 478 |  | 
| Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 479 | // don't close ep0 here, since we may not need to reinitialize it with | 
|  | 480 | // the same descriptors again. if however ep1/ep2 fail to re-open in | 
|  | 481 | // init_functionfs, only then would we close and open ep0 again. | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 482 | // Ditto the comment in usb_adb_kick. | 
|  | 483 | h->kicked = true; | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 484 | TEMP_FAILURE_RETRY(dup2(dummy_fd.get(), h->bulk_out.get())); | 
|  | 485 | TEMP_FAILURE_RETRY(dup2(dummy_fd.get(), h->bulk_in.get())); | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 486 | } | 
|  | 487 |  | 
| Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 488 | static void usb_ffs_close(usb_handle* h) { | 
| Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 489 | LOG(INFO) << "closing functionfs transport"; | 
|  | 490 |  | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 491 | h->kicked = false; | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 492 | h->bulk_out.reset(); | 
|  | 493 | h->bulk_in.reset(); | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 494 |  | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 495 | // Notify usb_adb_open_thread to open a new connection. | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 496 | h->lock.lock(); | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 497 | h->open_new_connection = true; | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 498 | h->lock.unlock(); | 
|  | 499 | h->notify.notify_one(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 500 | } | 
|  | 501 |  | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 502 | usb_handle *create_usb_handle(unsigned num_bufs, unsigned io_size) { | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 503 | usb_handle* h = new usb_handle(); | 
| Elliott Hughes | dc3b459 | 2015-04-21 19:39:52 -0700 | [diff] [blame] | 504 |  | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 505 | if (android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false)) { | 
|  | 506 | // Devices on older kernels (< 3.18) will not have aio support for ffs | 
|  | 507 | // unless backported. Fall back on the non-aio functions instead. | 
|  | 508 | h->write = usb_ffs_write; | 
|  | 509 | h->read = usb_ffs_read; | 
|  | 510 | } else { | 
|  | 511 | h->write = usb_ffs_aio_write; | 
|  | 512 | h->read = usb_ffs_aio_read; | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 513 | aio_block_init(&h->read_aiob, num_bufs); | 
|  | 514 | aio_block_init(&h->write_aiob, num_bufs); | 
| Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 515 | } | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 516 | h->io_size = io_size; | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 517 | h->kick = usb_ffs_kick; | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 518 | h->close = usb_ffs_close; | 
| Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 519 | return h; | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 520 | } | 
|  | 521 |  | 
| Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 522 | void usb_init() { | 
| Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 523 | D("[ usb_init - using FunctionFS ]"); | 
| Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 524 | dummy_fd.reset(adb_open("/dev/null", O_WRONLY | O_CLOEXEC)); | 
|  | 525 | CHECK_NE(-1, dummy_fd.get()); | 
| Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 526 |  | 
| Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 527 | std::thread(usb_ffs_open_thread, create_usb_handle(USB_FFS_NUM_BUFS, USB_FFS_BULK_SIZE)).detach(); | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 528 | } | 
|  | 529 |  | 
| Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 530 | int usb_write(usb_handle* h, const void* data, int len) { | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 531 | return h->write(h, data, len); | 
|  | 532 | } | 
|  | 533 |  | 
| Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 534 | int usb_read(usb_handle* h, void* data, int len) { | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 535 | return h->read(h, data, len); | 
|  | 536 | } | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 537 |  | 
| Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 538 | int usb_close(usb_handle* h) { | 
| Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 539 | h->close(h); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 540 | return 0; | 
|  | 541 | } | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 542 |  | 
| Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 543 | void usb_kick(usb_handle* h) { | 
| Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 544 | h->kick(h); | 
|  | 545 | } |