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