rjw | 2e8229f | 2022-02-15 21:08:12 +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 | |
qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 61 | #define USB_FFS_WAKE_LOCK_ID "usb_adbd_wakelock" |
| 62 | #define USB_FFS_WAKE_LOCK_TIMEOUT (2 * 1000) |
| 63 | |
| 64 | static const char* sTag = "adbd_usb"; |
| 65 | adb_mutex_t g_wake_lock; |
| 66 | timer_t g_wake_unlock_timer; |
| 67 | bool g_wake_lock_acquired; |
| 68 | typedef void (*timer_routine) (int id); |
| 69 | |
rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 70 | struct usb_handle |
| 71 | { |
| 72 | adb_cond_t notify; |
| 73 | adb_mutex_t lock; |
| 74 | |
| 75 | int (*write)(usb_handle *h, const void *data, int len); |
| 76 | int (*read)(usb_handle *h, void *data, int len); |
| 77 | void (*kick)(usb_handle *h); |
| 78 | |
| 79 | // Legacy f_adb |
| 80 | int fd; |
| 81 | |
| 82 | // FunctionFS |
| 83 | int control; |
| 84 | int bulk_out; /* "out" from the host's perspective => source for adbd */ |
| 85 | int bulk_in; /* "in" from the host's perspective => sink for adbd */ |
| 86 | bool ffs_control_thread_created; |
qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 87 | bool ffs_control_suspend_mode; |
rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | static const struct { |
| 91 | __le32 magic; |
| 92 | __le32 length; |
| 93 | __le32 flags; |
| 94 | __le32 fs_count; |
| 95 | __le32 hs_count; |
| 96 | __le32 ss_count; |
| 97 | struct { |
| 98 | struct usb_interface_descriptor intf; |
| 99 | struct usb_endpoint_descriptor_no_audio source; |
| 100 | struct usb_endpoint_descriptor_no_audio sink; |
| 101 | } __attribute__((packed)) fs_descs, hs_descs; |
| 102 | struct { |
| 103 | struct usb_interface_descriptor intf; |
| 104 | struct usb_endpoint_descriptor_no_audio source; |
| 105 | struct usb_ss_ep_comp_descriptor source_comp; |
| 106 | struct usb_endpoint_descriptor_no_audio sink; |
| 107 | struct usb_ss_ep_comp_descriptor sink_comp; |
| 108 | } __attribute__((packed)) ss_descs; |
| 109 | } __attribute__((packed)) descriptors = { |
| 110 | .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2), |
| 111 | .length = cpu_to_le32(sizeof(descriptors)), |
| 112 | .flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC | |
| 113 | FUNCTIONFS_HAS_HS_DESC | |
| 114 | FUNCTIONFS_HAS_SS_DESC), |
| 115 | .fs_count = 3, |
| 116 | .hs_count = 3, |
| 117 | .ss_count = 5, |
| 118 | .fs_descs = { |
| 119 | .intf = { |
| 120 | .bLength = sizeof(descriptors.fs_descs.intf), |
| 121 | .bDescriptorType = USB_DT_INTERFACE, |
| 122 | .bInterfaceNumber = 0, |
| 123 | .bNumEndpoints = 2, |
| 124 | .bInterfaceClass = ADB_CLASS, |
| 125 | .bInterfaceSubClass = ADB_SUBCLASS, |
| 126 | .bInterfaceProtocol = ADB_PROTOCOL, |
| 127 | .iInterface = 1, /* first string from the provided table */ |
| 128 | }, |
| 129 | .source = { |
| 130 | .bLength = sizeof(descriptors.fs_descs.source), |
| 131 | .bDescriptorType = USB_DT_ENDPOINT, |
| 132 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 133 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 134 | .wMaxPacketSize = MAX_PACKET_SIZE_FS, |
| 135 | }, |
| 136 | .sink = { |
| 137 | .bLength = sizeof(descriptors.fs_descs.sink), |
| 138 | .bDescriptorType = USB_DT_ENDPOINT, |
| 139 | .bEndpointAddress = 2 | USB_DIR_IN, |
| 140 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 141 | .wMaxPacketSize = MAX_PACKET_SIZE_FS, |
| 142 | }, |
| 143 | }, |
| 144 | .hs_descs = { |
| 145 | .intf = { |
| 146 | .bLength = sizeof(descriptors.hs_descs.intf), |
| 147 | .bDescriptorType = USB_DT_INTERFACE, |
| 148 | .bInterfaceNumber = 0, |
| 149 | .bNumEndpoints = 2, |
| 150 | .bInterfaceClass = ADB_CLASS, |
| 151 | .bInterfaceSubClass = ADB_SUBCLASS, |
| 152 | .bInterfaceProtocol = ADB_PROTOCOL, |
| 153 | .iInterface = 1, /* first string from the provided table */ |
| 154 | }, |
| 155 | .source = { |
| 156 | .bLength = sizeof(descriptors.hs_descs.source), |
| 157 | .bDescriptorType = USB_DT_ENDPOINT, |
| 158 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 159 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 160 | .wMaxPacketSize = MAX_PACKET_SIZE_HS, |
| 161 | }, |
| 162 | .sink = { |
| 163 | .bLength = sizeof(descriptors.hs_descs.sink), |
| 164 | .bDescriptorType = USB_DT_ENDPOINT, |
| 165 | .bEndpointAddress = 2 | USB_DIR_IN, |
| 166 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 167 | .wMaxPacketSize = MAX_PACKET_SIZE_HS, |
| 168 | }, |
| 169 | }, |
| 170 | .ss_descs = { |
| 171 | .intf = { |
| 172 | .bLength = sizeof(descriptors.ss_descs.intf), |
| 173 | .bDescriptorType = USB_DT_INTERFACE, |
| 174 | .bInterfaceNumber = 0, |
| 175 | .bNumEndpoints = 2, |
| 176 | .bInterfaceClass = ADB_CLASS, |
| 177 | .bInterfaceSubClass = ADB_SUBCLASS, |
| 178 | .bInterfaceProtocol = ADB_PROTOCOL, |
| 179 | .iInterface = 1, /* first string from the provided table */ |
| 180 | }, |
| 181 | .source = { |
| 182 | .bLength = sizeof(descriptors.ss_descs.source), |
| 183 | .bDescriptorType = USB_DT_ENDPOINT, |
| 184 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 185 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 186 | .wMaxPacketSize = MAX_PACKET_SIZE_SS, |
| 187 | }, |
| 188 | .source_comp = { |
| 189 | .bLength = sizeof(descriptors.ss_descs.source_comp), |
| 190 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 191 | }, |
| 192 | .sink = { |
| 193 | .bLength = sizeof(descriptors.ss_descs.sink), |
| 194 | .bDescriptorType = USB_DT_ENDPOINT, |
| 195 | .bEndpointAddress = 2 | USB_DIR_IN, |
| 196 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 197 | .wMaxPacketSize = MAX_PACKET_SIZE_SS, |
| 198 | }, |
| 199 | .sink_comp = { |
| 200 | .bLength = sizeof(descriptors.ss_descs.sink_comp), |
| 201 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 202 | }, |
| 203 | }, |
| 204 | }; |
| 205 | |
| 206 | #define STR_INTERFACE_ "ADB Interface" |
| 207 | |
| 208 | static const struct { |
| 209 | struct usb_functionfs_strings_head header; |
| 210 | struct { |
| 211 | __le16 code; |
| 212 | const char str1[sizeof(STR_INTERFACE_)]; |
| 213 | } __attribute__((packed)) lang0; |
| 214 | } __attribute__((packed)) strings = { |
| 215 | .header = { |
| 216 | .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC), |
| 217 | .length = cpu_to_le32(sizeof(strings)), |
| 218 | .str_count = cpu_to_le32(1), |
| 219 | .lang_count = cpu_to_le32(1), |
| 220 | }, |
| 221 | .lang0 = { |
| 222 | cpu_to_le16(0x0409), /* en-us */ |
| 223 | STR_INTERFACE_, |
| 224 | }, |
| 225 | }; |
| 226 | |
| 227 | static void *usb_adb_open_thread(void *x) |
| 228 | { |
| 229 | struct usb_handle *usb = (struct usb_handle *)x; |
| 230 | int fd; |
| 231 | |
| 232 | while (1) { |
| 233 | // wait until the USB device needs opening |
| 234 | adb_mutex_lock(&usb->lock); |
| 235 | while (usb->fd != -1) |
| 236 | adb_cond_wait(&usb->notify, &usb->lock); |
| 237 | adb_mutex_unlock(&usb->lock); |
| 238 | |
| 239 | D("[ usb_thread - opening device ]\n"); |
| 240 | do { |
| 241 | /* XXX use inotify? */ |
| 242 | fd = unix_open("/dev/android_adb", O_RDWR); |
| 243 | if (fd < 0) { |
| 244 | // to support older kernels |
| 245 | fd = unix_open("/dev/android", O_RDWR); |
| 246 | } |
| 247 | if (fd < 0) { |
| 248 | adb_sleep_ms(1000); |
| 249 | } |
| 250 | } while (fd < 0); |
| 251 | D("[ opening device succeeded ]\n"); |
| 252 | |
| 253 | close_on_exec(fd); |
| 254 | usb->fd = fd; |
| 255 | |
| 256 | D("[ usb_thread - registering device ]\n"); |
| 257 | register_usb_transport(usb, 0, 0, 1); |
| 258 | } |
| 259 | |
| 260 | // never gets here |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static int usb_adb_write(usb_handle *h, const void *data, int len) |
| 265 | { |
| 266 | int n; |
| 267 | |
| 268 | D("about to write (fd=%d, len=%d)\n", h->fd, len); |
| 269 | n = adb_write(h->fd, data, len); |
| 270 | if(n != len) { |
| 271 | D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", |
| 272 | h->fd, n, errno, strerror(errno)); |
| 273 | return -1; |
| 274 | } |
| 275 | D("[ done fd=%d ]\n", h->fd); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static int usb_adb_read(usb_handle *h, void *data, int len) |
| 280 | { |
| 281 | int n; |
| 282 | |
| 283 | D("about to read (fd=%d, len=%d)\n", h->fd, len); |
| 284 | n = adb_read(h->fd, data, len); |
| 285 | if(n != len) { |
| 286 | D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", |
| 287 | h->fd, n, errno, strerror(errno)); |
| 288 | return -1; |
| 289 | } |
| 290 | D("[ done fd=%d ]\n", h->fd); |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static void usb_adb_kick(usb_handle *h) |
| 295 | { |
| 296 | D("usb_kick\n"); |
| 297 | adb_mutex_lock(&h->lock); |
| 298 | adb_close(h->fd); |
| 299 | h->fd = -1; |
| 300 | |
| 301 | // notify usb_adb_open_thread that we are disconnected |
| 302 | adb_cond_signal(&h->notify); |
| 303 | adb_mutex_unlock(&h->lock); |
| 304 | } |
| 305 | |
| 306 | static void usb_adb_init() |
| 307 | { |
| 308 | usb_handle *h; |
| 309 | adb_thread_t tid; |
| 310 | int fd; |
| 311 | |
| 312 | h = calloc(1, sizeof(usb_handle)); |
| 313 | if (h == 0) { |
| 314 | fatal("cannot allocate usb_handle"); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | h->write = usb_adb_write; |
| 319 | h->read = usb_adb_read; |
| 320 | h->kick = usb_adb_kick; |
| 321 | h->bulk_in = -1; |
| 322 | h->bulk_out = -1; |
| 323 | h->control = -1; |
| 324 | h->ffs_control_thread_created = false; |
qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 325 | h->ffs_control_suspend_mode = false; |
rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 326 | h->fd = -1; |
| 327 | |
| 328 | adb_cond_init(&h->notify, 0); |
| 329 | adb_mutex_init(&h->lock, 0); |
qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 330 | adb_mutex_init(&g_wake_lock, 0); |
rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 331 | |
| 332 | // Open the file /dev/android_adb_enable to trigger |
| 333 | // the enabling of the adb USB function in the kernel. |
| 334 | // We never touch this file again - just leave it open |
| 335 | // indefinitely so the kernel will know when we are running |
| 336 | // and when we are not. |
| 337 | fd = unix_open("/dev/android_adb_enable", O_RDWR); |
| 338 | if (fd < 0) { |
| 339 | D("failed to open /dev/android_adb_enable\n"); |
| 340 | } else { |
| 341 | close_on_exec(fd); |
| 342 | } |
| 343 | |
| 344 | D("[ usb_init - starting thread ]\n"); |
| 345 | if(adb_thread_create(&tid, usb_adb_open_thread, h)){ |
| 346 | fatal_errno("cannot create usb thread"); |
| 347 | } |
| 348 | } |
| 349 | |
qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 350 | ////////////////////// wake lock /////////////////////////////// |
| 351 | timer_t timer_init(timer_routine cb, int id) { |
| 352 | struct sigevent sevp; |
| 353 | timer_t timerid; |
| 354 | |
| 355 | memset(&sevp, 0, sizeof(sevp)); |
| 356 | sevp.sigev_value.sival_int = id; |
| 357 | sevp.sigev_notify = SIGEV_THREAD; |
| 358 | sevp.sigev_notify_function = (void*)cb; |
| 359 | |
| 360 | if(timer_create(CLOCK_BOOTTIME, &sevp, &timerid) == -1) { |
| 361 | D("timer_init() timer_create() failed, reason=[%s]%d", |
| 362 | strerror(errno), errno); |
| 363 | return NULL; |
| 364 | } |
| 365 | return timerid; |
| 366 | } |
| 367 | |
| 368 | bool timer_start(timer_t timerid, int milliseconds) { |
| 369 | struct itimerspec expire; |
| 370 | expire.it_interval.tv_sec = 0; |
| 371 | expire.it_interval.tv_nsec = 0; |
| 372 | expire.it_value.tv_sec = milliseconds/1000; |
| 373 | expire.it_value.tv_nsec = (milliseconds%1000)*1000000; |
| 374 | if(timer_settime(timerid, 0, &expire, NULL) == -1) { |
| 375 | D("timer_start() timer_settime() failed, reason=[%s]%d", strerror(errno), errno); |
| 376 | return false; |
| 377 | } |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | bool timer_stop(timer_t timerid) { |
| 382 | return timer_start(timerid, 0); |
| 383 | } |
| 384 | |
| 385 | bool usb_ffs_wake_lock() { |
| 386 | #if !defined(__LBS_OS_LINUX__) //no permission to open wake_lock on Linux |
| 387 | int fd = adb_open("/sys/power/wake_lock", O_RDWR | O_NONBLOCK); |
| 388 | if(fd == -1) { |
| 389 | D("[%s] open() failed, reason=[%s]%d", sTag, strerror(errno), errno); |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | int ret = adb_write(fd, USB_FFS_WAKE_LOCK_ID, strlen(USB_FFS_WAKE_LOCK_ID)); |
| 394 | if(ret == -1) { |
| 395 | D("[%s] write() failed id=[%s], reason=[%s]%d", sTag, USB_FFS_WAKE_LOCK_ID, strerror(errno), errno); |
| 396 | adb_close(fd); |
| 397 | return false; |
| 398 | } |
| 399 | |
| 400 | adb_close(fd); |
| 401 | #endif |
| 402 | return true; |
| 403 | } |
| 404 | |
| 405 | bool usb_ffs_wake_unlock() { |
| 406 | #if !defined(__LBS_OS_LINUX__) //no permission to open wake_lock on Linux |
| 407 | int fd = adb_open("/sys/power/wake_unlock", O_RDWR | O_NONBLOCK); |
| 408 | if(fd == -1) { |
| 409 | D("[%s] open() failed, reason=[%s]%d", sTag, strerror(errno), errno); |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | int ret = adb_write(fd, USB_FFS_WAKE_LOCK_ID, strlen(USB_FFS_WAKE_LOCK_ID)); |
| 414 | if(ret == -1) { |
| 415 | D("[%s] write() failed id=[%s], reason=[%s]%d", sTag, |
| 416 | USB_FFS_WAKE_LOCK_ID, strerror(errno), errno); |
| 417 | adb_close(fd); |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | adb_close(fd); |
| 422 | #endif |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | static void usb_ffs_timer_wake_unlock_routine(int id) { |
| 427 | //do not use the internal msg or it will cause infinite loop in epoll_wait |
| 428 | adb_mutex_lock(&g_wake_lock); |
| 429 | if(g_wake_lock_acquired) { |
| 430 | if(usb_ffs_wake_unlock()) { |
| 431 | g_wake_lock_acquired = false; |
| 432 | } |
| 433 | } |
| 434 | if (DEBUG) D("[%s] unlock wake_lock_acquired=[%d]", sTag, g_wake_lock_acquired); |
| 435 | adb_mutex_unlock(&g_wake_lock); |
| 436 | } |
| 437 | |
rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 438 | static void *ffs_control_read_msg_thread(void *_h) |
| 439 | { |
| 440 | usb_handle *h = _h; |
| 441 | int fd = h->control; |
| 442 | |
| 443 | char buf[MAX_PACKET_SIZE_FS]; |
| 444 | int size = 0;//event.u.setup.wLength; |
| 445 | if (DEBUG) D("FUNCTIONFS_SETUP acking host-to-device control transfer size=%d", size); |
| 446 | if (size+1 > MAX_PACKET_SIZE_FS) { |
| 447 | D("package size larger than MAX_PACKET_SIZE_FS"); |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | if (DEBUG) D("FUNCTIONFS_SETUP before get host-to-device msg size:%d", size); |
| 452 | int rc = adb_read(fd, buf, size); |
| 453 | if (DEBUG) D("FUNCTIONFS_SETUP after get host-to-device msg rc:%d", rc); |
| 454 | if (rc != size) { |
| 455 | D("Read %d bytes when trying to read control request, expected %d", rc, size); |
| 456 | } |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static char* ffs_get_event_type_code(int type) { |
| 461 | char* code; |
| 462 | switch(type) { |
| 463 | case FUNCTIONFS_BIND: |
| 464 | code = "BIND"; |
| 465 | break; |
| 466 | case FUNCTIONFS_UNBIND: |
| 467 | code = "UNBIND"; |
| 468 | break; |
| 469 | case FUNCTIONFS_ENABLE: |
| 470 | code = "ENABLE"; |
| 471 | break; |
| 472 | case FUNCTIONFS_DISABLE: |
| 473 | code = "DISABLE"; |
| 474 | break; |
| 475 | case FUNCTIONFS_SETUP: |
| 476 | code = "SETUP"; |
| 477 | break; |
| 478 | case FUNCTIONFS_SUSPEND: |
| 479 | code = "SUSPEND"; |
| 480 | break; |
| 481 | case FUNCTIONFS_RESUME: |
| 482 | code = "RESUME"; |
| 483 | break; |
| 484 | default: |
| 485 | code = "UNKNOWN"; |
| 486 | break; |
| 487 | } |
| 488 | return code; |
| 489 | } |
| 490 | |
| 491 | static void ffs_control_event_handler(usb_handle *h) { |
| 492 | int fd = h->control; |
| 493 | struct usb_functionfs_event event; |
| 494 | int ret = 0; |
| 495 | |
| 496 | if (DEBUG) D("start remote usb read control fd:%d size=%d\n", fd, (int)sizeof(event)); |
| 497 | ret = adb_read(fd, &event, sizeof(event)); |
| 498 | if (DEBUG) D("done remote usb read control ret=%d size=%d\n", ret, (int)sizeof(event)); |
| 499 | if (ret != sizeof(event)) { |
| 500 | if (DEBUG) D("remote usb: read size:%d not expect to event size:%d\n", ret, (int)sizeof(event)); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | D("event.type: %s\n", ffs_get_event_type_code(event.type)); |
| 505 | |
| 506 | switch (event.type) { |
qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 507 | case FUNCTIONFS_SUSPEND: |
| 508 | D("received FUNCTIONFS_SUSPEND set suspend mode 1"); |
| 509 | h->ffs_control_suspend_mode = true; |
| 510 | break; |
| 511 | case FUNCTIONFS_RESUME: |
| 512 | D("received FUNCTIONFS_RESUME"); |
| 513 | break; |
rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 514 | case FUNCTIONFS_SETUP: { |
| 515 | D("received FUNCTIONFS_SETUP"); |
| 516 | D("bRequestType = %d",(int)(event.u.setup.bRequestType)); |
| 517 | D("bRequest = %d\n", (int)(event.u.setup.bRequest)); |
| 518 | D("wValue = %d\n", (int)(event.u.setup.wValue)); |
| 519 | D("wIndex = %d\n", (int)(event.u.setup.wIndex)); |
| 520 | D("wLength = %d\n", (int)(event.u.setup.wLength)); |
| 521 | |
| 522 | if ((event.u.setup.bRequestType & USB_DIR_IN)) { |
| 523 | if (DEBUG) D("FUNCTIONFS_SETUP acking device-to-host control transfer"); |
| 524 | int rc = adb_write(fd, "", 0); |
| 525 | if (rc != 0) { |
| 526 | D("Failed to write empty packet to host"); |
| 527 | break; |
| 528 | } |
| 529 | } else { |
| 530 | adb_thread_t ffs_control_read_msg_thread_ptr; |
| 531 | |
| 532 | if(adb_thread_create(&ffs_control_read_msg_thread_ptr, ffs_control_read_msg_thread, h)) { |
| 533 | fatal_errno("cannot create control thread"); |
| 534 | } |
| 535 | |
| 536 | |
| 537 | if (DEBUG) D("Wait 100ms before kill read msg thread"); |
| 538 | adb_sleep_ms(100); |
| 539 | int ret = pthread_cancel(ffs_control_read_msg_thread_ptr); |
| 540 | D("Done kill the read msg thread ret=%d", ret); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | // -1 means failure |
| 547 | static int epoll_add_fd(int epfd, int fd) { |
| 548 | struct epoll_event ev; |
| 549 | memset(&ev, 0, sizeof(ev)); |
| 550 | ev.data.fd = fd; |
| 551 | ev.events = EPOLLIN; |
| 552 | // don't set the fd to edge trigger |
| 553 | // the some event like accept may be lost if two or more clients are connecting to server at the same time |
| 554 | // level trigger is preferred to avoid event lost |
| 555 | // do not set EPOLLOUT due to it will always trigger when write is available |
| 556 | if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
| 557 | D("epoll_add_fd3() epoll_ctl() failed reason=[%s]%d epfd=%d fd=%d", |
| 558 | strerror(errno), errno, epfd, fd); |
| 559 | return -1; |
| 560 | } |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static void *ffs_control_thread(void *_h) |
| 565 | { |
| 566 | usb_handle *h = _h; |
| 567 | int control_fd = h->control; |
| 568 | struct epoll_event events[FFS_CONTOL_MAX_EPOLL_EVENT]; |
| 569 | |
qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 570 | g_wake_unlock_timer = timer_init(usb_ffs_timer_wake_unlock_routine, 0); |
rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 571 | int epfd = epoll_create(FFS_CONTOL_MAX_EPOLL_EVENT); |
| 572 | if(epfd == -1) { |
| 573 | D("ERR: epoll_create() fail reason=[%s]", strerror(errno)); |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | epoll_add_fd(epfd, control_fd); |
| 578 | |
| 579 | while(1) { |
| 580 | int i; |
| 581 | int n; |
| 582 | |
| 583 | D("Before ffs control thread epoll_wait"); |
| 584 | n = epoll_wait(epfd, events, FFS_CONTOL_MAX_EPOLL_EVENT , -1); |
| 585 | |
qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 586 | adb_mutex_lock(&g_wake_lock); |
| 587 | timer_stop(g_wake_unlock_timer); |
| 588 | if (!g_wake_lock_acquired) { |
| 589 | g_wake_lock_acquired = usb_ffs_wake_lock(); |
| 590 | if (DEBUG) D("[%s] wake_lock_acquired=[%d]", sTag, g_wake_lock_acquired); |
| 591 | } |
| 592 | adb_mutex_unlock(&g_wake_lock); |
| 593 | |
rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 594 | for(i = 0; i < n; i++) { |
| 595 | if(events[i].data.fd == control_fd) { |
| 596 | if(events[i].events & EPOLLIN) { |
| 597 | if (DEBUG) D("control_fd event triggered"); |
| 598 | ffs_control_event_handler(h); |
| 599 | } |
| 600 | } |
| 601 | } |
qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 602 | |
| 603 | timer_start(g_wake_unlock_timer, USB_FFS_WAKE_LOCK_TIMEOUT); |
rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| 607 | void ffs_create_control_thread(usb_handle *h) { |
| 608 | if (h->control >= 0) { // only for ffs usb |
| 609 | if (!h->ffs_control_thread_created) { |
| 610 | adb_thread_t ffs_control_thread_ptr; |
| 611 | |
| 612 | if(adb_thread_create(&ffs_control_thread_ptr, ffs_control_thread, h)){ |
| 613 | fatal_errno("cannot create ffs_control_thread"); |
| 614 | } else { |
| 615 | h->ffs_control_thread_created = true; |
| 616 | if (DEBUG) D("Created ffs_control_thread success"); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | static void init_functionfs(struct usb_handle *h) |
| 623 | { |
| 624 | ssize_t ret; |
| 625 | |
| 626 | if (h->control < 0) { // might have already done this before |
| 627 | D("OPENING %s\n", USB_FFS_ADB_EP0); |
| 628 | h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR); |
| 629 | if (h->control < 0) { |
| 630 | D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno); |
| 631 | goto err; |
| 632 | } |
| 633 | |
| 634 | ret = adb_write(h->control, &descriptors, sizeof(descriptors)); |
| 635 | if (ret < 0) { |
| 636 | D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno); |
| 637 | goto err; |
| 638 | } |
| 639 | |
| 640 | ret = adb_write(h->control, &strings, sizeof(strings)); |
| 641 | if (ret < 0) { |
| 642 | D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno); |
| 643 | goto err; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR); |
| 648 | if (h->bulk_out < 0) { |
| 649 | D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno); |
| 650 | goto err; |
| 651 | } |
| 652 | |
| 653 | h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR); |
| 654 | if (h->bulk_in < 0) { |
| 655 | D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno); |
| 656 | goto err; |
| 657 | } |
| 658 | |
| 659 | ffs_create_control_thread(h); |
| 660 | return; |
| 661 | |
| 662 | err: |
| 663 | if (h->bulk_in > 0) { |
| 664 | adb_close(h->bulk_in); |
| 665 | h->bulk_in = -1; |
| 666 | } |
| 667 | if (h->bulk_out > 0) { |
| 668 | adb_close(h->bulk_out); |
| 669 | h->bulk_out = -1; |
| 670 | } |
| 671 | if (h->control > 0) { |
| 672 | adb_close(h->control); |
| 673 | h->control = -1; |
| 674 | } |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | static void *usb_ffs_open_thread(void *x) |
| 679 | { |
| 680 | struct usb_handle *usb = (struct usb_handle *)x; |
| 681 | |
| 682 | while (1) { |
| 683 | // wait until the USB device needs opening |
| 684 | adb_mutex_lock(&usb->lock); |
| 685 | while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1) |
| 686 | adb_cond_wait(&usb->notify, &usb->lock); |
| 687 | adb_mutex_unlock(&usb->lock); |
| 688 | |
| 689 | while (1) { |
| 690 | init_functionfs(usb); |
| 691 | |
| 692 | if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0) |
| 693 | break; |
| 694 | |
| 695 | adb_sleep_ms(1000); |
| 696 | } |
| 697 | |
| 698 | D("[ usb_thread - registering device ]\n"); |
| 699 | register_usb_transport(usb, 0, 0, 1); |
| 700 | } |
| 701 | |
| 702 | // never gets here |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | static int bulk_write(int bulk_in, const char *buf, size_t length) |
| 707 | { |
| 708 | size_t count = 0; |
| 709 | int ret; |
| 710 | |
| 711 | do { |
| 712 | ret = adb_write(bulk_in, buf + count, length - count); |
| 713 | if (ret < 0) { |
| 714 | if (errno != EINTR) |
| 715 | return ret; |
| 716 | } else { |
| 717 | count += ret; |
| 718 | } |
| 719 | } while (count < length); |
| 720 | |
| 721 | D("[ bulk_write done fd=%d ]\n", bulk_in); |
| 722 | return count; |
| 723 | } |
| 724 | |
| 725 | static int usb_ffs_write(usb_handle *h, const void *data, int len) |
| 726 | { |
| 727 | int n; |
| 728 | |
| 729 | D("about to write (fd=%d, len=%d)\n", h->bulk_in, len); |
| 730 | n = bulk_write(h->bulk_in, data, len); |
| 731 | if (n != len) { |
| 732 | D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", |
| 733 | h->bulk_in, n, errno, strerror(errno)); |
| 734 | return -1; |
| 735 | } |
| 736 | D("[ done fd=%d ]\n", h->bulk_in); |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | static int bulk_read(int bulk_out, char *buf, size_t length) |
| 741 | { |
| 742 | size_t count = 0; |
| 743 | int ret; |
| 744 | |
| 745 | do { |
| 746 | ret = adb_read(bulk_out, buf + count, length - count); |
| 747 | if (ret < 0) { |
| 748 | if (errno != EINTR) { |
| 749 | D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n", |
| 750 | bulk_out, length, count); |
| 751 | return ret; |
| 752 | } |
| 753 | } else { |
| 754 | count += ret; |
| 755 | } |
| 756 | } while (count < length); |
| 757 | |
| 758 | return count; |
| 759 | } |
| 760 | |
| 761 | static int usb_ffs_read(usb_handle *h, void *data, int len) |
| 762 | { |
| 763 | int n; |
| 764 | |
| 765 | D("about to read (fd=%d, len=%d)\n", h->bulk_out, len); |
| 766 | n = bulk_read(h->bulk_out, data, len); |
qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 767 | if (h->ffs_control_suspend_mode) { |
| 768 | if (n < 0) { |
| 769 | int ret_err = -1 * errno; |
| 770 | D("Suspended bulk_read() ERROR: fd = %d, n = %d, errno = %d (%s) ret_err=%d\n", |
| 771 | h->bulk_out, n, errno, strerror(errno), ret_err); |
| 772 | return ret_err; |
| 773 | } else { |
| 774 | D("Suspended bulk_read: fd = %d, n = %d, errno = %d (%s) len:%d data:%s\n", |
| 775 | h->bulk_out, n, errno, strerror(errno), len, data); |
| 776 | } |
| 777 | } else if (n != len) { |
rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 778 | D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", |
| 779 | h->bulk_out, n, errno, strerror(errno)); |
| 780 | return -1; |
| 781 | } |
qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 782 | h->ffs_control_suspend_mode = false; |
rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 783 | D("[ done fd=%d ]\n", h->bulk_out); |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static void usb_ffs_kick(usb_handle *h) |
| 788 | { |
| 789 | int err; |
| 790 | |
| 791 | err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT); |
| 792 | if (err < 0) |
| 793 | D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno); |
| 794 | |
| 795 | err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT); |
| 796 | if (err < 0) |
| 797 | D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno); |
| 798 | |
| 799 | adb_mutex_lock(&h->lock); |
| 800 | |
| 801 | // don't close ep0 here, since we may not need to reinitialize it with |
| 802 | // the same descriptors again. if however ep1/ep2 fail to re-open in |
| 803 | // init_functionfs, only then would we close and open ep0 again. |
| 804 | adb_close(h->bulk_out); |
| 805 | adb_close(h->bulk_in); |
| 806 | h->bulk_out = h->bulk_in = -1; |
| 807 | |
| 808 | // notify usb_ffs_open_thread that we are disconnected |
| 809 | adb_cond_signal(&h->notify); |
| 810 | adb_mutex_unlock(&h->lock); |
| 811 | } |
| 812 | |
| 813 | static void usb_ffs_init() |
| 814 | { |
| 815 | usb_handle *h; |
| 816 | adb_thread_t tid; |
| 817 | |
| 818 | D("[ usb_init - using FunctionFS ]\n"); |
| 819 | |
| 820 | h = calloc(1, sizeof(usb_handle)); |
| 821 | if (h == 0) { |
| 822 | fatal("cannot allocate usb_handle"); |
| 823 | return; |
| 824 | } |
| 825 | |
| 826 | h->write = usb_ffs_write; |
| 827 | h->read = usb_ffs_read; |
| 828 | h->kick = usb_ffs_kick; |
| 829 | |
| 830 | h->control = -1; |
| 831 | h->bulk_out = -1; |
| 832 | h->bulk_out = -1; |
| 833 | h->ffs_control_thread_created = false; |
| 834 | |
| 835 | adb_cond_init(&h->notify, 0); |
| 836 | adb_mutex_init(&h->lock, 0); |
qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 837 | adb_mutex_init(&g_wake_lock, 0); |
rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 838 | |
| 839 | D("[ usb_init - starting thread ]\n"); |
| 840 | if (adb_thread_create(&tid, usb_ffs_open_thread, h)){ |
| 841 | fatal_errno("[ cannot create usb thread ]\n"); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | void usb_init() |
| 846 | { |
| 847 | if (access(USB_FFS_ADB_EP0, F_OK) == 0) |
| 848 | usb_ffs_init(); |
| 849 | else |
| 850 | usb_adb_init(); |
| 851 | } |
| 852 | |
| 853 | void usb_cleanup() |
| 854 | { |
| 855 | } |
| 856 | |
| 857 | int usb_write(usb_handle *h, const void *data, int len) |
| 858 | { |
| 859 | return h->write(h, data, len); |
| 860 | } |
| 861 | |
| 862 | int usb_read(usb_handle *h, void *data, int len) |
| 863 | { |
| 864 | return h->read(h, data, len); |
| 865 | } |
| 866 | int usb_close(usb_handle *h) |
| 867 | { |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | void usb_kick(usb_handle *h) |
| 872 | { |
| 873 | h->kick(h); |
| 874 | } |