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 | |
Dan Albert | 3313426 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | #include <errno.h> |
Dan Albert | 7664901 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | #include "adb.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 33 | #include "transport.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | |
Andrzej Pietrasiewicz | fd96db1 | 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 | d6ee9f2 | 2014-09-02 13:04:44 +0800 | [diff] [blame] | 37 | #define MAX_PACKET_SIZE_SS 1024 |
Andrzej Pietrasiewicz | fd96db1 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | |
| 42 | struct usb_handle |
| 43 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | adb_cond_t notify; |
| 45 | adb_mutex_t lock; |
Andrzej Pietrasiewicz | fd96db1 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
Badhri Jagan Sridharan | ab3446d | 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 | c49f51c | 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 | ab3446d | 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 | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 94 | }, |
Badhri Jagan Sridharan | ab3446d | 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 | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 101 | }, |
Badhri Jagan Sridharan | ab3446d | 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 | d6ee9f2 | 2014-09-02 13:04:44 +0800 | [diff] [blame] | 135 | }, |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 136 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | |
Andrzej Pietrasiewicz | fd96db1 | 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 | dd7bc33 | 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 | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 191 | register_usb_transport(usb, 0, 0, 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // never gets here |
| 195 | return 0; |
| 196 | } |
| 197 | |
Andrzej Pietrasiewicz | fd96db1 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | { |
| 200 | int n; |
| 201 | |
JP Abgrall | 408fa57 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 203 | n = adb_write(h->fd, data, len); |
| 204 | if(n != len) { |
JP Abgrall | 408fa57 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 207 | return -1; |
| 208 | } |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 209 | D("[ done fd=%d ]\n", h->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Andrzej Pietrasiewicz | fd96db1 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | { |
| 215 | int n; |
| 216 | |
JP Abgrall | 408fa57 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 218 | n = adb_read(h->fd, data, len); |
| 219 | if(n != len) { |
JP Abgrall | 408fa57 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 222 | return -1; |
| 223 | } |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 224 | D("[ done fd=%d ]\n", h->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Andrzej Pietrasiewicz | fd96db1 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 241 | { |
| 242 | usb_handle *h; |
| 243 | adb_thread_t tid; |
| 244 | int fd; |
| 245 | |
| 246 | h = calloc(1, sizeof(usb_handle)); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 247 | |
| 248 | h->write = usb_adb_write; |
| 249 | h->read = usb_adb_read; |
| 250 | h->kick = usb_adb_kick; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 251 | h->fd = -1; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 252 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 253 | adb_cond_init(&h->notify, 0); |
| 254 | adb_mutex_init(&h->lock, 0); |
| 255 | |
| 256 | // Open the file /dev/android_adb_enable to trigger |
| 257 | // the enabling of the adb USB function in the kernel. |
| 258 | // We never touch this file again - just leave it open |
| 259 | // indefinitely so the kernel will know when we are running |
| 260 | // and when we are not. |
| 261 | fd = unix_open("/dev/android_adb_enable", O_RDWR); |
| 262 | if (fd < 0) { |
| 263 | D("failed to open /dev/android_adb_enable\n"); |
| 264 | } else { |
| 265 | close_on_exec(fd); |
| 266 | } |
| 267 | |
| 268 | D("[ usb_init - starting thread ]\n"); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 269 | if(adb_thread_create(&tid, usb_adb_open_thread, h)){ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 270 | fatal_errno("cannot create usb thread"); |
| 271 | } |
| 272 | } |
| 273 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 274 | |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 275 | static void init_functionfs(struct usb_handle *h) |
| 276 | { |
| 277 | ssize_t ret; |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 278 | struct desc_v1 v1_descriptor; |
| 279 | struct desc_v2 v2_descriptor; |
| 280 | |
| 281 | v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2); |
| 282 | v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor)); |
| 283 | v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC; |
Christopher Ferris | c49f51c | 2015-01-23 17:09:56 -0800 | [diff] [blame] | 284 | v2_descriptor.fs_count = 3; |
| 285 | v2_descriptor.hs_count = 3; |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 286 | v2_descriptor.fs_descs = fs_descriptors; |
| 287 | v2_descriptor.hs_descs = hs_descriptors; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 288 | |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 289 | if (h->control < 0) { // might have already done this before |
| 290 | D("OPENING %s\n", USB_FFS_ADB_EP0); |
| 291 | h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR); |
| 292 | if (h->control < 0) { |
| 293 | D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno); |
| 294 | goto err; |
| 295 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 296 | |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 297 | ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor)); |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 298 | if (ret < 0) { |
Badhri Jagan Sridharan | ab3446d | 2014-10-27 18:26:17 -0700 | [diff] [blame] | 299 | v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC); |
| 300 | v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor)); |
| 301 | v1_descriptor.header.fs_count = 3; |
| 302 | v1_descriptor.header.hs_count = 3; |
| 303 | v1_descriptor.fs_descs = fs_descriptors; |
| 304 | v1_descriptor.hs_descs = hs_descriptors; |
| 305 | D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno); |
| 306 | ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor)); |
| 307 | if (ret < 0) { |
| 308 | D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno); |
| 309 | goto err; |
| 310 | } |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 311 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 312 | |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 313 | ret = adb_write(h->control, &strings, sizeof(strings)); |
| 314 | if (ret < 0) { |
| 315 | D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno); |
| 316 | goto err; |
| 317 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR); |
| 321 | if (h->bulk_out < 0) { |
| 322 | D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno); |
| 323 | goto err; |
| 324 | } |
| 325 | |
| 326 | h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR); |
| 327 | if (h->bulk_in < 0) { |
| 328 | D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno); |
| 329 | goto err; |
| 330 | } |
| 331 | |
| 332 | return; |
| 333 | |
| 334 | err: |
| 335 | if (h->bulk_in > 0) { |
| 336 | adb_close(h->bulk_in); |
| 337 | h->bulk_in = -1; |
| 338 | } |
| 339 | if (h->bulk_out > 0) { |
| 340 | adb_close(h->bulk_out); |
| 341 | h->bulk_out = -1; |
| 342 | } |
| 343 | if (h->control > 0) { |
| 344 | adb_close(h->control); |
| 345 | h->control = -1; |
| 346 | } |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | static void *usb_ffs_open_thread(void *x) |
| 351 | { |
| 352 | struct usb_handle *usb = (struct usb_handle *)x; |
| 353 | |
| 354 | while (1) { |
| 355 | // wait until the USB device needs opening |
| 356 | adb_mutex_lock(&usb->lock); |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 357 | while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1) |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 358 | adb_cond_wait(&usb->notify, &usb->lock); |
| 359 | adb_mutex_unlock(&usb->lock); |
| 360 | |
| 361 | while (1) { |
| 362 | init_functionfs(usb); |
| 363 | |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 364 | if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0) |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 365 | break; |
| 366 | |
| 367 | adb_sleep_ms(1000); |
| 368 | } |
| 369 | |
| 370 | D("[ usb_thread - registering device ]\n"); |
| 371 | register_usb_transport(usb, 0, 0, 1); |
| 372 | } |
| 373 | |
| 374 | // never gets here |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static int bulk_write(int bulk_in, const char *buf, size_t length) |
| 379 | { |
| 380 | size_t count = 0; |
| 381 | int ret; |
| 382 | |
| 383 | do { |
| 384 | ret = adb_write(bulk_in, buf + count, length - count); |
| 385 | if (ret < 0) { |
| 386 | if (errno != EINTR) |
| 387 | return ret; |
| 388 | } else { |
| 389 | count += ret; |
| 390 | } |
| 391 | } while (count < length); |
| 392 | |
| 393 | D("[ bulk_write done fd=%d ]\n", bulk_in); |
| 394 | return count; |
| 395 | } |
| 396 | |
| 397 | static int usb_ffs_write(usb_handle *h, const void *data, int len) |
| 398 | { |
| 399 | int n; |
| 400 | |
| 401 | D("about to write (fd=%d, len=%d)\n", h->bulk_in, len); |
| 402 | n = bulk_write(h->bulk_in, data, len); |
| 403 | if (n != len) { |
| 404 | D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", |
| 405 | h->bulk_in, n, errno, strerror(errno)); |
| 406 | return -1; |
| 407 | } |
| 408 | D("[ done fd=%d ]\n", h->bulk_in); |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static int bulk_read(int bulk_out, char *buf, size_t length) |
| 413 | { |
| 414 | size_t count = 0; |
| 415 | int ret; |
| 416 | |
| 417 | do { |
| 418 | ret = adb_read(bulk_out, buf + count, length - count); |
| 419 | if (ret < 0) { |
| 420 | if (errno != EINTR) { |
Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 421 | D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n", |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 422 | bulk_out, length, count); |
| 423 | return ret; |
| 424 | } |
| 425 | } else { |
| 426 | count += ret; |
| 427 | } |
| 428 | } while (count < length); |
| 429 | |
| 430 | return count; |
| 431 | } |
| 432 | |
| 433 | static int usb_ffs_read(usb_handle *h, void *data, int len) |
| 434 | { |
| 435 | int n; |
| 436 | |
| 437 | D("about to read (fd=%d, len=%d)\n", h->bulk_out, len); |
| 438 | n = bulk_read(h->bulk_out, data, len); |
| 439 | if (n != len) { |
| 440 | D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", |
| 441 | h->bulk_out, n, errno, strerror(errno)); |
| 442 | return -1; |
| 443 | } |
| 444 | D("[ done fd=%d ]\n", h->bulk_out); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static void usb_ffs_kick(usb_handle *h) |
| 449 | { |
| 450 | int err; |
| 451 | |
| 452 | err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT); |
| 453 | if (err < 0) |
| 454 | D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno); |
| 455 | |
| 456 | err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT); |
| 457 | if (err < 0) |
| 458 | D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno); |
| 459 | |
| 460 | adb_mutex_lock(&h->lock); |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 461 | |
| 462 | // don't close ep0 here, since we may not need to reinitialize it with |
| 463 | // the same descriptors again. if however ep1/ep2 fail to re-open in |
| 464 | // init_functionfs, only then would we close and open ep0 again. |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 465 | adb_close(h->bulk_out); |
| 466 | adb_close(h->bulk_in); |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 467 | h->bulk_out = h->bulk_in = -1; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 468 | |
| 469 | // notify usb_ffs_open_thread that we are disconnected |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 470 | adb_cond_signal(&h->notify); |
| 471 | adb_mutex_unlock(&h->lock); |
| 472 | } |
| 473 | |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 474 | static void usb_ffs_init() |
| 475 | { |
| 476 | usb_handle *h; |
| 477 | adb_thread_t tid; |
| 478 | |
| 479 | D("[ usb_init - using FunctionFS ]\n"); |
| 480 | |
| 481 | h = calloc(1, sizeof(usb_handle)); |
| 482 | |
| 483 | h->write = usb_ffs_write; |
| 484 | h->read = usb_ffs_read; |
| 485 | h->kick = usb_ffs_kick; |
| 486 | |
| 487 | h->control = -1; |
| 488 | h->bulk_out = -1; |
| 489 | h->bulk_out = -1; |
| 490 | |
| 491 | adb_cond_init(&h->notify, 0); |
| 492 | adb_mutex_init(&h->lock, 0); |
| 493 | |
| 494 | D("[ usb_init - starting thread ]\n"); |
| 495 | if (adb_thread_create(&tid, usb_ffs_open_thread, h)){ |
| 496 | fatal_errno("[ cannot create usb thread ]\n"); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | void usb_init() |
| 501 | { |
| 502 | if (access(USB_FFS_ADB_EP0, F_OK) == 0) |
| 503 | usb_ffs_init(); |
| 504 | else |
| 505 | usb_adb_init(); |
| 506 | } |
| 507 | |
| 508 | void usb_cleanup() |
| 509 | { |
| 510 | } |
| 511 | |
| 512 | int usb_write(usb_handle *h, const void *data, int len) |
| 513 | { |
| 514 | return h->write(h, data, len); |
| 515 | } |
| 516 | |
| 517 | int usb_read(usb_handle *h, void *data, int len) |
| 518 | { |
| 519 | return h->read(h, data, len); |
| 520 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 521 | int usb_close(usb_handle *h) |
| 522 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 523 | return 0; |
| 524 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 525 | |
| 526 | void usb_kick(usb_handle *h) |
| 527 | { |
| 528 | h->kick(h); |
| 529 | } |