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 | |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 61 | static int dummy_fd = -1; |
| 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 | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 229 | static void aio_block_init(aio_block* aiob) { |
| 230 | aiob->iocb.resize(USB_FFS_NUM_BUFS); |
| 231 | aiob->iocbs.resize(USB_FFS_NUM_BUFS); |
| 232 | aiob->events.resize(USB_FFS_NUM_BUFS); |
| 233 | aiob->num_submitted = 0; |
| 234 | for (unsigned i = 0; i < USB_FFS_NUM_BUFS; i++) { |
| 235 | aiob->iocbs[i] = &aiob->iocb[i]; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | static int getMaxPacketSize(int ffs_fd) { |
| 240 | usb_endpoint_descriptor desc; |
| 241 | if (ioctl(ffs_fd, FUNCTIONFS_ENDPOINT_DESC, reinterpret_cast<unsigned long>(&desc))) { |
| 242 | D("[ could not get endpoint descriptor! (%d) ]", errno); |
| 243 | return MAX_PACKET_SIZE_HS; |
| 244 | } else { |
| 245 | return desc.wMaxPacketSize; |
| 246 | } |
| 247 | } |
| 248 | |
Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 249 | bool init_functionfs(struct usb_handle* h) { |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 250 | LOG(INFO) << "initializing functionfs"; |
| 251 | |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 252 | ssize_t ret; |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 253 | struct desc_v1 v1_descriptor; |
| 254 | struct desc_v2 v2_descriptor; |
| 255 | |
| 256 | v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2); |
| 257 | v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor)); |
Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 258 | 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] | 259 | FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC; |
Christopher Ferris | c49f51c | 2015-01-23 17:09:56 -0800 | [diff] [blame] | 260 | v2_descriptor.fs_count = 3; |
| 261 | v2_descriptor.hs_count = 3; |
Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 262 | v2_descriptor.ss_count = 5; |
Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 263 | v2_descriptor.os_count = 1; |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 264 | v2_descriptor.fs_descs = fs_descriptors; |
| 265 | v2_descriptor.hs_descs = hs_descriptors; |
Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 266 | v2_descriptor.ss_descs = ss_descriptors; |
Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 267 | v2_descriptor.os_header = os_desc_header; |
| 268 | v2_descriptor.os_desc = os_desc_compat; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 269 | |
Siqi Lin | 8c40762 | 2016-05-26 13:04:52 -0700 | [diff] [blame] | 270 | if (h->control < 0) { // might have already done this before |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 271 | LOG(INFO) << "opening control endpoint " << USB_FFS_ADB_EP0; |
Luis Hector Chavez | 3a3df40 | 2018-03-09 10:14:01 -0800 | [diff] [blame] | 272 | h->control = adb_open(USB_FFS_ADB_EP0, O_WRONLY); |
Siqi Lin | 8c40762 | 2016-05-26 13:04:52 -0700 | [diff] [blame] | 273 | if (h->control < 0) { |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 274 | PLOG(ERROR) << "cannot open control endpoint " << USB_FFS_ADB_EP0; |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 275 | goto err; |
| 276 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 277 | |
Siqi Lin | 8c40762 | 2016-05-26 13:04:52 -0700 | [diff] [blame] | 278 | ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor)); |
| 279 | if (ret < 0) { |
| 280 | v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC); |
| 281 | v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor)); |
| 282 | v1_descriptor.header.fs_count = 3; |
| 283 | v1_descriptor.header.hs_count = 3; |
| 284 | v1_descriptor.fs_descs = fs_descriptors; |
| 285 | v1_descriptor.hs_descs = hs_descriptors; |
| 286 | D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno); |
| 287 | ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor)); |
| 288 | if (ret < 0) { |
| 289 | D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno); |
| 290 | goto err; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | ret = adb_write(h->control, &strings, sizeof(strings)); |
| 295 | if (ret < 0) { |
| 296 | D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno); |
| 297 | goto err; |
| 298 | } |
Badhri Jagan Sridharan | 43fd1a4 | 2017-03-07 17:30:03 -0800 | [diff] [blame] | 299 | //Signal only when writing the descriptors to ffs |
| 300 | android::base::SetProperty("sys.usb.ffs.ready", "1"); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Luis Hector Chavez | 3a3df40 | 2018-03-09 10:14:01 -0800 | [diff] [blame] | 303 | h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDONLY); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 304 | if (h->bulk_out < 0) { |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 305 | PLOG(ERROR) << "cannot open bulk-out endpoint " << USB_FFS_ADB_OUT; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 306 | goto err; |
| 307 | } |
| 308 | |
Luis Hector Chavez | 3a3df40 | 2018-03-09 10:14:01 -0800 | [diff] [blame] | 309 | h->bulk_in = adb_open(USB_FFS_ADB_IN, O_WRONLY); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 310 | if (h->bulk_in < 0) { |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 311 | PLOG(ERROR) << "cannot open bulk-in endpoint " << USB_FFS_ADB_IN; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 312 | goto err; |
| 313 | } |
| 314 | |
Jerry Zhang | c3d4e72 | 2018-02-12 16:15:50 -0800 | [diff] [blame] | 315 | memset(&h->read_aiob.ctx, 0, sizeof(h->read_aiob.ctx)); |
| 316 | memset(&h->write_aiob.ctx, 0, sizeof(h->write_aiob.ctx)); |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 317 | if (io_setup(USB_FFS_NUM_BUFS, &h->read_aiob.ctx) || |
| 318 | io_setup(USB_FFS_NUM_BUFS, &h->write_aiob.ctx)) { |
| 319 | D("[ aio: got error on io_setup (%d) ]", errno); |
| 320 | } |
| 321 | |
| 322 | h->read_aiob.fd = h->bulk_out; |
| 323 | h->write_aiob.fd = h->bulk_in; |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 324 | return true; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 325 | |
| 326 | err: |
| 327 | if (h->bulk_in > 0) { |
| 328 | adb_close(h->bulk_in); |
| 329 | h->bulk_in = -1; |
| 330 | } |
| 331 | if (h->bulk_out > 0) { |
| 332 | adb_close(h->bulk_out); |
| 333 | h->bulk_out = -1; |
| 334 | } |
| 335 | if (h->control > 0) { |
| 336 | adb_close(h->control); |
| 337 | h->control = -1; |
| 338 | } |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 339 | return false; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Josh Gao | d9db09c | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 342 | static void usb_ffs_open_thread(void* x) { |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 343 | struct usb_handle* usb = (struct usb_handle*)x; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 344 | |
Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 345 | adb_thread_setname("usb ffs open"); |
| 346 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 347 | while (true) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 348 | // wait until the USB device needs opening |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 349 | std::unique_lock<std::mutex> lock(usb->lock); |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 350 | while (!usb->open_new_connection) { |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 351 | usb->notify.wait(lock); |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 352 | } |
| 353 | usb->open_new_connection = false; |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 354 | lock.unlock(); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 355 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 356 | while (true) { |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 357 | if (init_functionfs(usb)) { |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 358 | LOG(INFO) << "functionfs successfully initialized"; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 359 | break; |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 360 | } |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 361 | std::this_thread::sleep_for(1s); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 364 | LOG(INFO) << "registering usb transport"; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 365 | register_usb_transport(usb, 0, 0, 1); |
| 366 | } |
| 367 | |
| 368 | // never gets here |
Josh Gao | d9db09c | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 369 | abort(); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 372 | 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] | 373 | D("about to write (fd=%d, len=%d)", h->bulk_in, len); |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 374 | |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 375 | const char* buf = static_cast<const char*>(data); |
| 376 | while (len > 0) { |
Jerry Zhang | 99499f1 | 2018-03-14 14:57:31 -0700 | [diff] [blame] | 377 | int write_len = std::min(USB_FFS_BULK_SIZE, len); |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 378 | int n = adb_write(h->bulk_in, buf, write_len); |
| 379 | if (n < 0) { |
| 380 | D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno)); |
| 381 | return -1; |
| 382 | } |
| 383 | buf += n; |
| 384 | len -= n; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 385 | } |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 386 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 387 | D("[ done fd=%d ]", h->bulk_in); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 391 | static int usb_ffs_read(usb_handle* h, void* data, int len) { |
| 392 | D("about to read (fd=%d, len=%d)", h->bulk_out, len); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 393 | |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 394 | char* buf = static_cast<char*>(data); |
| 395 | while (len > 0) { |
Jerry Zhang | 99499f1 | 2018-03-14 14:57:31 -0700 | [diff] [blame] | 396 | int read_len = std::min(USB_FFS_BULK_SIZE, len); |
Josh Gao | 0b19540 | 2015-12-16 11:00:08 -0800 | [diff] [blame] | 397 | int n = adb_read(h->bulk_out, buf, read_len); |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 398 | if (n < 0) { |
| 399 | 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] | 400 | return -1; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 401 | } |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 402 | buf += n; |
| 403 | len -= n; |
Elliott Hughes | 8fcd8bc | 2015-08-25 10:59:45 -0700 | [diff] [blame] | 404 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 405 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 406 | D("[ done fd=%d ]", h->bulk_out); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 410 | static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) { |
| 411 | aio_block* aiob = read ? &h->read_aiob : &h->write_aiob; |
| 412 | bool zero_packet = false; |
| 413 | |
| 414 | int num_bufs = len / USB_FFS_BULK_SIZE + (len % USB_FFS_BULK_SIZE == 0 ? 0 : 1); |
| 415 | const char* cur_data = reinterpret_cast<const char*>(data); |
| 416 | int packet_size = getMaxPacketSize(aiob->fd); |
| 417 | |
| 418 | if (posix_madvise(const_cast<void*>(data), len, POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED) < |
| 419 | 0) { |
| 420 | D("[ Failed to madvise: %d ]", errno); |
| 421 | } |
| 422 | |
| 423 | for (int i = 0; i < num_bufs; i++) { |
| 424 | int buf_len = std::min(len, USB_FFS_BULK_SIZE); |
| 425 | io_prep(&aiob->iocb[i], aiob->fd, cur_data, buf_len, 0, read); |
| 426 | |
| 427 | len -= buf_len; |
| 428 | cur_data += buf_len; |
| 429 | |
| 430 | if (len == 0 && buf_len % packet_size == 0 && read) { |
| 431 | // adb does not expect the device to send a zero packet after data transfer, |
| 432 | // but the host *does* send a zero packet for the device to read. |
| 433 | zero_packet = true; |
| 434 | } |
| 435 | } |
| 436 | if (zero_packet) { |
| 437 | io_prep(&aiob->iocb[num_bufs], aiob->fd, reinterpret_cast<const void*>(cur_data), |
| 438 | packet_size, 0, read); |
| 439 | num_bufs += 1; |
| 440 | } |
| 441 | |
Jerry Zhang | 5ba6802 | 2018-03-19 17:54:39 -0700 | [diff] [blame^] | 442 | while (true) { |
| 443 | if (TEMP_FAILURE_RETRY(io_submit(aiob->ctx, num_bufs, aiob->iocbs.data())) < num_bufs) { |
| 444 | PLOG(ERROR) << "aio: got error submitting " << (read ? "read" : "write"); |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 445 | return -1; |
| 446 | } |
Jerry Zhang | 5ba6802 | 2018-03-19 17:54:39 -0700 | [diff] [blame^] | 447 | if (TEMP_FAILURE_RETRY(io_getevents(aiob->ctx, num_bufs, num_bufs, aiob->events.data(), |
| 448 | nullptr)) < num_bufs) { |
| 449 | PLOG(ERROR) << "aio: got error waiting " << (read ? "read" : "write"); |
| 450 | return -1; |
| 451 | } |
| 452 | if (num_bufs == 1 && aiob->events[0].res == -EINTR) { |
| 453 | continue; |
| 454 | } |
| 455 | for (int i = 0; i < num_bufs; i++) { |
| 456 | if (aiob->events[i].res < 0) { |
| 457 | errno = -aiob->events[i].res; |
| 458 | PLOG(ERROR) << "aio: got error event on " << (read ? "read" : "write") |
| 459 | << " total bufs " << num_bufs; |
| 460 | return -1; |
| 461 | } |
| 462 | } |
| 463 | return 0; |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 464 | } |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static int usb_ffs_aio_read(usb_handle* h, void* data, int len) { |
| 468 | return usb_ffs_do_aio(h, data, len, true); |
| 469 | } |
| 470 | |
| 471 | static int usb_ffs_aio_write(usb_handle* h, const void* data, int len) { |
| 472 | return usb_ffs_do_aio(h, data, len, false); |
| 473 | } |
| 474 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 475 | static void usb_ffs_kick(usb_handle* h) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 476 | int err; |
| 477 | |
| 478 | err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT); |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 479 | if (err < 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 480 | 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] | 481 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 482 | |
| 483 | err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT); |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 484 | if (err < 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 485 | 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] | 486 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 487 | |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 488 | // don't close ep0 here, since we may not need to reinitialize it with |
| 489 | // the same descriptors again. if however ep1/ep2 fail to re-open in |
| 490 | // init_functionfs, only then would we close and open ep0 again. |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 491 | // Ditto the comment in usb_adb_kick. |
| 492 | h->kicked = true; |
| 493 | TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_out)); |
| 494 | TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_in)); |
| 495 | } |
| 496 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 497 | static void usb_ffs_close(usb_handle* h) { |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 498 | LOG(INFO) << "closing functionfs transport"; |
| 499 | |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 500 | h->kicked = false; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 501 | adb_close(h->bulk_out); |
| 502 | adb_close(h->bulk_in); |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 503 | io_destroy(h->read_aiob.ctx); |
| 504 | io_destroy(h->write_aiob.ctx); |
| 505 | |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 506 | // Notify usb_adb_open_thread to open a new connection. |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 507 | h->lock.lock(); |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 508 | h->open_new_connection = true; |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 509 | h->lock.unlock(); |
| 510 | h->notify.notify_one(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 513 | static void usb_ffs_init() { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 514 | D("[ usb_init - using FunctionFS ]"); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 515 | |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 516 | usb_handle* h = new usb_handle(); |
Elliott Hughes | dc3b459 | 2015-04-21 19:39:52 -0700 | [diff] [blame] | 517 | |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 518 | if (android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false)) { |
| 519 | // Devices on older kernels (< 3.18) will not have aio support for ffs |
| 520 | // unless backported. Fall back on the non-aio functions instead. |
| 521 | h->write = usb_ffs_write; |
| 522 | h->read = usb_ffs_read; |
| 523 | } else { |
| 524 | h->write = usb_ffs_aio_write; |
| 525 | h->read = usb_ffs_aio_read; |
| 526 | aio_block_init(&h->read_aiob); |
| 527 | aio_block_init(&h->write_aiob); |
| 528 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 529 | h->kick = usb_ffs_kick; |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 530 | h->close = usb_ffs_close; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 531 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 532 | D("[ usb_init - starting thread ]"); |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 533 | std::thread(usb_ffs_open_thread, h).detach(); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 534 | } |
| 535 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 536 | void usb_init() { |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 537 | dummy_fd = adb_open("/dev/null", O_WRONLY); |
| 538 | CHECK_NE(dummy_fd, -1); |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 539 | usb_ffs_init(); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 542 | int usb_write(usb_handle* h, const void* data, int len) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 543 | return h->write(h, data, len); |
| 544 | } |
| 545 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 546 | int usb_read(usb_handle* h, void* data, int len) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 547 | return h->read(h, data, len); |
| 548 | } |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 549 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 550 | int usb_close(usb_handle* h) { |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 551 | h->close(h); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 552 | return 0; |
| 553 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 554 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 555 | void usb_kick(usb_handle* h) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 556 | h->kick(h); |
| 557 | } |