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