Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #define TRACE_TAG USB |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
| 21 | #include <errno.h> |
Josh Gao | a4dfc14 | 2020-02-19 13:50:57 -0800 | [diff] [blame] | 22 | #include <inttypes.h> |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <linux/usb/functionfs.h> |
| 31 | #include <sys/eventfd.h> |
| 32 | |
Josh Gao | b9727b0 | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 33 | #include <algorithm> |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 34 | #include <array> |
| 35 | #include <future> |
| 36 | #include <memory> |
| 37 | #include <mutex> |
| 38 | #include <optional> |
| 39 | #include <vector> |
| 40 | |
| 41 | #include <asyncio/AsyncIO.h> |
| 42 | |
| 43 | #include <android-base/logging.h> |
| 44 | #include <android-base/macros.h> |
Josh Gao | a42a56d | 2021-03-22 13:56:40 -0700 | [diff] [blame] | 45 | #include <android-base/parsebool.h> |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 46 | #include <android-base/properties.h> |
| 47 | #include <android-base/thread_annotations.h> |
| 48 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 49 | #include "adb_unique_fd.h" |
| 50 | #include "adb_utils.h" |
Josh Gao | a42a56d | 2021-03-22 13:56:40 -0700 | [diff] [blame] | 51 | #include "daemon/property_monitor.h" |
Josh Gao | 6b55e75 | 2020-03-27 18:09:56 -0700 | [diff] [blame] | 52 | #include "daemon/usb_ffs.h" |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 53 | #include "sysdeps/chrono.h" |
Josh Gao | d5417bc | 2021-04-27 20:32:01 -0700 | [diff] [blame] | 54 | #include "transfer_id.h" |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 55 | #include "transport.h" |
| 56 | #include "types.h" |
| 57 | |
| 58 | using android::base::StringPrintf; |
| 59 | |
Josh Gao | 641c708 | 2019-04-16 11:20:04 -0700 | [diff] [blame] | 60 | // Not all USB controllers support operations larger than 16k, so don't go above that. |
Josh Gao | 3b4be35 | 2019-04-24 14:28:25 -0700 | [diff] [blame] | 61 | // Also, each submitted operation does an allocation in the kernel of that size, so we want to |
| 62 | // minimize our queue depth while still maintaining a deep enough queue to keep the USB stack fed. |
| 63 | static constexpr size_t kUsbReadQueueDepth = 8; |
Josh Gao | 641c708 | 2019-04-16 11:20:04 -0700 | [diff] [blame] | 64 | static constexpr size_t kUsbReadSize = 4 * PAGE_SIZE; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 65 | |
Josh Gao | 3b4be35 | 2019-04-24 14:28:25 -0700 | [diff] [blame] | 66 | static constexpr size_t kUsbWriteQueueDepth = 8; |
Josh Gao | 641c708 | 2019-04-16 11:20:04 -0700 | [diff] [blame] | 67 | static constexpr size_t kUsbWriteSize = 4 * PAGE_SIZE; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 68 | |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 69 | static const char* to_string(enum usb_functionfs_event_type type) { |
| 70 | switch (type) { |
| 71 | case FUNCTIONFS_BIND: |
| 72 | return "FUNCTIONFS_BIND"; |
| 73 | case FUNCTIONFS_UNBIND: |
| 74 | return "FUNCTIONFS_UNBIND"; |
| 75 | case FUNCTIONFS_ENABLE: |
| 76 | return "FUNCTIONFS_ENABLE"; |
| 77 | case FUNCTIONFS_DISABLE: |
| 78 | return "FUNCTIONFS_DISABLE"; |
| 79 | case FUNCTIONFS_SETUP: |
| 80 | return "FUNCTIONFS_SETUP"; |
| 81 | case FUNCTIONFS_SUSPEND: |
| 82 | return "FUNCTIONFS_SUSPEND"; |
| 83 | case FUNCTIONFS_RESUME: |
| 84 | return "FUNCTIONFS_RESUME"; |
| 85 | } |
| 86 | } |
| 87 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 88 | template <class Payload> |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 89 | struct IoBlock { |
Josh Gao | 9f60846 | 2019-03-18 14:11:28 -0700 | [diff] [blame] | 90 | bool pending = false; |
Evgenii Stepanov | 6e03b68 | 2019-05-15 18:45:01 -0700 | [diff] [blame] | 91 | struct iocb control = {}; |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 92 | Payload payload; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 93 | |
| 94 | TransferId id() const { return TransferId::from_value(control.aio_data); } |
| 95 | }; |
| 96 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 97 | using IoReadBlock = IoBlock<Block>; |
| 98 | using IoWriteBlock = IoBlock<std::shared_ptr<Block>>; |
| 99 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 100 | struct ScopedAioContext { |
| 101 | ScopedAioContext() = default; |
| 102 | ~ScopedAioContext() { reset(); } |
| 103 | |
| 104 | ScopedAioContext(ScopedAioContext&& move) { reset(move.release()); } |
| 105 | ScopedAioContext(const ScopedAioContext& copy) = delete; |
| 106 | |
| 107 | ScopedAioContext& operator=(ScopedAioContext&& move) { |
| 108 | reset(move.release()); |
| 109 | return *this; |
| 110 | } |
| 111 | ScopedAioContext& operator=(const ScopedAioContext& copy) = delete; |
| 112 | |
| 113 | static ScopedAioContext Create(size_t max_events) { |
| 114 | aio_context_t ctx = 0; |
| 115 | if (io_setup(max_events, &ctx) != 0) { |
| 116 | PLOG(FATAL) << "failed to create aio_context_t"; |
| 117 | } |
| 118 | ScopedAioContext result; |
| 119 | result.reset(ctx); |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | aio_context_t release() { |
| 124 | aio_context_t result = context_; |
| 125 | context_ = 0; |
| 126 | return result; |
| 127 | } |
| 128 | |
| 129 | void reset(aio_context_t new_context = 0) { |
| 130 | if (context_ != 0) { |
| 131 | io_destroy(context_); |
| 132 | } |
| 133 | |
| 134 | context_ = new_context; |
| 135 | } |
| 136 | |
| 137 | aio_context_t get() { return context_; } |
| 138 | |
| 139 | private: |
| 140 | aio_context_t context_ = 0; |
| 141 | }; |
| 142 | |
| 143 | struct UsbFfsConnection : public Connection { |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 144 | UsbFfsConnection(unique_fd control, unique_fd read, unique_fd write, |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 145 | std::promise<void> destruction_notifier) |
Josh Gao | dcf3688 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 146 | : worker_started_(false), |
| 147 | stopped_(false), |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 148 | destruction_notifier_(std::move(destruction_notifier)), |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 149 | control_fd_(std::move(control)), |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 150 | read_fd_(std::move(read)), |
| 151 | write_fd_(std::move(write)) { |
| 152 | LOG(INFO) << "UsbFfsConnection constructed"; |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 153 | worker_event_fd_.reset(eventfd(0, EFD_CLOEXEC)); |
| 154 | if (worker_event_fd_ == -1) { |
| 155 | PLOG(FATAL) << "failed to create eventfd"; |
| 156 | } |
| 157 | |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 158 | monitor_event_fd_.reset(eventfd(0, EFD_CLOEXEC)); |
| 159 | if (monitor_event_fd_ == -1) { |
| 160 | PLOG(FATAL) << "failed to create eventfd"; |
| 161 | } |
| 162 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 163 | aio_context_ = ScopedAioContext::Create(kUsbReadQueueDepth + kUsbWriteQueueDepth); |
| 164 | } |
| 165 | |
| 166 | ~UsbFfsConnection() { |
| 167 | LOG(INFO) << "UsbFfsConnection being destroyed"; |
| 168 | Stop(); |
| 169 | monitor_thread_.join(); |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 170 | |
| 171 | // We need to explicitly close our file descriptors before we notify our destruction, |
| 172 | // because the thread listening on the future will immediately try to reopen the endpoint. |
Josh Gao | dcf3688 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 173 | aio_context_.reset(); |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 174 | control_fd_.reset(); |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 175 | read_fd_.reset(); |
| 176 | write_fd_.reset(); |
| 177 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 178 | destruction_notifier_.set_value(); |
| 179 | } |
| 180 | |
| 181 | virtual bool Write(std::unique_ptr<apacket> packet) override final { |
| 182 | LOG(DEBUG) << "USB write: " << dump_header(&packet->msg); |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 183 | auto header = std::make_shared<Block>(sizeof(packet->msg)); |
| 184 | memcpy(header->data(), &packet->msg, sizeof(packet->msg)); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 185 | |
| 186 | std::lock_guard<std::mutex> lock(write_mutex_); |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 187 | write_requests_.push_back( |
| 188 | CreateWriteBlock(std::move(header), 0, sizeof(packet->msg), next_write_id_++)); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 189 | if (!packet->payload.empty()) { |
Josh Gao | b9727b0 | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 190 | // The kernel attempts to allocate a contiguous block of memory for each write, |
| 191 | // which can fail if the write is large and the kernel heap is fragmented. |
| 192 | // Split large writes into smaller chunks to avoid this. |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 193 | auto payload = std::make_shared<Block>(std::move(packet->payload)); |
Josh Gao | b9727b0 | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 194 | size_t offset = 0; |
| 195 | size_t len = payload->size(); |
| 196 | |
| 197 | while (len > 0) { |
| 198 | size_t write_size = std::min(kUsbWriteSize, len); |
| 199 | write_requests_.push_back( |
| 200 | CreateWriteBlock(payload, offset, write_size, next_write_id_++)); |
| 201 | len -= write_size; |
| 202 | offset += write_size; |
| 203 | } |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 204 | } |
Josh Gao | 58cc515 | 2020-05-19 15:57:06 -0700 | [diff] [blame] | 205 | |
| 206 | // Wake up the worker thread to submit writes. |
| 207 | uint64_t notify = 1; |
| 208 | ssize_t rc = adb_write(worker_event_fd_.get(), ¬ify, sizeof(notify)); |
| 209 | if (rc < 0) { |
| 210 | PLOG(FATAL) << "failed to notify worker eventfd to submit writes"; |
| 211 | } |
| 212 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 213 | return true; |
| 214 | } |
| 215 | |
| 216 | virtual void Start() override final { StartMonitor(); } |
| 217 | |
| 218 | virtual void Stop() override final { |
| 219 | if (stopped_.exchange(true)) { |
| 220 | return; |
| 221 | } |
| 222 | stopped_ = true; |
| 223 | uint64_t notify = 1; |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 224 | ssize_t rc = adb_write(worker_event_fd_.get(), ¬ify, sizeof(notify)); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 225 | if (rc < 0) { |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 226 | PLOG(FATAL) << "failed to notify worker eventfd to stop UsbFfsConnection"; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 227 | } |
| 228 | CHECK_EQ(static_cast<size_t>(rc), sizeof(notify)); |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 229 | |
| 230 | rc = adb_write(monitor_event_fd_.get(), ¬ify, sizeof(notify)); |
| 231 | if (rc < 0) { |
| 232 | PLOG(FATAL) << "failed to notify monitor eventfd to stop UsbFfsConnection"; |
| 233 | } |
| 234 | |
| 235 | CHECK_EQ(static_cast<size_t>(rc), sizeof(notify)); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 238 | virtual bool DoTlsHandshake(RSA* key, std::string* auth_key) override final { |
| 239 | // TODO: support TLS for usb connections. |
| 240 | LOG(FATAL) << "Not supported yet."; |
| 241 | return false; |
| 242 | } |
| 243 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 244 | private: |
| 245 | void StartMonitor() { |
| 246 | // This is a bit of a mess. |
| 247 | // It's possible for io_submit to end up blocking, if we call it as the endpoint |
| 248 | // becomes disabled. Work around this by having a monitor thread to listen for functionfs |
| 249 | // lifecycle events. If we notice an error condition (either we've become disabled, or we |
| 250 | // were never enabled in the first place), we send interruption signals to the worker thread |
| 251 | // until it dies, and then report failure to the transport via HandleError, which will |
| 252 | // eventually result in the transport being destroyed, which will result in UsbFfsConnection |
| 253 | // being destroyed, which unblocks the open thread and restarts this entire process. |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 254 | static std::once_flag handler_once; |
| 255 | std::call_once(handler_once, []() { signal(kInterruptionSignal, [](int) {}); }); |
| 256 | |
| 257 | monitor_thread_ = std::thread([this]() { |
| 258 | adb_thread_setname("UsbFfs-monitor"); |
Josh Gao | bd4fb2d | 2020-02-26 15:44:30 -0800 | [diff] [blame] | 259 | LOG(INFO) << "UsbFfs-monitor thread spawned"; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 260 | |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 261 | bool bound = false; |
Josh Gao | 961496c | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 262 | bool enabled = false; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 263 | bool running = true; |
| 264 | while (running) { |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 265 | adb_pollfd pfd[2] = { |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 266 | { .fd = control_fd_.get(), .events = POLLIN, .revents = 0 }, |
| 267 | { .fd = monitor_event_fd_.get(), .events = POLLIN, .revents = 0 }, |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 268 | }; |
Josh Gao | dcf3688 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 269 | |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 270 | // If we don't see our first bind within a second, try again. |
| 271 | int timeout_ms = bound ? -1 : 1000; |
| 272 | |
| 273 | int rc = TEMP_FAILURE_RETRY(adb_poll(pfd, 2, timeout_ms)); |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 274 | if (rc == -1) { |
| 275 | PLOG(FATAL) << "poll on USB control fd failed"; |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 276 | } else if (rc == 0) { |
| 277 | LOG(WARNING) << "timed out while waiting for FUNCTIONFS_BIND, trying again"; |
| 278 | break; |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (pfd[1].revents) { |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 282 | // We were told to die. |
| 283 | break; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | struct usb_functionfs_event event; |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 287 | rc = TEMP_FAILURE_RETRY(adb_read(control_fd_.get(), &event, sizeof(event))); |
Josh Gao | 370dbb0 | 2019-05-10 11:37:34 -0700 | [diff] [blame] | 288 | if (rc == -1) { |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 289 | PLOG(FATAL) << "failed to read functionfs event"; |
Josh Gao | 370dbb0 | 2019-05-10 11:37:34 -0700 | [diff] [blame] | 290 | } else if (rc == 0) { |
| 291 | LOG(WARNING) << "hit EOF on functionfs control fd"; |
| 292 | break; |
| 293 | } else if (rc != sizeof(event)) { |
| 294 | LOG(FATAL) << "read functionfs event of unexpected size, expected " |
| 295 | << sizeof(event) << ", got " << rc; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | LOG(INFO) << "USB event: " |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 299 | << to_string(static_cast<usb_functionfs_event_type>(event.type)); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 300 | |
| 301 | switch (event.type) { |
| 302 | case FUNCTIONFS_BIND: |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 303 | if (bound) { |
| 304 | LOG(WARNING) << "received FUNCTIONFS_BIND while already bound?"; |
| 305 | running = false; |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | if (enabled) { |
| 310 | LOG(WARNING) << "received FUNCTIONFS_BIND while already enabled?"; |
| 311 | running = false; |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | bound = true; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 316 | break; |
| 317 | |
| 318 | case FUNCTIONFS_ENABLE: |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 319 | if (!bound) { |
| 320 | LOG(WARNING) << "received FUNCTIONFS_ENABLE while not bound?"; |
| 321 | running = false; |
| 322 | break; |
| 323 | } |
| 324 | |
Josh Gao | e3fa0c8 | 2019-03-28 11:05:53 -0700 | [diff] [blame] | 325 | if (enabled) { |
| 326 | LOG(WARNING) << "received FUNCTIONFS_ENABLE while already enabled?"; |
| 327 | running = false; |
Josh Gao | 56463fc | 2019-05-01 16:53:53 -0700 | [diff] [blame] | 328 | break; |
Josh Gao | e3fa0c8 | 2019-03-28 11:05:53 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | enabled = true; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 332 | StartWorker(); |
| 333 | break; |
| 334 | |
| 335 | case FUNCTIONFS_DISABLE: |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 336 | if (!bound) { |
| 337 | LOG(WARNING) << "received FUNCTIONFS_DISABLE while not bound?"; |
| 338 | } |
| 339 | |
Josh Gao | e3fa0c8 | 2019-03-28 11:05:53 -0700 | [diff] [blame] | 340 | if (!enabled) { |
| 341 | LOG(WARNING) << "received FUNCTIONFS_DISABLE while not enabled?"; |
| 342 | } |
| 343 | |
| 344 | enabled = false; |
Josh Gao | 961496c | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 345 | running = false; |
| 346 | break; |
| 347 | |
| 348 | case FUNCTIONFS_UNBIND: |
Josh Gao | e3fa0c8 | 2019-03-28 11:05:53 -0700 | [diff] [blame] | 349 | if (enabled) { |
| 350 | LOG(WARNING) << "received FUNCTIONFS_UNBIND while still enabled?"; |
| 351 | } |
Josh Gao | 961496c | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 352 | |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 353 | if (!bound) { |
| 354 | LOG(WARNING) << "received FUNCTIONFS_UNBIND when not bound?"; |
| 355 | } |
| 356 | |
| 357 | bound = false; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 358 | running = false; |
| 359 | break; |
Josh Gao | 7be217e | 2019-05-15 18:03:29 -0700 | [diff] [blame] | 360 | |
| 361 | case FUNCTIONFS_SETUP: { |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 362 | LOG(INFO) << "received FUNCTIONFS_SETUP control transfer: bRequestType = " |
| 363 | << static_cast<int>(event.u.setup.bRequestType) |
| 364 | << ", bRequest = " << static_cast<int>(event.u.setup.bRequest) |
| 365 | << ", wValue = " << static_cast<int>(event.u.setup.wValue) |
| 366 | << ", wIndex = " << static_cast<int>(event.u.setup.wIndex) |
| 367 | << ", wLength = " << static_cast<int>(event.u.setup.wLength); |
| 368 | |
| 369 | if ((event.u.setup.bRequestType & USB_DIR_IN)) { |
| 370 | LOG(INFO) << "acking device-to-host control transfer"; |
| 371 | ssize_t rc = adb_write(control_fd_.get(), "", 0); |
| 372 | if (rc != 0) { |
| 373 | PLOG(ERROR) << "failed to write empty packet to host"; |
| 374 | break; |
| 375 | } |
| 376 | } else { |
| 377 | std::string buf; |
| 378 | buf.resize(event.u.setup.wLength + 1); |
| 379 | |
| 380 | ssize_t rc = adb_read(control_fd_.get(), buf.data(), buf.size()); |
| 381 | if (rc != event.u.setup.wLength) { |
| 382 | LOG(ERROR) |
| 383 | << "read " << rc |
| 384 | << " bytes when trying to read control request, expected " |
| 385 | << event.u.setup.wLength; |
| 386 | } |
| 387 | |
| 388 | LOG(INFO) << "control request contents: " << buf; |
| 389 | break; |
| 390 | } |
Josh Gao | 7be217e | 2019-05-15 18:03:29 -0700 | [diff] [blame] | 391 | } |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Josh Gao | 5f57277 | 2019-02-28 13:29:32 -0800 | [diff] [blame] | 395 | StopWorker(); |
Josh Gao | dcf3688 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 396 | HandleError("monitor thread finished"); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 397 | }); |
| 398 | } |
| 399 | |
| 400 | void StartWorker() { |
Josh Gao | dcf3688 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 401 | CHECK(!worker_started_); |
| 402 | worker_started_ = true; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 403 | worker_thread_ = std::thread([this]() { |
| 404 | adb_thread_setname("UsbFfs-worker"); |
Josh Gao | bd4fb2d | 2020-02-26 15:44:30 -0800 | [diff] [blame] | 405 | LOG(INFO) << "UsbFfs-worker thread spawned"; |
| 406 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 407 | for (size_t i = 0; i < kUsbReadQueueDepth; ++i) { |
| 408 | read_requests_[i] = CreateReadBlock(next_read_id_++); |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 409 | if (!SubmitRead(&read_requests_[i])) { |
| 410 | return; |
| 411 | } |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | while (!stopped_) { |
| 415 | uint64_t dummy; |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 416 | ssize_t rc = adb_read(worker_event_fd_.get(), &dummy, sizeof(dummy)); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 417 | if (rc == -1) { |
| 418 | PLOG(FATAL) << "failed to read from eventfd"; |
| 419 | } else if (rc == 0) { |
| 420 | LOG(FATAL) << "hit EOF on eventfd"; |
| 421 | } |
| 422 | |
Josh Gao | 961496c | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 423 | ReadEvents(); |
Josh Gao | 58cc515 | 2020-05-19 15:57:06 -0700 | [diff] [blame] | 424 | |
| 425 | std::lock_guard<std::mutex> lock(write_mutex_); |
| 426 | SubmitWrites(); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 427 | } |
| 428 | }); |
| 429 | } |
| 430 | |
Josh Gao | 5f57277 | 2019-02-28 13:29:32 -0800 | [diff] [blame] | 431 | void StopWorker() { |
Josh Gao | dcf3688 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 432 | if (!worker_started_) { |
| 433 | return; |
| 434 | } |
| 435 | |
Josh Gao | 5f57277 | 2019-02-28 13:29:32 -0800 | [diff] [blame] | 436 | pthread_t worker_thread_handle = worker_thread_.native_handle(); |
| 437 | while (true) { |
| 438 | int rc = pthread_kill(worker_thread_handle, kInterruptionSignal); |
| 439 | if (rc != 0) { |
| 440 | LOG(ERROR) << "failed to send interruption signal to worker: " << strerror(rc); |
| 441 | break; |
| 442 | } |
| 443 | |
| 444 | std::this_thread::sleep_for(100ms); |
| 445 | |
| 446 | rc = pthread_kill(worker_thread_handle, 0); |
| 447 | if (rc == 0) { |
| 448 | continue; |
| 449 | } else if (rc == ESRCH) { |
| 450 | break; |
| 451 | } else { |
| 452 | LOG(ERROR) << "failed to send interruption signal to worker: " << strerror(rc); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | worker_thread_.join(); |
| 457 | } |
| 458 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 459 | void PrepareReadBlock(IoReadBlock* block, uint64_t id) { |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 460 | block->pending = false; |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 461 | if (block->payload.capacity() >= kUsbReadSize) { |
| 462 | block->payload.resize(kUsbReadSize); |
| 463 | } else { |
| 464 | block->payload = Block(kUsbReadSize); |
| 465 | } |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 466 | block->control.aio_data = static_cast<uint64_t>(TransferId::read(id)); |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 467 | block->control.aio_buf = reinterpret_cast<uintptr_t>(block->payload.data()); |
| 468 | block->control.aio_nbytes = block->payload.size(); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 471 | IoReadBlock CreateReadBlock(uint64_t id) { |
| 472 | IoReadBlock block; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 473 | PrepareReadBlock(&block, id); |
| 474 | block.control.aio_rw_flags = 0; |
| 475 | block.control.aio_lio_opcode = IOCB_CMD_PREAD; |
| 476 | block.control.aio_reqprio = 0; |
| 477 | block.control.aio_fildes = read_fd_.get(); |
| 478 | block.control.aio_offset = 0; |
| 479 | block.control.aio_flags = IOCB_FLAG_RESFD; |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 480 | block.control.aio_resfd = worker_event_fd_.get(); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 481 | return block; |
| 482 | } |
| 483 | |
Josh Gao | 961496c | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 484 | void ReadEvents() { |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 485 | static constexpr size_t kMaxEvents = kUsbReadQueueDepth + kUsbWriteQueueDepth; |
| 486 | struct io_event events[kMaxEvents]; |
| 487 | struct timespec timeout = {.tv_sec = 0, .tv_nsec = 0}; |
| 488 | int rc = io_getevents(aio_context_.get(), 0, kMaxEvents, events, &timeout); |
| 489 | if (rc == -1) { |
| 490 | HandleError(StringPrintf("io_getevents failed while reading: %s", strerror(errno))); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | for (int event_idx = 0; event_idx < rc; ++event_idx) { |
| 495 | auto& event = events[event_idx]; |
| 496 | TransferId id = TransferId::from_value(event.data); |
| 497 | |
| 498 | if (event.res < 0) { |
Josh Gao | 40c84a3 | 2021-05-06 16:31:44 -0700 | [diff] [blame] | 499 | // On initial connection, some clients will send a ClearFeature(HALT) to |
| 500 | // attempt to resynchronize host and device after the adb server is killed. |
| 501 | // On newer device kernels, the reads we've already dispatched will be cancelled. |
| 502 | // Instead of treating this as a failure, which will tear down the interface and |
| 503 | // lead to the client doing the same thing again, just resubmit if this happens |
| 504 | // before we've actually read anything. |
| 505 | if (!connection_started_ && event.res == -EPIPE && |
| 506 | id.direction == TransferDirection::READ) { |
| 507 | uint64_t read_idx = id.id % kUsbReadQueueDepth; |
| 508 | SubmitRead(&read_requests_[read_idx]); |
| 509 | continue; |
| 510 | } else { |
| 511 | std::string error = |
| 512 | StringPrintf("%s %" PRIu64 " failed with error %s", |
| 513 | id.direction == TransferDirection::READ ? "read" : "write", |
| 514 | id.id, strerror(-event.res)); |
| 515 | HandleError(error); |
| 516 | return; |
| 517 | } |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | if (id.direction == TransferDirection::READ) { |
Josh Gao | 40c84a3 | 2021-05-06 16:31:44 -0700 | [diff] [blame] | 521 | connection_started_ = true; |
Josh Gao | 6948244 | 2020-02-28 14:53:56 -0800 | [diff] [blame] | 522 | if (!HandleRead(id, event.res)) { |
| 523 | return; |
| 524 | } |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 525 | } else { |
| 526 | HandleWrite(id); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
Josh Gao | 6948244 | 2020-02-28 14:53:56 -0800 | [diff] [blame] | 531 | bool HandleRead(TransferId id, int64_t size) { |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 532 | uint64_t read_idx = id.id % kUsbReadQueueDepth; |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 533 | IoReadBlock* block = &read_requests_[read_idx]; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 534 | block->pending = false; |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 535 | block->payload.resize(size); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 536 | |
| 537 | // Notification for completed reads can be received out of order. |
| 538 | if (block->id().id != needed_read_id_) { |
| 539 | LOG(VERBOSE) << "read " << block->id().id << " completed while waiting for " |
| 540 | << needed_read_id_; |
Josh Gao | 6948244 | 2020-02-28 14:53:56 -0800 | [diff] [blame] | 541 | return true; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | for (uint64_t id = needed_read_id_;; ++id) { |
| 545 | size_t read_idx = id % kUsbReadQueueDepth; |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 546 | IoReadBlock* current_block = &read_requests_[read_idx]; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 547 | if (current_block->pending) { |
| 548 | break; |
| 549 | } |
Josh Gao | 6948244 | 2020-02-28 14:53:56 -0800 | [diff] [blame] | 550 | if (!ProcessRead(current_block)) { |
| 551 | return false; |
| 552 | } |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 553 | ++needed_read_id_; |
| 554 | } |
Josh Gao | 6948244 | 2020-02-28 14:53:56 -0800 | [diff] [blame] | 555 | |
| 556 | return true; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Josh Gao | 6948244 | 2020-02-28 14:53:56 -0800 | [diff] [blame] | 559 | bool ProcessRead(IoReadBlock* block) { |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 560 | if (!block->payload.empty()) { |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 561 | if (!incoming_header_.has_value()) { |
Josh Gao | 6948244 | 2020-02-28 14:53:56 -0800 | [diff] [blame] | 562 | if (block->payload.size() != sizeof(amessage)) { |
| 563 | HandleError("received packet of unexpected length while reading header"); |
| 564 | return false; |
| 565 | } |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 566 | amessage& msg = incoming_header_.emplace(); |
| 567 | memcpy(&msg, block->payload.data(), sizeof(msg)); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 568 | LOG(DEBUG) << "USB read:" << dump_header(&msg); |
| 569 | incoming_header_ = msg; |
| 570 | } else { |
| 571 | size_t bytes_left = incoming_header_->data_length - incoming_payload_.size(); |
Josh Gao | 6948244 | 2020-02-28 14:53:56 -0800 | [diff] [blame] | 572 | if (block->payload.size() > bytes_left) { |
| 573 | HandleError("received too many bytes while waiting for payload"); |
| 574 | return false; |
| 575 | } |
Mayank Rana | 1cc42d3 | 2020-09-11 11:40:00 -0700 | [diff] [blame] | 576 | incoming_payload_.append(std::move(block->payload)); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | if (incoming_header_->data_length == incoming_payload_.size()) { |
| 580 | auto packet = std::make_unique<apacket>(); |
| 581 | packet->msg = *incoming_header_; |
| 582 | |
| 583 | // TODO: Make apacket contain an IOVector so we don't have to coalesce. |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 584 | packet->payload = std::move(incoming_payload_).coalesce(); |
Josh Gao | 7852ca4 | 2021-06-17 04:17:25 -0700 | [diff] [blame] | 585 | transport_->HandleRead(std::move(packet)); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 586 | |
| 587 | incoming_header_.reset(); |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 588 | // reuse the capacity of the incoming payload while we can. |
| 589 | auto free_block = incoming_payload_.clear(); |
| 590 | if (block->payload.capacity() == 0) { |
| 591 | block->payload = std::move(free_block); |
| 592 | } |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
| 596 | PrepareReadBlock(block, block->id().id + kUsbReadQueueDepth); |
| 597 | SubmitRead(block); |
Josh Gao | 6948244 | 2020-02-28 14:53:56 -0800 | [diff] [blame] | 598 | return true; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 601 | bool SubmitRead(IoReadBlock* block) { |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 602 | block->pending = true; |
| 603 | struct iocb* iocb = &block->control; |
| 604 | if (io_submit(aio_context_.get(), 1, &iocb) != 1) { |
| 605 | HandleError(StringPrintf("failed to submit read: %s", strerror(errno))); |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 606 | return false; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 607 | } |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 608 | |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 609 | return true; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | void HandleWrite(TransferId id) { |
| 613 | std::lock_guard<std::mutex> lock(write_mutex_); |
| 614 | auto it = |
| 615 | std::find_if(write_requests_.begin(), write_requests_.end(), [id](const auto& req) { |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 616 | return static_cast<uint64_t>(req.id()) == static_cast<uint64_t>(id); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 617 | }); |
| 618 | CHECK(it != write_requests_.end()); |
| 619 | |
| 620 | write_requests_.erase(it); |
| 621 | size_t outstanding_writes = --writes_submitted_; |
| 622 | LOG(DEBUG) << "USB write: reaped, down to " << outstanding_writes; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 625 | IoWriteBlock CreateWriteBlock(std::shared_ptr<Block> payload, size_t offset, size_t len, |
| 626 | uint64_t id) { |
| 627 | auto block = IoWriteBlock(); |
| 628 | block.payload = std::move(payload); |
| 629 | block.control.aio_data = static_cast<uint64_t>(TransferId::write(id)); |
| 630 | block.control.aio_rw_flags = 0; |
| 631 | block.control.aio_lio_opcode = IOCB_CMD_PWRITE; |
| 632 | block.control.aio_reqprio = 0; |
| 633 | block.control.aio_fildes = write_fd_.get(); |
| 634 | block.control.aio_buf = reinterpret_cast<uintptr_t>(block.payload->data() + offset); |
| 635 | block.control.aio_nbytes = len; |
| 636 | block.control.aio_offset = 0; |
| 637 | block.control.aio_flags = IOCB_FLAG_RESFD; |
| 638 | block.control.aio_resfd = worker_event_fd_.get(); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 639 | return block; |
| 640 | } |
| 641 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 642 | IoWriteBlock CreateWriteBlock(Block&& payload, uint64_t id) { |
| 643 | size_t len = payload.size(); |
| 644 | return CreateWriteBlock(std::make_shared<Block>(std::move(payload)), 0, len, id); |
Josh Gao | b9727b0 | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 647 | void SubmitWrites() REQUIRES(write_mutex_) { |
| 648 | if (writes_submitted_ == kUsbWriteQueueDepth) { |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | ssize_t writes_to_submit = std::min(kUsbWriteQueueDepth - writes_submitted_, |
| 653 | write_requests_.size() - writes_submitted_); |
| 654 | CHECK_GE(writes_to_submit, 0); |
| 655 | if (writes_to_submit == 0) { |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | struct iocb* iocbs[kUsbWriteQueueDepth]; |
| 660 | for (int i = 0; i < writes_to_submit; ++i) { |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 661 | CHECK(!write_requests_[writes_submitted_ + i].pending); |
| 662 | write_requests_[writes_submitted_ + i].pending = true; |
| 663 | iocbs[i] = &write_requests_[writes_submitted_ + i].control; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 664 | LOG(VERBOSE) << "submitting write_request " << static_cast<void*>(iocbs[i]); |
| 665 | } |
| 666 | |
Josh Gao | cc14d39 | 2019-03-26 13:06:38 -0700 | [diff] [blame] | 667 | writes_submitted_ += writes_to_submit; |
| 668 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 669 | int rc = io_submit(aio_context_.get(), writes_to_submit, iocbs); |
| 670 | if (rc == -1) { |
| 671 | HandleError(StringPrintf("failed to submit write requests: %s", strerror(errno))); |
| 672 | return; |
| 673 | } else if (rc != writes_to_submit) { |
| 674 | LOG(FATAL) << "failed to submit all writes: wanted to submit " << writes_to_submit |
| 675 | << ", actually submitted " << rc; |
| 676 | } |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void HandleError(const std::string& error) { |
| 680 | std::call_once(error_flag_, [&]() { |
Josh Gao | 7852ca4 | 2021-06-17 04:17:25 -0700 | [diff] [blame] | 681 | if (transport_) { |
| 682 | transport_->HandleError(error); |
| 683 | } |
| 684 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 685 | if (!stopped_) { |
| 686 | Stop(); |
| 687 | } |
| 688 | }); |
| 689 | } |
| 690 | |
| 691 | std::thread monitor_thread_; |
Josh Gao | dcf3688 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 692 | |
| 693 | bool worker_started_; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 694 | std::thread worker_thread_; |
| 695 | |
| 696 | std::atomic<bool> stopped_; |
| 697 | std::promise<void> destruction_notifier_; |
| 698 | std::once_flag error_flag_; |
| 699 | |
Josh Gao | b8afeea | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 700 | unique_fd worker_event_fd_; |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 701 | unique_fd monitor_event_fd_; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 702 | |
| 703 | ScopedAioContext aio_context_; |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 704 | unique_fd control_fd_; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 705 | unique_fd read_fd_; |
| 706 | unique_fd write_fd_; |
| 707 | |
Josh Gao | 40c84a3 | 2021-05-06 16:31:44 -0700 | [diff] [blame] | 708 | bool connection_started_ = false; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 709 | std::optional<amessage> incoming_header_; |
| 710 | IOVector incoming_payload_; |
| 711 | |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 712 | std::array<IoReadBlock, kUsbReadQueueDepth> read_requests_; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 713 | IOVector read_data_; |
| 714 | |
| 715 | // ID of the next request that we're going to send out. |
| 716 | size_t next_read_id_ = 0; |
| 717 | |
| 718 | // ID of the next packet we're waiting for. |
| 719 | size_t needed_read_id_ = 0; |
| 720 | |
| 721 | std::mutex write_mutex_; |
Yurii Zubrytskyi | e5e6b0d | 2019-07-12 14:11:54 -0700 | [diff] [blame] | 722 | std::deque<IoWriteBlock> write_requests_ GUARDED_BY(write_mutex_); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 723 | size_t next_write_id_ GUARDED_BY(write_mutex_) = 0; |
| 724 | size_t writes_submitted_ GUARDED_BY(write_mutex_) = 0; |
Josh Gao | 5f57277 | 2019-02-28 13:29:32 -0800 | [diff] [blame] | 725 | |
| 726 | static constexpr int kInterruptionSignal = SIGUSR1; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 727 | }; |
| 728 | |
| 729 | static void usb_ffs_open_thread() { |
| 730 | adb_thread_setname("usb ffs open"); |
| 731 | |
Josh Gao | a42a56d | 2021-03-22 13:56:40 -0700 | [diff] [blame] | 732 | // When the device is acting as a USB host, we'll be unable to bind to the USB gadget on kernels |
| 733 | // that don't carry a downstream patch to enable that behavior. |
| 734 | // |
| 735 | // This property is copied from vendor.sys.usb.adb.disabled by an init.rc script. |
| 736 | // |
| 737 | // Note that this property only disables rebinding the USB gadget: setting it while an interface |
| 738 | // is already bound will do nothing. |
| 739 | static const char* kPropertyUsbDisabled = "sys.usb.adb.disabled"; |
| 740 | PropertyMonitor prop_mon; |
| 741 | prop_mon.Add(kPropertyUsbDisabled, [](std::string value) { |
| 742 | // Return false (i.e. break out of PropertyMonitor::Run) when the property != 1. |
| 743 | return android::base::ParseBool(value) == android::base::ParseBoolResult::kTrue; |
| 744 | }); |
| 745 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 746 | while (true) { |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 747 | unique_fd control; |
| 748 | unique_fd bulk_out; |
| 749 | unique_fd bulk_in; |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 750 | if (!open_functionfs(&control, &bulk_out, &bulk_in)) { |
| 751 | std::this_thread::sleep_for(1s); |
| 752 | continue; |
| 753 | } |
| 754 | |
Jack Pham | e44a4e7 | 2021-05-24 21:32:50 -0700 | [diff] [blame] | 755 | if (android::base::GetBoolProperty(kPropertyUsbDisabled, false)) { |
| 756 | LOG(INFO) << "pausing USB due to " << kPropertyUsbDisabled; |
| 757 | prop_mon.Run(); |
| 758 | LOG(INFO) << "resuming USB"; |
| 759 | } |
| 760 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 761 | atransport* transport = new atransport(); |
| 762 | transport->serial = "UsbFfs"; |
| 763 | std::promise<void> destruction_notifier; |
| 764 | std::future<void> future = destruction_notifier.get_future(); |
| 765 | transport->SetConnection(std::make_unique<UsbFfsConnection>( |
Dan Albert | 4c5a66b | 2019-06-24 14:35:35 -0700 | [diff] [blame] | 766 | std::move(control), std::move(bulk_out), std::move(bulk_in), |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 767 | std::move(destruction_notifier))); |
| 768 | register_transport(transport); |
| 769 | future.wait(); |
| 770 | } |
| 771 | } |
| 772 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 773 | void usb_init() { |
Josh Gao | 6b55e75 | 2020-03-27 18:09:56 -0700 | [diff] [blame] | 774 | std::thread(usb_ffs_open_thread).detach(); |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 775 | } |