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