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