The Android Open Source Project | 9ca14dc | 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 | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG USB |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | #include <errno.h> |
Dan Albert | b302d12 | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | |
Josh Gao | c95bb3d | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 32 | #include <algorithm> |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 33 | #include <atomic> |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 34 | #include <chrono> |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 35 | #include <condition_variable> |
| 36 | #include <mutex> |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 37 | #include <thread> |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 38 | |
| 39 | #include <android-base/logging.h> |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 40 | #include <android-base/properties.h> |
Josh Gao | c95bb3d | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 41 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | #include "adb.h" |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 43 | #include "transport.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 45 | using namespace std::chrono_literals; |
| 46 | |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 47 | #define MAX_PACKET_SIZE_FS 64 |
| 48 | #define MAX_PACKET_SIZE_HS 512 |
Zhuang Jin Can | 1fc58c5 | 2014-09-02 13:04:44 +0800 | [diff] [blame] | 49 | #define MAX_PACKET_SIZE_SS 1024 |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 50 | |
Josh Gao | c95bb3d | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 51 | // Writes larger than 16k fail on some devices (seed with 3.10.49-g209ea2f in particular). |
| 52 | #define USB_FFS_MAX_WRITE 16384 |
| 53 | |
| 54 | // The kernel allocates a contiguous buffer for reads, which can fail for large ones due to |
| 55 | // fragmentation. 16k chosen arbitrarily to match the write limit. |
| 56 | #define USB_FFS_MAX_READ 16384 |
| 57 | |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 58 | #define cpu_to_le16(x) htole16(x) |
| 59 | #define cpu_to_le32(x) htole32(x) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 61 | static int dummy_fd = -1; |
| 62 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 63 | struct usb_handle { |
| 64 | usb_handle() : kicked(false) { |
| 65 | } |
| 66 | |
| 67 | std::condition_variable notify; |
| 68 | std::mutex lock; |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 69 | std::atomic<bool> kicked; |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 70 | bool open_new_connection = true; |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 71 | |
| 72 | int (*write)(usb_handle *h, const void *data, int len); |
| 73 | int (*read)(usb_handle *h, void *data, int len); |
| 74 | void (*kick)(usb_handle *h); |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 75 | void (*close)(usb_handle *h); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 76 | |
| 77 | // Legacy f_adb |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 78 | int fd = -1; |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 79 | |
| 80 | // FunctionFS |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 81 | int control = -1; |
| 82 | int bulk_out = -1; /* "out" from the host's perspective => source for adbd */ |
| 83 | int bulk_in = -1; /* "in" from the host's perspective => sink for adbd */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
Badhri Jagan Sridharan | d6a6373 | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 86 | struct func_desc { |
| 87 | struct usb_interface_descriptor intf; |
| 88 | struct usb_endpoint_descriptor_no_audio source; |
| 89 | struct usb_endpoint_descriptor_no_audio sink; |
| 90 | } __attribute__((packed)); |
| 91 | |
Jack Pham | b64ab09 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 92 | struct ss_func_desc { |
| 93 | struct usb_interface_descriptor intf; |
| 94 | struct usb_endpoint_descriptor_no_audio source; |
| 95 | struct usb_ss_ep_comp_descriptor source_comp; |
| 96 | struct usb_endpoint_descriptor_no_audio sink; |
| 97 | struct usb_ss_ep_comp_descriptor sink_comp; |
| 98 | } __attribute__((packed)); |
| 99 | |
Badhri Jagan Sridharan | d6a6373 | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 100 | struct desc_v1 { |
| 101 | struct usb_functionfs_descs_head_v1 { |
| 102 | __le32 magic; |
| 103 | __le32 length; |
| 104 | __le32 fs_count; |
| 105 | __le32 hs_count; |
| 106 | } __attribute__((packed)) header; |
| 107 | struct func_desc fs_descs, hs_descs; |
| 108 | } __attribute__((packed)); |
| 109 | |
| 110 | struct desc_v2 { |
Christopher Ferris | f7555b1 | 2015-01-23 17:09:56 -0800 | [diff] [blame] | 111 | struct usb_functionfs_descs_head_v2 header; |
| 112 | // The rest of the structure depends on the flags in the header. |
| 113 | __le32 fs_count; |
| 114 | __le32 hs_count; |
Jack Pham | b64ab09 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 115 | __le32 ss_count; |
Badhri Jagan Sridharan | f1500ca | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 116 | __le32 os_count; |
Badhri Jagan Sridharan | d6a6373 | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 117 | struct func_desc fs_descs, hs_descs; |
Jack Pham | b64ab09 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 118 | struct ss_func_desc ss_descs; |
Badhri Jagan Sridharan | f1500ca | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 119 | struct usb_os_desc_header os_header; |
| 120 | struct usb_ext_compat_desc os_desc; |
Badhri Jagan Sridharan | d6a6373 | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 121 | } __attribute__((packed)); |
| 122 | |
Elliott Hughes | 712416a | 2015-05-05 18:26:10 -0700 | [diff] [blame] | 123 | static struct func_desc fs_descriptors = { |
Badhri Jagan Sridharan | d6a6373 | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 124 | .intf = { |
| 125 | .bLength = sizeof(fs_descriptors.intf), |
| 126 | .bDescriptorType = USB_DT_INTERFACE, |
| 127 | .bInterfaceNumber = 0, |
| 128 | .bNumEndpoints = 2, |
| 129 | .bInterfaceClass = ADB_CLASS, |
| 130 | .bInterfaceSubClass = ADB_SUBCLASS, |
| 131 | .bInterfaceProtocol = ADB_PROTOCOL, |
| 132 | .iInterface = 1, /* first string from the provided table */ |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 133 | }, |
Badhri Jagan Sridharan | d6a6373 | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 134 | .source = { |
| 135 | .bLength = sizeof(fs_descriptors.source), |
| 136 | .bDescriptorType = USB_DT_ENDPOINT, |
| 137 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 138 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 139 | .wMaxPacketSize = MAX_PACKET_SIZE_FS, |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 140 | }, |
Badhri Jagan Sridharan | d6a6373 | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 141 | .sink = { |
| 142 | .bLength = sizeof(fs_descriptors.sink), |
| 143 | .bDescriptorType = USB_DT_ENDPOINT, |
| 144 | .bEndpointAddress = 2 | USB_DIR_IN, |
| 145 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 146 | .wMaxPacketSize = MAX_PACKET_SIZE_FS, |
| 147 | }, |
| 148 | }; |
| 149 | |
Elliott Hughes | 712416a | 2015-05-05 18:26:10 -0700 | [diff] [blame] | 150 | static struct func_desc hs_descriptors = { |
Badhri Jagan Sridharan | d6a6373 | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 151 | .intf = { |
| 152 | .bLength = sizeof(hs_descriptors.intf), |
| 153 | .bDescriptorType = USB_DT_INTERFACE, |
| 154 | .bInterfaceNumber = 0, |
| 155 | .bNumEndpoints = 2, |
| 156 | .bInterfaceClass = ADB_CLASS, |
| 157 | .bInterfaceSubClass = ADB_SUBCLASS, |
| 158 | .bInterfaceProtocol = ADB_PROTOCOL, |
| 159 | .iInterface = 1, /* first string from the provided table */ |
| 160 | }, |
| 161 | .source = { |
| 162 | .bLength = sizeof(hs_descriptors.source), |
| 163 | .bDescriptorType = USB_DT_ENDPOINT, |
| 164 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 165 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 166 | .wMaxPacketSize = MAX_PACKET_SIZE_HS, |
| 167 | }, |
| 168 | .sink = { |
| 169 | .bLength = sizeof(hs_descriptors.sink), |
| 170 | .bDescriptorType = USB_DT_ENDPOINT, |
| 171 | .bEndpointAddress = 2 | USB_DIR_IN, |
| 172 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 173 | .wMaxPacketSize = MAX_PACKET_SIZE_HS, |
Zhuang Jin Can | 1fc58c5 | 2014-09-02 13:04:44 +0800 | [diff] [blame] | 174 | }, |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 175 | }; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 176 | |
Jack Pham | b64ab09 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 177 | static struct ss_func_desc ss_descriptors = { |
| 178 | .intf = { |
| 179 | .bLength = sizeof(ss_descriptors.intf), |
| 180 | .bDescriptorType = USB_DT_INTERFACE, |
| 181 | .bInterfaceNumber = 0, |
| 182 | .bNumEndpoints = 2, |
| 183 | .bInterfaceClass = ADB_CLASS, |
| 184 | .bInterfaceSubClass = ADB_SUBCLASS, |
| 185 | .bInterfaceProtocol = ADB_PROTOCOL, |
| 186 | .iInterface = 1, /* first string from the provided table */ |
| 187 | }, |
| 188 | .source = { |
| 189 | .bLength = sizeof(ss_descriptors.source), |
| 190 | .bDescriptorType = USB_DT_ENDPOINT, |
| 191 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 192 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 193 | .wMaxPacketSize = MAX_PACKET_SIZE_SS, |
| 194 | }, |
| 195 | .source_comp = { |
| 196 | .bLength = sizeof(ss_descriptors.source_comp), |
| 197 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 198 | }, |
| 199 | .sink = { |
| 200 | .bLength = sizeof(ss_descriptors.sink), |
| 201 | .bDescriptorType = USB_DT_ENDPOINT, |
| 202 | .bEndpointAddress = 2 | USB_DIR_IN, |
| 203 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 204 | .wMaxPacketSize = MAX_PACKET_SIZE_SS, |
| 205 | }, |
| 206 | .sink_comp = { |
| 207 | .bLength = sizeof(ss_descriptors.sink_comp), |
| 208 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 209 | }, |
| 210 | }; |
| 211 | |
Badhri Jagan Sridharan | f1500ca | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 212 | struct usb_ext_compat_desc os_desc_compat = { |
| 213 | .bFirstInterfaceNumber = 0, |
| 214 | .Reserved1 = cpu_to_le32(1), |
| 215 | .CompatibleID = {0}, |
| 216 | .SubCompatibleID = {0}, |
| 217 | .Reserved2 = {0}, |
| 218 | }; |
| 219 | |
| 220 | static struct usb_os_desc_header os_desc_header = { |
| 221 | .interface = cpu_to_le32(1), |
| 222 | .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)), |
| 223 | .bcdVersion = cpu_to_le32(1), |
| 224 | .wIndex = cpu_to_le32(4), |
| 225 | .bCount = cpu_to_le32(1), |
| 226 | .Reserved = cpu_to_le32(0), |
| 227 | }; |
| 228 | |
| 229 | |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 230 | #define STR_INTERFACE_ "ADB Interface" |
| 231 | |
| 232 | static const struct { |
| 233 | struct usb_functionfs_strings_head header; |
| 234 | struct { |
| 235 | __le16 code; |
| 236 | const char str1[sizeof(STR_INTERFACE_)]; |
| 237 | } __attribute__((packed)) lang0; |
| 238 | } __attribute__((packed)) strings = { |
| 239 | .header = { |
| 240 | .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC), |
| 241 | .length = cpu_to_le32(sizeof(strings)), |
| 242 | .str_count = cpu_to_le32(1), |
| 243 | .lang_count = cpu_to_le32(1), |
| 244 | }, |
| 245 | .lang0 = { |
| 246 | cpu_to_le16(0x0409), /* en-us */ |
| 247 | STR_INTERFACE_, |
| 248 | }, |
| 249 | }; |
| 250 | |
Josh Gao | 382ea6e | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 251 | static void usb_adb_open_thread(void* x) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 252 | struct usb_handle *usb = (struct usb_handle *)x; |
| 253 | int fd; |
| 254 | |
Siva Velusamy | 2669cf9 | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 255 | adb_thread_setname("usb open"); |
| 256 | |
Elliott Hughes | 7cf3575 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 257 | while (true) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 | // wait until the USB device needs opening |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 259 | std::unique_lock<std::mutex> lock(usb->lock); |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 260 | while (!usb->open_new_connection) { |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 261 | usb->notify.wait(lock); |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 262 | } |
| 263 | usb->open_new_connection = false; |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 264 | lock.unlock(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 265 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 266 | D("[ usb_thread - opening device ]"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | do { |
| 268 | /* XXX use inotify? */ |
| 269 | fd = unix_open("/dev/android_adb", O_RDWR); |
| 270 | if (fd < 0) { |
| 271 | // to support older kernels |
| 272 | fd = unix_open("/dev/android", O_RDWR); |
| 273 | } |
| 274 | if (fd < 0) { |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 275 | std::this_thread::sleep_for(1s); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 276 | } |
| 277 | } while (fd < 0); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 278 | D("[ opening device succeeded ]"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 279 | |
| 280 | close_on_exec(fd); |
| 281 | usb->fd = fd; |
| 282 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 283 | D("[ usb_thread - registering device ]"); |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 284 | register_usb_transport(usb, 0, 0, 1); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // never gets here |
Josh Gao | 382ea6e | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 288 | abort(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 291 | static int usb_adb_write(usb_handle *h, const void *data, int len) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 292 | { |
| 293 | int n; |
| 294 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 295 | D("about to write (fd=%d, len=%d)", h->fd, len); |
Spencer Low | 3a2421b | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 296 | n = unix_write(h->fd, data, len); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 297 | if(n != len) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 298 | D("ERROR: fd = %d, n = %d, errno = %d (%s)", |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 299 | h->fd, n, errno, strerror(errno)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 300 | return -1; |
| 301 | } |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 302 | if (h->kicked) { |
| 303 | D("usb_adb_write finished due to kicked"); |
| 304 | return -1; |
| 305 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 306 | D("[ done fd=%d ]", h->fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 310 | static int usb_adb_read(usb_handle *h, void *data, int len) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 311 | { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 312 | D("about to read (fd=%d, len=%d)", h->fd, len); |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 313 | while (len > 0) { |
| 314 | // The kernel implementation of adb_read in f_adb.c doesn't support |
| 315 | // reads larger then 4096 bytes. Read the data in 4096 byte chunks to |
| 316 | // avoid the issue. (The ffs implementation doesn't have this limit.) |
| 317 | int bytes_to_read = len < 4096 ? len : 4096; |
| 318 | int n = unix_read(h->fd, data, bytes_to_read); |
| 319 | if (n != bytes_to_read) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 320 | D("ERROR: fd = %d, n = %d, errno = %d (%s)", |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 321 | h->fd, n, errno, strerror(errno)); |
| 322 | return -1; |
| 323 | } |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 324 | if (h->kicked) { |
| 325 | D("usb_adb_read finished due to kicked"); |
| 326 | return -1; |
| 327 | } |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 328 | len -= n; |
| 329 | data = ((char*)data) + n; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 330 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 331 | D("[ done fd=%d ]", h->fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 335 | static void usb_adb_kick(usb_handle *h) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 336 | D("usb_kick"); |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 337 | // Other threads may be calling usb_adb_read/usb_adb_write at the same time. |
| 338 | // If we close h->fd, the file descriptor will be reused to open other files, |
| 339 | // and the read/write thread may operate on the wrong file. So instead |
| 340 | // we set the kicked flag and reopen h->fd to a dummy file here. After read/write |
| 341 | // threads finish, we close h->fd in usb_adb_close(). |
| 342 | h->kicked = true; |
| 343 | TEMP_FAILURE_RETRY(dup2(dummy_fd, h->fd)); |
| 344 | } |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 345 | |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 346 | static void usb_adb_close(usb_handle *h) { |
| 347 | h->kicked = false; |
| 348 | adb_close(h->fd); |
| 349 | // Notify usb_adb_open_thread to open a new connection. |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 350 | h->lock.lock(); |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 351 | h->open_new_connection = true; |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 352 | h->lock.unlock(); |
| 353 | h->notify.notify_one(); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static void usb_adb_init() |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 357 | { |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 358 | usb_handle* h = new usb_handle(); |
Elliott Hughes | d0269c9 | 2015-04-21 19:39:52 -0700 | [diff] [blame] | 359 | |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 360 | h->write = usb_adb_write; |
| 361 | h->read = usb_adb_read; |
| 362 | h->kick = usb_adb_kick; |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 363 | h->close = usb_adb_close; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 364 | |
Elliott Hughes | 392692c | 2015-04-16 14:12:50 -0700 | [diff] [blame] | 365 | // Open the file /dev/android_adb_enable to trigger |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 366 | // the enabling of the adb USB function in the kernel. |
| 367 | // We never touch this file again - just leave it open |
| 368 | // indefinitely so the kernel will know when we are running |
| 369 | // and when we are not. |
Elliott Hughes | 392692c | 2015-04-16 14:12:50 -0700 | [diff] [blame] | 370 | int fd = unix_open("/dev/android_adb_enable", O_RDWR); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 371 | if (fd < 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 372 | D("failed to open /dev/android_adb_enable"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 373 | } else { |
| 374 | close_on_exec(fd); |
| 375 | } |
| 376 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 377 | D("[ usb_init - starting thread ]"); |
Elliott Hughes | f251714 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 378 | if (!adb_thread_create(usb_adb_open_thread, h)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 | fatal_errno("cannot create usb thread"); |
| 380 | } |
| 381 | } |
| 382 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 383 | |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 384 | static bool init_functionfs(struct usb_handle *h) |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 385 | { |
| 386 | ssize_t ret; |
Badhri Jagan Sridharan | d6a6373 | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 387 | struct desc_v1 v1_descriptor; |
| 388 | struct desc_v2 v2_descriptor; |
| 389 | |
| 390 | v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2); |
| 391 | v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor)); |
Jack Pham | b64ab09 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 392 | v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC | |
Badhri Jagan Sridharan | f1500ca | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 393 | FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC; |
Christopher Ferris | f7555b1 | 2015-01-23 17:09:56 -0800 | [diff] [blame] | 394 | v2_descriptor.fs_count = 3; |
| 395 | v2_descriptor.hs_count = 3; |
Jack Pham | b64ab09 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 396 | v2_descriptor.ss_count = 5; |
Badhri Jagan Sridharan | f1500ca | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 397 | v2_descriptor.os_count = 1; |
Badhri Jagan Sridharan | d6a6373 | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 398 | v2_descriptor.fs_descs = fs_descriptors; |
| 399 | v2_descriptor.hs_descs = hs_descriptors; |
Jack Pham | b64ab09 | 2015-06-02 10:36:43 -0700 | [diff] [blame] | 400 | v2_descriptor.ss_descs = ss_descriptors; |
Badhri Jagan Sridharan | f1500ca | 2015-10-05 13:04:03 -0700 | [diff] [blame] | 401 | v2_descriptor.os_header = os_desc_header; |
| 402 | v2_descriptor.os_desc = os_desc_compat; |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 403 | |
Siqi Lin | 686558e | 2016-05-26 13:04:52 -0700 | [diff] [blame] | 404 | if (h->control < 0) { // might have already done this before |
| 405 | D("OPENING %s", USB_FFS_ADB_EP0); |
| 406 | h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR); |
| 407 | if (h->control < 0) { |
| 408 | D("[ %s: cannot open control endpoint: errno=%d]", USB_FFS_ADB_EP0, errno); |
Jack Pham | 6c3cef5 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 409 | goto err; |
| 410 | } |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 411 | |
Siqi Lin | 686558e | 2016-05-26 13:04:52 -0700 | [diff] [blame] | 412 | ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor)); |
| 413 | if (ret < 0) { |
| 414 | v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC); |
| 415 | v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor)); |
| 416 | v1_descriptor.header.fs_count = 3; |
| 417 | v1_descriptor.header.hs_count = 3; |
| 418 | v1_descriptor.fs_descs = fs_descriptors; |
| 419 | v1_descriptor.hs_descs = hs_descriptors; |
| 420 | D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno); |
| 421 | ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor)); |
| 422 | if (ret < 0) { |
| 423 | D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno); |
| 424 | goto err; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | ret = adb_write(h->control, &strings, sizeof(strings)); |
| 429 | if (ret < 0) { |
| 430 | D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno); |
| 431 | goto err; |
| 432 | } |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR); |
| 436 | if (h->bulk_out < 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 437 | D("[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_ADB_OUT, errno); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 438 | goto err; |
| 439 | } |
| 440 | |
| 441 | h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR); |
| 442 | if (h->bulk_in < 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 443 | D("[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_ADB_IN, errno); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 444 | goto err; |
| 445 | } |
| 446 | |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 447 | return true; |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 448 | |
| 449 | err: |
| 450 | if (h->bulk_in > 0) { |
| 451 | adb_close(h->bulk_in); |
| 452 | h->bulk_in = -1; |
| 453 | } |
| 454 | if (h->bulk_out > 0) { |
| 455 | adb_close(h->bulk_out); |
| 456 | h->bulk_out = -1; |
| 457 | } |
| 458 | if (h->control > 0) { |
| 459 | adb_close(h->control); |
| 460 | h->control = -1; |
| 461 | } |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 462 | return false; |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Josh Gao | 382ea6e | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 465 | static void usb_ffs_open_thread(void* x) { |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 466 | struct usb_handle *usb = (struct usb_handle *)x; |
| 467 | |
Siva Velusamy | 2669cf9 | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 468 | adb_thread_setname("usb ffs open"); |
| 469 | |
Elliott Hughes | 7cf3575 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 470 | while (true) { |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 471 | // wait until the USB device needs opening |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 472 | std::unique_lock<std::mutex> lock(usb->lock); |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 473 | while (!usb->open_new_connection) { |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 474 | usb->notify.wait(lock); |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 475 | } |
| 476 | usb->open_new_connection = false; |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 477 | lock.unlock(); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 478 | |
Elliott Hughes | 7cf3575 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 479 | while (true) { |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 480 | if (init_functionfs(usb)) { |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 481 | break; |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 482 | } |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 483 | std::this_thread::sleep_for(1s); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 484 | } |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 485 | android::base::SetProperty("sys.usb.ffs.ready", "1"); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 486 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 487 | D("[ usb_thread - registering device ]"); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 488 | register_usb_transport(usb, 0, 0, 1); |
| 489 | } |
| 490 | |
| 491 | // never gets here |
Josh Gao | 382ea6e | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 492 | abort(); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 493 | } |
| 494 | |
Josh Gao | 8e70125 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 495 | static int usb_ffs_write(usb_handle* h, const void* data, int len) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 496 | D("about to write (fd=%d, len=%d)", h->bulk_in, len); |
Josh Gao | 8e70125 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 497 | |
Josh Gao | 8e70125 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 498 | const char* buf = static_cast<const char*>(data); |
| 499 | while (len > 0) { |
Josh Gao | c95bb3d | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 500 | int write_len = std::min(USB_FFS_MAX_WRITE, len); |
Josh Gao | 8e70125 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 501 | int n = adb_write(h->bulk_in, buf, write_len); |
| 502 | if (n < 0) { |
| 503 | D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno)); |
| 504 | return -1; |
| 505 | } |
| 506 | buf += n; |
| 507 | len -= n; |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 508 | } |
Josh Gao | 8e70125 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 509 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 510 | D("[ done fd=%d ]", h->bulk_in); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 511 | return 0; |
| 512 | } |
| 513 | |
Josh Gao | 8e70125 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 514 | static int usb_ffs_read(usb_handle* h, void* data, int len) { |
| 515 | D("about to read (fd=%d, len=%d)", h->bulk_out, len); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 516 | |
Josh Gao | 8e70125 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 517 | char* buf = static_cast<char*>(data); |
| 518 | while (len > 0) { |
Josh Gao | c95bb3d | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 519 | int read_len = std::min(USB_FFS_MAX_READ, len); |
Josh Gao | 83e329e | 2015-12-16 11:00:08 -0800 | [diff] [blame] | 520 | int n = adb_read(h->bulk_out, buf, read_len); |
Josh Gao | 8e70125 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 521 | if (n < 0) { |
| 522 | D("ERROR: fd = %d, n = %d: %s", h->bulk_out, n, strerror(errno)); |
Elliott Hughes | 0bd8587 | 2015-08-25 10:59:45 -0700 | [diff] [blame] | 523 | return -1; |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 524 | } |
Josh Gao | 8e70125 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 525 | buf += n; |
| 526 | len -= n; |
Elliott Hughes | 0bd8587 | 2015-08-25 10:59:45 -0700 | [diff] [blame] | 527 | } |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 528 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 529 | D("[ done fd=%d ]", h->bulk_out); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static void usb_ffs_kick(usb_handle *h) |
| 534 | { |
| 535 | int err; |
| 536 | |
| 537 | err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT); |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 538 | if (err < 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 539 | D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno); |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 540 | } |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 541 | |
| 542 | err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT); |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 543 | if (err < 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 544 | D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno); |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 545 | } |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 546 | |
Jack Pham | 6c3cef5 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 547 | // don't close ep0 here, since we may not need to reinitialize it with |
| 548 | // the same descriptors again. if however ep1/ep2 fail to re-open in |
| 549 | // init_functionfs, only then would we close and open ep0 again. |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 550 | // Ditto the comment in usb_adb_kick. |
| 551 | h->kicked = true; |
| 552 | TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_out)); |
| 553 | TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_in)); |
| 554 | } |
| 555 | |
| 556 | static void usb_ffs_close(usb_handle *h) { |
| 557 | h->kicked = false; |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 558 | adb_close(h->bulk_out); |
| 559 | adb_close(h->bulk_in); |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 560 | // Notify usb_adb_open_thread to open a new connection. |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 561 | h->lock.lock(); |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 562 | h->open_new_connection = true; |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 563 | h->lock.unlock(); |
| 564 | h->notify.notify_one(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 567 | static void usb_ffs_init() |
| 568 | { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 569 | D("[ usb_init - using FunctionFS ]"); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 570 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 571 | usb_handle* h = new usb_handle(); |
Elliott Hughes | d0269c9 | 2015-04-21 19:39:52 -0700 | [diff] [blame] | 572 | |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 573 | h->write = usb_ffs_write; |
| 574 | h->read = usb_ffs_read; |
| 575 | h->kick = usb_ffs_kick; |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 576 | h->close = usb_ffs_close; |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 577 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 578 | D("[ usb_init - starting thread ]"); |
Elliott Hughes | f251714 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 579 | if (!adb_thread_create(usb_ffs_open_thread, h)) { |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 580 | fatal_errno("[ cannot create usb thread ]\n"); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | void usb_init() |
| 585 | { |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 586 | dummy_fd = adb_open("/dev/null", O_WRONLY); |
| 587 | CHECK_NE(dummy_fd, -1); |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 588 | if (access(USB_FFS_ADB_EP0, F_OK) == 0) |
| 589 | usb_ffs_init(); |
| 590 | else |
| 591 | usb_adb_init(); |
| 592 | } |
| 593 | |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 594 | int usb_write(usb_handle *h, const void *data, int len) |
| 595 | { |
| 596 | return h->write(h, data, len); |
| 597 | } |
| 598 | |
| 599 | int usb_read(usb_handle *h, void *data, int len) |
| 600 | { |
| 601 | return h->read(h, data, len); |
| 602 | } |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 603 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 604 | int usb_close(usb_handle *h) |
| 605 | { |
Yabin Cui | 6760b2a | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 606 | h->close(h); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 607 | return 0; |
| 608 | } |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 609 | |
| 610 | void usb_kick(usb_handle *h) |
| 611 | { |
| 612 | h->kick(h); |
| 613 | } |