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