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