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> |
| 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | |
Josh Gao | ae72b5a | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 32 | #include <algorithm> |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 33 | #include <atomic> |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 34 | #include <chrono> |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 35 | #include <condition_variable> |
| 36 | #include <mutex> |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 37 | #include <thread> |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 38 | |
| 39 | #include <android-base/logging.h> |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 40 | #include <android-base/properties.h> |
Josh Gao | ae72b5a | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 41 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | #include "adb.h" |
Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame^] | 43 | #include "daemon/usb.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 44 | #include "transport.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 46 | using namespace std::chrono_literals; |
| 47 | |
Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame^] | 48 | #define MAX_PACKET_SIZE_FS 64 |
| 49 | #define MAX_PACKET_SIZE_HS 512 |
| 50 | #define MAX_PACKET_SIZE_SS 1024 |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 51 | |
Josh Gao | ae72b5a | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 52 | // Writes larger than 16k fail on some devices (seed with 3.10.49-g209ea2f in particular). |
| 53 | #define USB_FFS_MAX_WRITE 16384 |
| 54 | |
| 55 | // The kernel allocates a contiguous buffer for reads, which can fail for large ones due to |
| 56 | // fragmentation. 16k chosen arbitrarily to match the write limit. |
| 57 | #define USB_FFS_MAX_READ 16384 |
| 58 | |
Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame^] | 59 | #define cpu_to_le16(x) htole16(x) |
| 60 | #define cpu_to_le32(x) htole32(x) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 62 | static int dummy_fd = -1; |
| 63 | |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 64 | struct func_desc { |
| 65 | struct usb_interface_descriptor intf; |
| 66 | struct usb_endpoint_descriptor_no_audio source; |
| 67 | struct usb_endpoint_descriptor_no_audio sink; |
| 68 | } __attribute__((packed)); |
| 69 | |
Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 70 | struct ss_func_desc { |
| 71 | struct usb_interface_descriptor intf; |
| 72 | struct usb_endpoint_descriptor_no_audio source; |
| 73 | struct usb_ss_ep_comp_descriptor source_comp; |
| 74 | struct usb_endpoint_descriptor_no_audio sink; |
| 75 | struct usb_ss_ep_comp_descriptor sink_comp; |
| 76 | } __attribute__((packed)); |
| 77 | |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 78 | struct desc_v1 { |
| 79 | struct usb_functionfs_descs_head_v1 { |
| 80 | __le32 magic; |
| 81 | __le32 length; |
| 82 | __le32 fs_count; |
| 83 | __le32 hs_count; |
| 84 | } __attribute__((packed)) header; |
| 85 | struct func_desc fs_descs, hs_descs; |
| 86 | } __attribute__((packed)); |
| 87 | |
| 88 | struct desc_v2 { |
Christopher Ferris | c49f51c | 2015-01-23 17:09:56 -0800 | [diff] [blame] | 89 | struct usb_functionfs_descs_head_v2 header; |
| 90 | // The rest of the structure depends on the flags in the header. |
| 91 | __le32 fs_count; |
| 92 | __le32 hs_count; |
Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 93 | __le32 ss_count; |
Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 94 | __le32 os_count; |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 95 | struct func_desc fs_descs, hs_descs; |
Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 96 | struct ss_func_desc ss_descs; |
Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 97 | struct usb_os_desc_header os_header; |
| 98 | struct usb_ext_compat_desc os_desc; |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 99 | } __attribute__((packed)); |
| 100 | |
Elliott Hughes | 3edd54b | 2015-05-05 18:26:10 -0700 | [diff] [blame] | 101 | static struct func_desc fs_descriptors = { |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 102 | .intf = { |
| 103 | .bLength = sizeof(fs_descriptors.intf), |
| 104 | .bDescriptorType = USB_DT_INTERFACE, |
| 105 | .bInterfaceNumber = 0, |
| 106 | .bNumEndpoints = 2, |
| 107 | .bInterfaceClass = ADB_CLASS, |
| 108 | .bInterfaceSubClass = ADB_SUBCLASS, |
| 109 | .bInterfaceProtocol = ADB_PROTOCOL, |
| 110 | .iInterface = 1, /* first string from the provided table */ |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 111 | }, |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 112 | .source = { |
| 113 | .bLength = sizeof(fs_descriptors.source), |
| 114 | .bDescriptorType = USB_DT_ENDPOINT, |
| 115 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 116 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 117 | .wMaxPacketSize = MAX_PACKET_SIZE_FS, |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 118 | }, |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 119 | .sink = { |
| 120 | .bLength = sizeof(fs_descriptors.sink), |
| 121 | .bDescriptorType = USB_DT_ENDPOINT, |
| 122 | .bEndpointAddress = 2 | USB_DIR_IN, |
| 123 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 124 | .wMaxPacketSize = MAX_PACKET_SIZE_FS, |
| 125 | }, |
| 126 | }; |
| 127 | |
Elliott Hughes | 3edd54b | 2015-05-05 18:26:10 -0700 | [diff] [blame] | 128 | static struct func_desc hs_descriptors = { |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 129 | .intf = { |
| 130 | .bLength = sizeof(hs_descriptors.intf), |
| 131 | .bDescriptorType = USB_DT_INTERFACE, |
| 132 | .bInterfaceNumber = 0, |
| 133 | .bNumEndpoints = 2, |
| 134 | .bInterfaceClass = ADB_CLASS, |
| 135 | .bInterfaceSubClass = ADB_SUBCLASS, |
| 136 | .bInterfaceProtocol = ADB_PROTOCOL, |
| 137 | .iInterface = 1, /* first string from the provided table */ |
| 138 | }, |
| 139 | .source = { |
| 140 | .bLength = sizeof(hs_descriptors.source), |
| 141 | .bDescriptorType = USB_DT_ENDPOINT, |
| 142 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 143 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 144 | .wMaxPacketSize = MAX_PACKET_SIZE_HS, |
| 145 | }, |
| 146 | .sink = { |
| 147 | .bLength = sizeof(hs_descriptors.sink), |
| 148 | .bDescriptorType = USB_DT_ENDPOINT, |
| 149 | .bEndpointAddress = 2 | USB_DIR_IN, |
| 150 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 151 | .wMaxPacketSize = MAX_PACKET_SIZE_HS, |
Zhuang Jin Can | d6ee9f2 | 2014-09-02 13:04:44 +0800 | [diff] [blame] | 152 | }, |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 153 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | |
Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 155 | static struct ss_func_desc ss_descriptors = { |
| 156 | .intf = { |
| 157 | .bLength = sizeof(ss_descriptors.intf), |
| 158 | .bDescriptorType = USB_DT_INTERFACE, |
| 159 | .bInterfaceNumber = 0, |
| 160 | .bNumEndpoints = 2, |
| 161 | .bInterfaceClass = ADB_CLASS, |
| 162 | .bInterfaceSubClass = ADB_SUBCLASS, |
| 163 | .bInterfaceProtocol = ADB_PROTOCOL, |
| 164 | .iInterface = 1, /* first string from the provided table */ |
| 165 | }, |
| 166 | .source = { |
| 167 | .bLength = sizeof(ss_descriptors.source), |
| 168 | .bDescriptorType = USB_DT_ENDPOINT, |
| 169 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 170 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 171 | .wMaxPacketSize = MAX_PACKET_SIZE_SS, |
| 172 | }, |
| 173 | .source_comp = { |
| 174 | .bLength = sizeof(ss_descriptors.source_comp), |
| 175 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 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, |
| 187 | }, |
| 188 | }; |
| 189 | |
Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 190 | struct usb_ext_compat_desc os_desc_compat = { |
| 191 | .bFirstInterfaceNumber = 0, |
| 192 | .Reserved1 = cpu_to_le32(1), |
| 193 | .CompatibleID = {0}, |
| 194 | .SubCompatibleID = {0}, |
| 195 | .Reserved2 = {0}, |
| 196 | }; |
| 197 | |
| 198 | static struct usb_os_desc_header os_desc_header = { |
| 199 | .interface = cpu_to_le32(1), |
| 200 | .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)), |
| 201 | .bcdVersion = cpu_to_le32(1), |
| 202 | .wIndex = cpu_to_le32(4), |
| 203 | .bCount = cpu_to_le32(1), |
| 204 | .Reserved = cpu_to_le32(0), |
| 205 | }; |
| 206 | |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 207 | #define STR_INTERFACE_ "ADB Interface" |
| 208 | |
| 209 | static const struct { |
| 210 | struct usb_functionfs_strings_head header; |
| 211 | struct { |
| 212 | __le16 code; |
| 213 | const char str1[sizeof(STR_INTERFACE_)]; |
| 214 | } __attribute__((packed)) lang0; |
| 215 | } __attribute__((packed)) strings = { |
| 216 | .header = { |
| 217 | .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC), |
| 218 | .length = cpu_to_le32(sizeof(strings)), |
| 219 | .str_count = cpu_to_le32(1), |
| 220 | .lang_count = cpu_to_le32(1), |
| 221 | }, |
| 222 | .lang0 = { |
| 223 | cpu_to_le16(0x0409), /* en-us */ |
| 224 | STR_INTERFACE_, |
| 225 | }, |
| 226 | }; |
| 227 | |
Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame^] | 228 | bool init_functionfs(struct usb_handle* h) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 229 | ssize_t ret; |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 230 | struct desc_v1 v1_descriptor; |
| 231 | struct desc_v2 v2_descriptor; |
| 232 | |
| 233 | v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2); |
| 234 | v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor)); |
Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 235 | 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] | 236 | FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC; |
Christopher Ferris | c49f51c | 2015-01-23 17:09:56 -0800 | [diff] [blame] | 237 | v2_descriptor.fs_count = 3; |
| 238 | v2_descriptor.hs_count = 3; |
Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 239 | v2_descriptor.ss_count = 5; |
Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 240 | v2_descriptor.os_count = 1; |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 241 | v2_descriptor.fs_descs = fs_descriptors; |
| 242 | v2_descriptor.hs_descs = hs_descriptors; |
Jack Pham | a190c80 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 243 | v2_descriptor.ss_descs = ss_descriptors; |
Badhri Jagan Sridharan | ca2a0bd | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 244 | v2_descriptor.os_header = os_desc_header; |
| 245 | v2_descriptor.os_desc = os_desc_compat; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 246 | |
Siqi Lin | 8c40762 | 2016-05-26 13:04:52 -0700 | [diff] [blame] | 247 | if (h->control < 0) { // might have already done this before |
| 248 | D("OPENING %s", USB_FFS_ADB_EP0); |
| 249 | h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR); |
| 250 | if (h->control < 0) { |
| 251 | D("[ %s: cannot open control endpoint: errno=%d]", USB_FFS_ADB_EP0, errno); |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 252 | goto err; |
| 253 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 254 | |
Siqi Lin | 8c40762 | 2016-05-26 13:04:52 -0700 | [diff] [blame] | 255 | ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor)); |
| 256 | if (ret < 0) { |
| 257 | v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC); |
| 258 | v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor)); |
| 259 | v1_descriptor.header.fs_count = 3; |
| 260 | v1_descriptor.header.hs_count = 3; |
| 261 | v1_descriptor.fs_descs = fs_descriptors; |
| 262 | v1_descriptor.hs_descs = hs_descriptors; |
| 263 | D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno); |
| 264 | ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor)); |
| 265 | if (ret < 0) { |
| 266 | D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno); |
| 267 | goto err; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | ret = adb_write(h->control, &strings, sizeof(strings)); |
| 272 | if (ret < 0) { |
| 273 | D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno); |
| 274 | goto err; |
| 275 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR); |
| 279 | if (h->bulk_out < 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 280 | D("[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_ADB_OUT, errno); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 281 | goto err; |
| 282 | } |
| 283 | |
| 284 | h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR); |
| 285 | if (h->bulk_in < 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 286 | D("[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_ADB_IN, errno); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 287 | goto err; |
| 288 | } |
| 289 | |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 290 | return true; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 291 | |
| 292 | err: |
| 293 | if (h->bulk_in > 0) { |
| 294 | adb_close(h->bulk_in); |
| 295 | h->bulk_in = -1; |
| 296 | } |
| 297 | if (h->bulk_out > 0) { |
| 298 | adb_close(h->bulk_out); |
| 299 | h->bulk_out = -1; |
| 300 | } |
| 301 | if (h->control > 0) { |
| 302 | adb_close(h->control); |
| 303 | h->control = -1; |
| 304 | } |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 305 | return false; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Josh Gao | d9db09c | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 308 | static void usb_ffs_open_thread(void* x) { |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 309 | struct usb_handle* usb = (struct usb_handle*)x; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 310 | |
Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 311 | adb_thread_setname("usb ffs open"); |
| 312 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 313 | while (true) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 314 | // wait until the USB device needs opening |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 315 | std::unique_lock<std::mutex> lock(usb->lock); |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 316 | while (!usb->open_new_connection) { |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 317 | usb->notify.wait(lock); |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 318 | } |
| 319 | usb->open_new_connection = false; |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 320 | lock.unlock(); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 321 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 322 | while (true) { |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 323 | if (init_functionfs(usb)) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 324 | break; |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 325 | } |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 326 | std::this_thread::sleep_for(1s); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 327 | } |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 328 | android::base::SetProperty("sys.usb.ffs.ready", "1"); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 329 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 330 | D("[ usb_thread - registering device ]"); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 331 | register_usb_transport(usb, 0, 0, 1); |
| 332 | } |
| 333 | |
| 334 | // never gets here |
Josh Gao | d9db09c | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 335 | abort(); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 338 | 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] | 339 | D("about to write (fd=%d, len=%d)", h->bulk_in, len); |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 340 | |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 341 | const char* buf = static_cast<const char*>(data); |
| 342 | while (len > 0) { |
Josh Gao | ae72b5a | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 343 | int write_len = std::min(USB_FFS_MAX_WRITE, len); |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 344 | int n = adb_write(h->bulk_in, buf, write_len); |
| 345 | if (n < 0) { |
| 346 | D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno)); |
| 347 | return -1; |
| 348 | } |
| 349 | buf += n; |
| 350 | len -= n; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 351 | } |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 352 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 353 | D("[ done fd=%d ]", h->bulk_in); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 357 | static int usb_ffs_read(usb_handle* h, void* data, int len) { |
| 358 | D("about to read (fd=%d, len=%d)", h->bulk_out, len); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 359 | |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 360 | char* buf = static_cast<char*>(data); |
| 361 | while (len > 0) { |
Josh Gao | ae72b5a | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 362 | int read_len = std::min(USB_FFS_MAX_READ, len); |
Josh Gao | 0b19540 | 2015-12-16 11:00:08 -0800 | [diff] [blame] | 363 | int n = adb_read(h->bulk_out, buf, read_len); |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 364 | if (n < 0) { |
| 365 | 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] | 366 | return -1; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 367 | } |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 368 | buf += n; |
| 369 | len -= n; |
Elliott Hughes | 8fcd8bc | 2015-08-25 10:59:45 -0700 | [diff] [blame] | 370 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 371 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 372 | D("[ done fd=%d ]", h->bulk_out); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 373 | return 0; |
| 374 | } |
| 375 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 376 | static void usb_ffs_kick(usb_handle* h) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 377 | int err; |
| 378 | |
| 379 | err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT); |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 380 | if (err < 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 381 | 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] | 382 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 383 | |
| 384 | err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT); |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 385 | if (err < 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 386 | 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] | 387 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 388 | |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 389 | // don't close ep0 here, since we may not need to reinitialize it with |
| 390 | // the same descriptors again. if however ep1/ep2 fail to re-open in |
| 391 | // init_functionfs, only then would we close and open ep0 again. |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 392 | // Ditto the comment in usb_adb_kick. |
| 393 | h->kicked = true; |
| 394 | TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_out)); |
| 395 | TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_in)); |
| 396 | } |
| 397 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 398 | static void usb_ffs_close(usb_handle* h) { |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 399 | h->kicked = false; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 400 | adb_close(h->bulk_out); |
| 401 | adb_close(h->bulk_in); |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 402 | // Notify usb_adb_open_thread to open a new connection. |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 403 | h->lock.lock(); |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 404 | h->open_new_connection = true; |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 405 | h->lock.unlock(); |
| 406 | h->notify.notify_one(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 409 | static void usb_ffs_init() { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 410 | D("[ usb_init - using FunctionFS ]"); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 411 | |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 412 | usb_handle* h = new usb_handle(); |
Elliott Hughes | dc3b459 | 2015-04-21 19:39:52 -0700 | [diff] [blame] | 413 | |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 414 | h->write = usb_ffs_write; |
| 415 | h->read = usb_ffs_read; |
| 416 | h->kick = usb_ffs_kick; |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 417 | h->close = usb_ffs_close; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 418 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 419 | D("[ usb_init - starting thread ]"); |
Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 420 | if (!adb_thread_create(usb_ffs_open_thread, h)) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 421 | fatal_errno("[ cannot create usb thread ]\n"); |
| 422 | } |
| 423 | } |
| 424 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 425 | void usb_init() { |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 426 | dummy_fd = adb_open("/dev/null", O_WRONLY); |
| 427 | CHECK_NE(dummy_fd, -1); |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 428 | usb_ffs_init(); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 431 | int usb_write(usb_handle* h, const void* data, int len) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 432 | return h->write(h, data, len); |
| 433 | } |
| 434 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 435 | int usb_read(usb_handle* h, void* data, int len) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 436 | return h->read(h, data, len); |
| 437 | } |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 438 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 439 | int usb_close(usb_handle* h) { |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 440 | h->close(h); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 441 | return 0; |
| 442 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 443 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 444 | void usb_kick(usb_handle* h) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 445 | h->kick(h); |
| 446 | } |