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