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