xj | 123f7cc | 2022-06-06 11:35:21 +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 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include <linux/usb/ch9.h> |
| 23 | #include <linux/usb/functionfs.h> |
| 24 | #include <sys/ioctl.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <dirent.h> |
| 27 | #include <errno.h> |
| 28 | #include <stddef.h> |
| 29 | #include <sys/epoll.h> |
| 30 | |
| 31 | #include "sysdeps.h" |
| 32 | |
| 33 | #define TRACE_TAG TRACE_USB |
| 34 | #include "adb.h" |
| 35 | |
| 36 | #define MAX_PACKET_SIZE_FS 64 |
| 37 | #define MAX_PACKET_SIZE_HS 512 |
| 38 | #define MAX_PACKET_SIZE_SS 1024 |
| 39 | |
| 40 | //#define cpu_to_le16(x) htole16(x) |
| 41 | //#define cpu_to_le32(x) htole32(x) |
| 42 | /* |
| 43 | * cpu_to_le16/32 are used when initializing structures, a context where a |
| 44 | * function call is not allowed. To solve this, we code cpu_to_le16/32 in a way |
| 45 | * that allows them to be used when initializing structures. |
| 46 | */ |
| 47 | |
| 48 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 49 | #define cpu_to_le16(x) (x) |
| 50 | #define cpu_to_le32(x) (x) |
| 51 | #else |
| 52 | #define cpu_to_le16(x) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)) |
| 53 | #define cpu_to_le32(x) \ |
| 54 | ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \ |
| 55 | (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24)) |
| 56 | #endif |
| 57 | |
| 58 | #define FFS_CONTOL_MAX_EPOLL_EVENT 50 |
| 59 | #define DEBUG 0 |
| 60 | |
| 61 | struct usb_handle |
| 62 | { |
| 63 | adb_cond_t notify; |
| 64 | adb_mutex_t lock; |
| 65 | |
| 66 | int (*write)(usb_handle *h, const void *data, int len); |
| 67 | int (*read)(usb_handle *h, void *data, int len); |
| 68 | void (*kick)(usb_handle *h); |
| 69 | |
| 70 | // Legacy f_adb |
| 71 | int fd; |
| 72 | |
| 73 | // FunctionFS |
| 74 | int control; |
| 75 | int bulk_out; /* "out" from the host's perspective => source for adbd */ |
| 76 | int bulk_in; /* "in" from the host's perspective => sink for adbd */ |
| 77 | bool ffs_control_thread_created; |
| 78 | }; |
| 79 | |
| 80 | static const struct { |
| 81 | __le32 magic; |
| 82 | __le32 length; |
| 83 | __le32 flags; |
| 84 | __le32 fs_count; |
| 85 | __le32 hs_count; |
| 86 | __le32 ss_count; |
| 87 | struct { |
| 88 | struct usb_interface_descriptor intf; |
| 89 | struct usb_endpoint_descriptor_no_audio source; |
| 90 | struct usb_endpoint_descriptor_no_audio sink; |
| 91 | } __attribute__((packed)) fs_descs, hs_descs; |
| 92 | struct { |
| 93 | struct usb_interface_descriptor intf; |
| 94 | struct usb_endpoint_descriptor_no_audio source; |
| 95 | struct usb_ss_ep_comp_descriptor source_comp; |
| 96 | struct usb_endpoint_descriptor_no_audio sink; |
| 97 | struct usb_ss_ep_comp_descriptor sink_comp; |
| 98 | } __attribute__((packed)) ss_descs; |
| 99 | } __attribute__((packed)) descriptors = { |
| 100 | .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2), |
| 101 | .length = cpu_to_le32(sizeof(descriptors)), |
| 102 | .flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC | |
| 103 | FUNCTIONFS_HAS_HS_DESC | |
| 104 | FUNCTIONFS_HAS_SS_DESC), |
| 105 | .fs_count = 3, |
| 106 | .hs_count = 3, |
| 107 | .ss_count = 5, |
| 108 | .fs_descs = { |
| 109 | .intf = { |
| 110 | .bLength = sizeof(descriptors.fs_descs.intf), |
| 111 | .bDescriptorType = USB_DT_INTERFACE, |
| 112 | .bInterfaceNumber = 0, |
| 113 | .bNumEndpoints = 2, |
| 114 | .bInterfaceClass = ADB_CLASS, |
| 115 | .bInterfaceSubClass = ADB_SUBCLASS, |
| 116 | .bInterfaceProtocol = ADB_PROTOCOL, |
| 117 | .iInterface = 1, /* first string from the provided table */ |
| 118 | }, |
| 119 | .source = { |
| 120 | .bLength = sizeof(descriptors.fs_descs.source), |
| 121 | .bDescriptorType = USB_DT_ENDPOINT, |
| 122 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 123 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 124 | .wMaxPacketSize = MAX_PACKET_SIZE_FS, |
| 125 | }, |
| 126 | .sink = { |
| 127 | .bLength = sizeof(descriptors.fs_descs.sink), |
| 128 | .bDescriptorType = USB_DT_ENDPOINT, |
| 129 | .bEndpointAddress = 2 | USB_DIR_IN, |
| 130 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 131 | .wMaxPacketSize = MAX_PACKET_SIZE_FS, |
| 132 | }, |
| 133 | }, |
| 134 | .hs_descs = { |
| 135 | .intf = { |
| 136 | .bLength = sizeof(descriptors.hs_descs.intf), |
| 137 | .bDescriptorType = USB_DT_INTERFACE, |
| 138 | .bInterfaceNumber = 0, |
| 139 | .bNumEndpoints = 2, |
| 140 | .bInterfaceClass = ADB_CLASS, |
| 141 | .bInterfaceSubClass = ADB_SUBCLASS, |
| 142 | .bInterfaceProtocol = ADB_PROTOCOL, |
| 143 | .iInterface = 1, /* first string from the provided table */ |
| 144 | }, |
| 145 | .source = { |
| 146 | .bLength = sizeof(descriptors.hs_descs.source), |
| 147 | .bDescriptorType = USB_DT_ENDPOINT, |
| 148 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 149 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 150 | .wMaxPacketSize = MAX_PACKET_SIZE_HS, |
| 151 | }, |
| 152 | .sink = { |
| 153 | .bLength = sizeof(descriptors.hs_descs.sink), |
| 154 | .bDescriptorType = USB_DT_ENDPOINT, |
| 155 | .bEndpointAddress = 2 | USB_DIR_IN, |
| 156 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 157 | .wMaxPacketSize = MAX_PACKET_SIZE_HS, |
| 158 | }, |
| 159 | }, |
| 160 | .ss_descs = { |
| 161 | .intf = { |
| 162 | .bLength = sizeof(descriptors.ss_descs.intf), |
| 163 | .bDescriptorType = USB_DT_INTERFACE, |
| 164 | .bInterfaceNumber = 0, |
| 165 | .bNumEndpoints = 2, |
| 166 | .bInterfaceClass = ADB_CLASS, |
| 167 | .bInterfaceSubClass = ADB_SUBCLASS, |
| 168 | .bInterfaceProtocol = ADB_PROTOCOL, |
| 169 | .iInterface = 1, /* first string from the provided table */ |
| 170 | }, |
| 171 | .source = { |
| 172 | .bLength = sizeof(descriptors.ss_descs.source), |
| 173 | .bDescriptorType = USB_DT_ENDPOINT, |
| 174 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 175 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 176 | .wMaxPacketSize = MAX_PACKET_SIZE_SS, |
| 177 | }, |
| 178 | .source_comp = { |
| 179 | .bLength = sizeof(descriptors.ss_descs.source_comp), |
| 180 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 181 | }, |
| 182 | .sink = { |
| 183 | .bLength = sizeof(descriptors.ss_descs.sink), |
| 184 | .bDescriptorType = USB_DT_ENDPOINT, |
| 185 | .bEndpointAddress = 2 | USB_DIR_IN, |
| 186 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 187 | .wMaxPacketSize = MAX_PACKET_SIZE_SS, |
| 188 | }, |
| 189 | .sink_comp = { |
| 190 | .bLength = sizeof(descriptors.ss_descs.sink_comp), |
| 191 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 192 | }, |
| 193 | }, |
| 194 | }; |
| 195 | |
| 196 | #define STR_INTERFACE_ "ADB Interface" |
| 197 | |
| 198 | static const struct { |
| 199 | struct usb_functionfs_strings_head header; |
| 200 | struct { |
| 201 | __le16 code; |
| 202 | const char str1[sizeof(STR_INTERFACE_)]; |
| 203 | } __attribute__((packed)) lang0; |
| 204 | } __attribute__((packed)) strings = { |
| 205 | .header = { |
| 206 | .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC), |
| 207 | .length = cpu_to_le32(sizeof(strings)), |
| 208 | .str_count = cpu_to_le32(1), |
| 209 | .lang_count = cpu_to_le32(1), |
| 210 | }, |
| 211 | .lang0 = { |
| 212 | cpu_to_le16(0x0409), /* en-us */ |
| 213 | STR_INTERFACE_, |
| 214 | }, |
| 215 | }; |
| 216 | |
| 217 | static void *usb_adb_open_thread(void *x) |
| 218 | { |
| 219 | struct usb_handle *usb = (struct usb_handle *)x; |
| 220 | int fd; |
| 221 | |
| 222 | while (1) { |
| 223 | // wait until the USB device needs opening |
| 224 | adb_mutex_lock(&usb->lock); |
| 225 | while (usb->fd != -1) |
| 226 | adb_cond_wait(&usb->notify, &usb->lock); |
| 227 | adb_mutex_unlock(&usb->lock); |
| 228 | |
| 229 | D("[ usb_thread - opening device ]\n"); |
| 230 | do { |
| 231 | /* XXX use inotify? */ |
| 232 | fd = unix_open("/dev/android_adb", O_RDWR); |
| 233 | if (fd < 0) { |
| 234 | // to support older kernels |
| 235 | fd = unix_open("/dev/android", O_RDWR); |
| 236 | } |
| 237 | if (fd < 0) { |
| 238 | adb_sleep_ms(1000); |
| 239 | } |
| 240 | } while (fd < 0); |
| 241 | D("[ opening device succeeded ]\n"); |
| 242 | |
| 243 | close_on_exec(fd); |
| 244 | usb->fd = fd; |
| 245 | |
| 246 | D("[ usb_thread - registering device ]\n"); |
| 247 | register_usb_transport(usb, 0, 0, 1); |
| 248 | } |
| 249 | |
| 250 | // never gets here |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static int usb_adb_write(usb_handle *h, const void *data, int len) |
| 255 | { |
| 256 | int n; |
| 257 | |
| 258 | D("about to write (fd=%d, len=%d)\n", h->fd, len); |
| 259 | n = adb_write(h->fd, data, len); |
| 260 | if(n != len) { |
| 261 | D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", |
| 262 | h->fd, n, errno, strerror(errno)); |
| 263 | return -1; |
| 264 | } |
| 265 | D("[ done fd=%d ]\n", h->fd); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static int usb_adb_read(usb_handle *h, void *data, int len) |
| 270 | { |
| 271 | int n; |
| 272 | |
| 273 | D("about to read (fd=%d, len=%d)\n", h->fd, len); |
| 274 | n = adb_read(h->fd, data, len); |
| 275 | if(n != len) { |
| 276 | D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", |
| 277 | h->fd, n, errno, strerror(errno)); |
| 278 | return -1; |
| 279 | } |
| 280 | D("[ done fd=%d ]\n", h->fd); |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | static void usb_adb_kick(usb_handle *h) |
| 285 | { |
| 286 | D("usb_kick\n"); |
| 287 | adb_mutex_lock(&h->lock); |
| 288 | adb_close(h->fd); |
| 289 | h->fd = -1; |
| 290 | |
| 291 | // notify usb_adb_open_thread that we are disconnected |
| 292 | adb_cond_signal(&h->notify); |
| 293 | adb_mutex_unlock(&h->lock); |
| 294 | } |
| 295 | |
| 296 | static void usb_adb_init() |
| 297 | { |
| 298 | usb_handle *h; |
| 299 | adb_thread_t tid; |
| 300 | int fd; |
| 301 | |
| 302 | h = calloc(1, sizeof(usb_handle)); |
| 303 | if (h == 0) { |
| 304 | fatal("cannot allocate usb_handle"); |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | h->write = usb_adb_write; |
| 309 | h->read = usb_adb_read; |
| 310 | h->kick = usb_adb_kick; |
| 311 | h->bulk_in = -1; |
| 312 | h->bulk_out = -1; |
| 313 | h->control = -1; |
| 314 | h->ffs_control_thread_created = false; |
| 315 | h->fd = -1; |
| 316 | |
| 317 | adb_cond_init(&h->notify, 0); |
| 318 | adb_mutex_init(&h->lock, 0); |
| 319 | |
| 320 | // Open the file /dev/android_adb_enable to trigger |
| 321 | // the enabling of the adb USB function in the kernel. |
| 322 | // We never touch this file again - just leave it open |
| 323 | // indefinitely so the kernel will know when we are running |
| 324 | // and when we are not. |
| 325 | fd = unix_open("/dev/android_adb_enable", O_RDWR); |
| 326 | if (fd < 0) { |
| 327 | D("failed to open /dev/android_adb_enable\n"); |
| 328 | } else { |
| 329 | close_on_exec(fd); |
| 330 | } |
| 331 | |
| 332 | D("[ usb_init - starting thread ]\n"); |
| 333 | if(adb_thread_create(&tid, usb_adb_open_thread, h)){ |
| 334 | fatal_errno("cannot create usb thread"); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | static void *ffs_control_read_msg_thread(void *_h) |
| 339 | { |
| 340 | usb_handle *h = _h; |
| 341 | int fd = h->control; |
| 342 | |
| 343 | char buf[MAX_PACKET_SIZE_FS]; |
| 344 | int size = 0;//event.u.setup.wLength; |
| 345 | if (DEBUG) D("FUNCTIONFS_SETUP acking host-to-device control transfer size=%d", size); |
| 346 | if (size+1 > MAX_PACKET_SIZE_FS) { |
| 347 | D("package size larger than MAX_PACKET_SIZE_FS"); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | if (DEBUG) D("FUNCTIONFS_SETUP before get host-to-device msg size:%d", size); |
| 352 | int rc = adb_read(fd, buf, size); |
| 353 | if (DEBUG) D("FUNCTIONFS_SETUP after get host-to-device msg rc:%d", rc); |
| 354 | if (rc != size) { |
| 355 | D("Read %d bytes when trying to read control request, expected %d", rc, size); |
| 356 | } |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static char* ffs_get_event_type_code(int type) { |
| 361 | char* code; |
| 362 | switch(type) { |
| 363 | case FUNCTIONFS_BIND: |
| 364 | code = "BIND"; |
| 365 | break; |
| 366 | case FUNCTIONFS_UNBIND: |
| 367 | code = "UNBIND"; |
| 368 | break; |
| 369 | case FUNCTIONFS_ENABLE: |
| 370 | code = "ENABLE"; |
| 371 | break; |
| 372 | case FUNCTIONFS_DISABLE: |
| 373 | code = "DISABLE"; |
| 374 | break; |
| 375 | case FUNCTIONFS_SETUP: |
| 376 | code = "SETUP"; |
| 377 | break; |
| 378 | case FUNCTIONFS_SUSPEND: |
| 379 | code = "SUSPEND"; |
| 380 | break; |
| 381 | case FUNCTIONFS_RESUME: |
| 382 | code = "RESUME"; |
| 383 | break; |
| 384 | default: |
| 385 | code = "UNKNOWN"; |
| 386 | break; |
| 387 | } |
| 388 | return code; |
| 389 | } |
| 390 | |
| 391 | static void ffs_control_event_handler(usb_handle *h) { |
| 392 | int fd = h->control; |
| 393 | struct usb_functionfs_event event; |
| 394 | int ret = 0; |
| 395 | |
| 396 | if (DEBUG) D("start remote usb read control fd:%d size=%d\n", fd, (int)sizeof(event)); |
| 397 | ret = adb_read(fd, &event, sizeof(event)); |
| 398 | if (DEBUG) D("done remote usb read control ret=%d size=%d\n", ret, (int)sizeof(event)); |
| 399 | if (ret != sizeof(event)) { |
| 400 | if (DEBUG) D("remote usb: read size:%d not expect to event size:%d\n", ret, (int)sizeof(event)); |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | D("event.type: %s\n", ffs_get_event_type_code(event.type)); |
| 405 | |
| 406 | switch (event.type) { |
| 407 | case FUNCTIONFS_SETUP: { |
| 408 | D("received FUNCTIONFS_SETUP"); |
| 409 | D("bRequestType = %d",(int)(event.u.setup.bRequestType)); |
| 410 | D("bRequest = %d\n", (int)(event.u.setup.bRequest)); |
| 411 | D("wValue = %d\n", (int)(event.u.setup.wValue)); |
| 412 | D("wIndex = %d\n", (int)(event.u.setup.wIndex)); |
| 413 | D("wLength = %d\n", (int)(event.u.setup.wLength)); |
| 414 | |
| 415 | if ((event.u.setup.bRequestType & USB_DIR_IN)) { |
| 416 | if (DEBUG) D("FUNCTIONFS_SETUP acking device-to-host control transfer"); |
| 417 | int rc = adb_write(fd, "", 0); |
| 418 | if (rc != 0) { |
| 419 | D("Failed to write empty packet to host"); |
| 420 | break; |
| 421 | } |
| 422 | } else { |
| 423 | adb_thread_t ffs_control_read_msg_thread_ptr; |
| 424 | |
| 425 | if(adb_thread_create(&ffs_control_read_msg_thread_ptr, ffs_control_read_msg_thread, h)) { |
| 426 | fatal_errno("cannot create control thread"); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | if (DEBUG) D("Wait 100ms before kill read msg thread"); |
| 431 | adb_sleep_ms(100); |
| 432 | int ret = pthread_cancel(ffs_control_read_msg_thread_ptr); |
| 433 | D("Done kill the read msg thread ret=%d", ret); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // -1 means failure |
| 440 | static int epoll_add_fd(int epfd, int fd) { |
| 441 | struct epoll_event ev; |
| 442 | memset(&ev, 0, sizeof(ev)); |
| 443 | ev.data.fd = fd; |
| 444 | ev.events = EPOLLIN; |
| 445 | // don't set the fd to edge trigger |
| 446 | // the some event like accept may be lost if two or more clients are connecting to server at the same time |
| 447 | // level trigger is preferred to avoid event lost |
| 448 | // do not set EPOLLOUT due to it will always trigger when write is available |
| 449 | if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
| 450 | D("epoll_add_fd3() epoll_ctl() failed reason=[%s]%d epfd=%d fd=%d", |
| 451 | strerror(errno), errno, epfd, fd); |
| 452 | return -1; |
| 453 | } |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static void *ffs_control_thread(void *_h) |
| 458 | { |
| 459 | usb_handle *h = _h; |
| 460 | int control_fd = h->control; |
| 461 | struct epoll_event events[FFS_CONTOL_MAX_EPOLL_EVENT]; |
| 462 | |
| 463 | int epfd = epoll_create(FFS_CONTOL_MAX_EPOLL_EVENT); |
| 464 | if(epfd == -1) { |
| 465 | D("ERR: epoll_create() fail reason=[%s]", strerror(errno)); |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | epoll_add_fd(epfd, control_fd); |
| 470 | |
| 471 | while(1) { |
| 472 | int i; |
| 473 | int n; |
| 474 | |
| 475 | D("Before ffs control thread epoll_wait"); |
| 476 | n = epoll_wait(epfd, events, FFS_CONTOL_MAX_EPOLL_EVENT , -1); |
| 477 | |
| 478 | for(i = 0; i < n; i++) { |
| 479 | if(events[i].data.fd == control_fd) { |
| 480 | if(events[i].events & EPOLLIN) { |
| 481 | if (DEBUG) D("control_fd event triggered"); |
| 482 | ffs_control_event_handler(h); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | void ffs_create_control_thread(usb_handle *h) { |
| 490 | if (h->control >= 0) { // only for ffs usb |
| 491 | if (!h->ffs_control_thread_created) { |
| 492 | adb_thread_t ffs_control_thread_ptr; |
| 493 | |
| 494 | if(adb_thread_create(&ffs_control_thread_ptr, ffs_control_thread, h)){ |
| 495 | fatal_errno("cannot create ffs_control_thread"); |
| 496 | } else { |
| 497 | h->ffs_control_thread_created = true; |
| 498 | if (DEBUG) D("Created ffs_control_thread success"); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | static void init_functionfs(struct usb_handle *h) |
| 505 | { |
| 506 | ssize_t ret; |
| 507 | |
| 508 | if (h->control < 0) { // might have already done this before |
| 509 | D("OPENING %s\n", USB_FFS_ADB_EP0); |
| 510 | h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR); |
| 511 | if (h->control < 0) { |
| 512 | D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno); |
| 513 | goto err; |
| 514 | } |
| 515 | |
| 516 | ret = adb_write(h->control, &descriptors, sizeof(descriptors)); |
| 517 | if (ret < 0) { |
| 518 | D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno); |
| 519 | goto err; |
| 520 | } |
| 521 | |
| 522 | ret = adb_write(h->control, &strings, sizeof(strings)); |
| 523 | if (ret < 0) { |
| 524 | D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno); |
| 525 | goto err; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR); |
| 530 | if (h->bulk_out < 0) { |
| 531 | D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno); |
| 532 | goto err; |
| 533 | } |
| 534 | |
| 535 | h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR); |
| 536 | if (h->bulk_in < 0) { |
| 537 | D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno); |
| 538 | goto err; |
| 539 | } |
| 540 | |
| 541 | ffs_create_control_thread(h); |
| 542 | return; |
| 543 | |
| 544 | err: |
| 545 | if (h->bulk_in > 0) { |
| 546 | adb_close(h->bulk_in); |
| 547 | h->bulk_in = -1; |
| 548 | } |
| 549 | if (h->bulk_out > 0) { |
| 550 | adb_close(h->bulk_out); |
| 551 | h->bulk_out = -1; |
| 552 | } |
| 553 | if (h->control > 0) { |
| 554 | adb_close(h->control); |
| 555 | h->control = -1; |
| 556 | } |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | static void *usb_ffs_open_thread(void *x) |
| 561 | { |
| 562 | struct usb_handle *usb = (struct usb_handle *)x; |
| 563 | |
| 564 | while (1) { |
| 565 | // wait until the USB device needs opening |
| 566 | adb_mutex_lock(&usb->lock); |
| 567 | while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1) |
| 568 | adb_cond_wait(&usb->notify, &usb->lock); |
| 569 | adb_mutex_unlock(&usb->lock); |
| 570 | |
| 571 | while (1) { |
| 572 | init_functionfs(usb); |
| 573 | |
| 574 | if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0) |
| 575 | break; |
| 576 | |
| 577 | adb_sleep_ms(1000); |
| 578 | } |
| 579 | |
| 580 | D("[ usb_thread - registering device ]\n"); |
| 581 | register_usb_transport(usb, 0, 0, 1); |
| 582 | } |
| 583 | |
| 584 | // never gets here |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static int bulk_write(int bulk_in, const char *buf, size_t length) |
| 589 | { |
| 590 | size_t count = 0; |
| 591 | int ret; |
| 592 | |
| 593 | do { |
| 594 | ret = adb_write(bulk_in, buf + count, length - count); |
| 595 | if (ret < 0) { |
| 596 | if (errno != EINTR) |
| 597 | return ret; |
| 598 | } else { |
| 599 | count += ret; |
| 600 | } |
| 601 | } while (count < length); |
| 602 | |
| 603 | D("[ bulk_write done fd=%d ]\n", bulk_in); |
| 604 | return count; |
| 605 | } |
| 606 | |
| 607 | static int usb_ffs_write(usb_handle *h, const void *data, int len) |
| 608 | { |
| 609 | int n; |
| 610 | |
| 611 | D("about to write (fd=%d, len=%d)\n", h->bulk_in, len); |
| 612 | n = bulk_write(h->bulk_in, data, len); |
| 613 | if (n != len) { |
| 614 | D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", |
| 615 | h->bulk_in, n, errno, strerror(errno)); |
| 616 | return -1; |
| 617 | } |
| 618 | D("[ done fd=%d ]\n", h->bulk_in); |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static int bulk_read(int bulk_out, char *buf, size_t length) |
| 623 | { |
| 624 | size_t count = 0; |
| 625 | int ret; |
| 626 | |
| 627 | do { |
| 628 | ret = adb_read(bulk_out, buf + count, length - count); |
| 629 | if (ret < 0) { |
| 630 | if (errno != EINTR) { |
| 631 | D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n", |
| 632 | bulk_out, length, count); |
| 633 | return ret; |
| 634 | } |
| 635 | } else { |
| 636 | count += ret; |
| 637 | } |
| 638 | } while (count < length); |
| 639 | |
| 640 | return count; |
| 641 | } |
| 642 | |
| 643 | static int usb_ffs_read(usb_handle *h, void *data, int len) |
| 644 | { |
| 645 | int n; |
| 646 | |
| 647 | D("about to read (fd=%d, len=%d)\n", h->bulk_out, len); |
| 648 | n = bulk_read(h->bulk_out, data, len); |
| 649 | if (n != len) { |
| 650 | D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", |
| 651 | h->bulk_out, n, errno, strerror(errno)); |
| 652 | return -1; |
| 653 | } |
| 654 | D("[ done fd=%d ]\n", h->bulk_out); |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static void usb_ffs_kick(usb_handle *h) |
| 659 | { |
| 660 | int err; |
| 661 | |
| 662 | err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT); |
| 663 | if (err < 0) |
| 664 | D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno); |
| 665 | |
| 666 | err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT); |
| 667 | if (err < 0) |
| 668 | D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno); |
| 669 | |
| 670 | adb_mutex_lock(&h->lock); |
| 671 | |
| 672 | // don't close ep0 here, since we may not need to reinitialize it with |
| 673 | // the same descriptors again. if however ep1/ep2 fail to re-open in |
| 674 | // init_functionfs, only then would we close and open ep0 again. |
| 675 | adb_close(h->bulk_out); |
| 676 | adb_close(h->bulk_in); |
| 677 | h->bulk_out = h->bulk_in = -1; |
| 678 | |
| 679 | // notify usb_ffs_open_thread that we are disconnected |
| 680 | adb_cond_signal(&h->notify); |
| 681 | adb_mutex_unlock(&h->lock); |
| 682 | } |
| 683 | |
| 684 | static void usb_ffs_init() |
| 685 | { |
| 686 | usb_handle *h; |
| 687 | adb_thread_t tid; |
| 688 | |
| 689 | D("[ usb_init - using FunctionFS ]\n"); |
| 690 | |
| 691 | h = calloc(1, sizeof(usb_handle)); |
| 692 | if (h == 0) { |
| 693 | fatal("cannot allocate usb_handle"); |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | h->write = usb_ffs_write; |
| 698 | h->read = usb_ffs_read; |
| 699 | h->kick = usb_ffs_kick; |
| 700 | |
| 701 | h->control = -1; |
| 702 | h->bulk_out = -1; |
| 703 | h->bulk_out = -1; |
| 704 | h->ffs_control_thread_created = false; |
| 705 | |
| 706 | adb_cond_init(&h->notify, 0); |
| 707 | adb_mutex_init(&h->lock, 0); |
| 708 | |
| 709 | D("[ usb_init - starting thread ]\n"); |
| 710 | if (adb_thread_create(&tid, usb_ffs_open_thread, h)){ |
| 711 | fatal_errno("[ cannot create usb thread ]\n"); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | void usb_init() |
| 716 | { |
| 717 | if (access(USB_FFS_ADB_EP0, F_OK) == 0) |
| 718 | usb_ffs_init(); |
| 719 | else |
| 720 | usb_adb_init(); |
| 721 | } |
| 722 | |
| 723 | void usb_cleanup() |
| 724 | { |
| 725 | } |
| 726 | |
| 727 | int usb_write(usb_handle *h, const void *data, int len) |
| 728 | { |
| 729 | return h->write(h, data, len); |
| 730 | } |
| 731 | |
| 732 | int usb_read(usb_handle *h, void *data, int len) |
| 733 | { |
| 734 | return h->read(h, data, len); |
| 735 | } |
| 736 | int usb_close(usb_handle *h) |
| 737 | { |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | void usb_kick(usb_handle *h) |
| 742 | { |
| 743 | h->kick(h); |
| 744 | } |