| Josh Gao | c51726c | 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> | 
 | 22 | #include <stdio.h> | 
 | 23 | #include <stdlib.h> | 
 | 24 | #include <string.h> | 
 | 25 | #include <sys/ioctl.h> | 
 | 26 | #include <sys/types.h> | 
 | 27 | #include <unistd.h> | 
 | 28 |  | 
 | 29 | #include <linux/usb/functionfs.h> | 
 | 30 | #include <sys/eventfd.h> | 
 | 31 |  | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 32 | #include <algorithm> | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 33 | #include <array> | 
 | 34 | #include <future> | 
 | 35 | #include <memory> | 
 | 36 | #include <mutex> | 
 | 37 | #include <optional> | 
 | 38 | #include <vector> | 
 | 39 |  | 
 | 40 | #include <asyncio/AsyncIO.h> | 
 | 41 |  | 
 | 42 | #include <android-base/logging.h> | 
 | 43 | #include <android-base/macros.h> | 
 | 44 | #include <android-base/properties.h> | 
 | 45 | #include <android-base/thread_annotations.h> | 
 | 46 |  | 
 | 47 | #include <adbd/usb.h> | 
 | 48 |  | 
 | 49 | #include "adb_unique_fd.h" | 
 | 50 | #include "adb_utils.h" | 
 | 51 | #include "sysdeps/chrono.h" | 
 | 52 | #include "transport.h" | 
 | 53 | #include "types.h" | 
 | 54 |  | 
 | 55 | using android::base::StringPrintf; | 
 | 56 |  | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 57 | // We can't find out whether we have support for AIO on ffs endpoints until we submit a read. | 
 | 58 | static std::optional<bool> gFfsAioSupported; | 
 | 59 |  | 
| Josh Gao | 770a6a4 | 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 | 5841a96 | 2019-02-28 15:44:05 -0800 | [diff] [blame] | 61 | static constexpr size_t kUsbReadQueueDepth = 32; | 
| Josh Gao | 770a6a4 | 2019-04-16 11:20:04 -0700 | [diff] [blame] | 62 | static constexpr size_t kUsbReadSize = 4 * PAGE_SIZE; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 63 |  | 
| Josh Gao | 5841a96 | 2019-02-28 15:44:05 -0800 | [diff] [blame] | 64 | static constexpr size_t kUsbWriteQueueDepth = 32; | 
| Josh Gao | 770a6a4 | 2019-04-16 11:20:04 -0700 | [diff] [blame] | 65 | static constexpr size_t kUsbWriteSize = 4 * PAGE_SIZE; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 66 |  | 
 | 67 | static const char* to_string(enum usb_functionfs_event_type type) { | 
 | 68 |     switch (type) { | 
 | 69 |         case FUNCTIONFS_BIND: | 
 | 70 |             return "FUNCTIONFS_BIND"; | 
 | 71 |         case FUNCTIONFS_UNBIND: | 
 | 72 |             return "FUNCTIONFS_UNBIND"; | 
 | 73 |         case FUNCTIONFS_ENABLE: | 
 | 74 |             return "FUNCTIONFS_ENABLE"; | 
 | 75 |         case FUNCTIONFS_DISABLE: | 
 | 76 |             return "FUNCTIONFS_DISABLE"; | 
 | 77 |         case FUNCTIONFS_SETUP: | 
 | 78 |             return "FUNCTIONFS_SETUP"; | 
 | 79 |         case FUNCTIONFS_SUSPEND: | 
 | 80 |             return "FUNCTIONFS_SUSPEND"; | 
 | 81 |         case FUNCTIONFS_RESUME: | 
 | 82 |             return "FUNCTIONFS_RESUME"; | 
 | 83 |     } | 
 | 84 | } | 
 | 85 |  | 
 | 86 | enum class TransferDirection : uint64_t { | 
 | 87 |     READ = 0, | 
 | 88 |     WRITE = 1, | 
 | 89 | }; | 
 | 90 |  | 
 | 91 | struct TransferId { | 
 | 92 |     TransferDirection direction : 1; | 
 | 93 |     uint64_t id : 63; | 
 | 94 |  | 
 | 95 |     TransferId() : TransferId(TransferDirection::READ, 0) {} | 
 | 96 |  | 
 | 97 |   private: | 
 | 98 |     TransferId(TransferDirection direction, uint64_t id) : direction(direction), id(id) {} | 
 | 99 |  | 
 | 100 |   public: | 
 | 101 |     explicit operator uint64_t() const { | 
 | 102 |         uint64_t result; | 
 | 103 |         static_assert(sizeof(*this) == sizeof(result)); | 
 | 104 |         memcpy(&result, this, sizeof(*this)); | 
 | 105 |         return result; | 
 | 106 |     } | 
 | 107 |  | 
 | 108 |     static TransferId read(uint64_t id) { return TransferId(TransferDirection::READ, id); } | 
 | 109 |     static TransferId write(uint64_t id) { return TransferId(TransferDirection::WRITE, id); } | 
 | 110 |  | 
 | 111 |     static TransferId from_value(uint64_t value) { | 
 | 112 |         TransferId result; | 
 | 113 |         memcpy(&result, &value, sizeof(value)); | 
 | 114 |         return result; | 
 | 115 |     } | 
 | 116 | }; | 
 | 117 |  | 
 | 118 | struct IoBlock { | 
| Josh Gao | b019574 | 2019-03-18 14:11:28 -0700 | [diff] [blame] | 119 |     bool pending = false; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 120 |     struct iocb control; | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 121 |     std::shared_ptr<Block> payload; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 122 |  | 
 | 123 |     TransferId id() const { return TransferId::from_value(control.aio_data); } | 
 | 124 | }; | 
 | 125 |  | 
 | 126 | struct ScopedAioContext { | 
 | 127 |     ScopedAioContext() = default; | 
 | 128 |     ~ScopedAioContext() { reset(); } | 
 | 129 |  | 
 | 130 |     ScopedAioContext(ScopedAioContext&& move) { reset(move.release()); } | 
 | 131 |     ScopedAioContext(const ScopedAioContext& copy) = delete; | 
 | 132 |  | 
 | 133 |     ScopedAioContext& operator=(ScopedAioContext&& move) { | 
 | 134 |         reset(move.release()); | 
 | 135 |         return *this; | 
 | 136 |     } | 
 | 137 |     ScopedAioContext& operator=(const ScopedAioContext& copy) = delete; | 
 | 138 |  | 
 | 139 |     static ScopedAioContext Create(size_t max_events) { | 
 | 140 |         aio_context_t ctx = 0; | 
 | 141 |         if (io_setup(max_events, &ctx) != 0) { | 
 | 142 |             PLOG(FATAL) << "failed to create aio_context_t"; | 
 | 143 |         } | 
 | 144 |         ScopedAioContext result; | 
 | 145 |         result.reset(ctx); | 
 | 146 |         return result; | 
 | 147 |     } | 
 | 148 |  | 
 | 149 |     aio_context_t release() { | 
 | 150 |         aio_context_t result = context_; | 
 | 151 |         context_ = 0; | 
 | 152 |         return result; | 
 | 153 |     } | 
 | 154 |  | 
 | 155 |     void reset(aio_context_t new_context = 0) { | 
 | 156 |         if (context_ != 0) { | 
 | 157 |             io_destroy(context_); | 
 | 158 |         } | 
 | 159 |  | 
 | 160 |         context_ = new_context; | 
 | 161 |     } | 
 | 162 |  | 
 | 163 |     aio_context_t get() { return context_; } | 
 | 164 |  | 
 | 165 |   private: | 
 | 166 |     aio_context_t context_ = 0; | 
 | 167 | }; | 
 | 168 |  | 
 | 169 | struct UsbFfsConnection : public Connection { | 
 | 170 |     UsbFfsConnection(unique_fd control, unique_fd read, unique_fd write, | 
 | 171 |                      std::promise<void> destruction_notifier) | 
| Josh Gao | 19dc296 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 172 |         : worker_started_(false), | 
 | 173 |           stopped_(false), | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 174 |           destruction_notifier_(std::move(destruction_notifier)), | 
 | 175 |           control_fd_(std::move(control)), | 
 | 176 |           read_fd_(std::move(read)), | 
 | 177 |           write_fd_(std::move(write)) { | 
 | 178 |         LOG(INFO) << "UsbFfsConnection constructed"; | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 179 |         worker_event_fd_.reset(eventfd(0, EFD_CLOEXEC)); | 
 | 180 |         if (worker_event_fd_ == -1) { | 
 | 181 |             PLOG(FATAL) << "failed to create eventfd"; | 
 | 182 |         } | 
 | 183 |  | 
 | 184 |         monitor_event_fd_.reset(eventfd(0, EFD_CLOEXEC)); | 
 | 185 |         if (monitor_event_fd_ == -1) { | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 186 |             PLOG(FATAL) << "failed to create eventfd"; | 
 | 187 |         } | 
 | 188 |  | 
 | 189 |         aio_context_ = ScopedAioContext::Create(kUsbReadQueueDepth + kUsbWriteQueueDepth); | 
 | 190 |     } | 
 | 191 |  | 
 | 192 |     ~UsbFfsConnection() { | 
 | 193 |         LOG(INFO) << "UsbFfsConnection being destroyed"; | 
 | 194 |         Stop(); | 
 | 195 |         monitor_thread_.join(); | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 196 |  | 
 | 197 |         // We need to explicitly close our file descriptors before we notify our destruction, | 
 | 198 |         // because the thread listening on the future will immediately try to reopen the endpoint. | 
| Josh Gao | 19dc296 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 199 |         aio_context_.reset(); | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 200 |         control_fd_.reset(); | 
 | 201 |         read_fd_.reset(); | 
 | 202 |         write_fd_.reset(); | 
 | 203 |  | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 204 |         destruction_notifier_.set_value(); | 
 | 205 |     } | 
 | 206 |  | 
 | 207 |     virtual bool Write(std::unique_ptr<apacket> packet) override final { | 
 | 208 |         LOG(DEBUG) << "USB write: " << dump_header(&packet->msg); | 
 | 209 |         Block header(sizeof(packet->msg)); | 
 | 210 |         memcpy(header.data(), &packet->msg, sizeof(packet->msg)); | 
 | 211 |  | 
 | 212 |         std::lock_guard<std::mutex> lock(write_mutex_); | 
 | 213 |         write_requests_.push_back(CreateWriteBlock(std::move(header), next_write_id_++)); | 
 | 214 |         if (!packet->payload.empty()) { | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 215 |             // The kernel attempts to allocate a contiguous block of memory for each write, | 
 | 216 |             // which can fail if the write is large and the kernel heap is fragmented. | 
 | 217 |             // Split large writes into smaller chunks to avoid this. | 
 | 218 |             std::shared_ptr<Block> payload = std::make_shared<Block>(std::move(packet->payload)); | 
 | 219 |             size_t offset = 0; | 
 | 220 |             size_t len = payload->size(); | 
 | 221 |  | 
 | 222 |             while (len > 0) { | 
 | 223 |                 size_t write_size = std::min(kUsbWriteSize, len); | 
 | 224 |                 write_requests_.push_back( | 
 | 225 |                         CreateWriteBlock(payload, offset, write_size, next_write_id_++)); | 
 | 226 |                 len -= write_size; | 
 | 227 |                 offset += write_size; | 
 | 228 |             } | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 229 |         } | 
 | 230 |         SubmitWrites(); | 
 | 231 |         return true; | 
 | 232 |     } | 
 | 233 |  | 
 | 234 |     virtual void Start() override final { StartMonitor(); } | 
 | 235 |  | 
 | 236 |     virtual void Stop() override final { | 
 | 237 |         if (stopped_.exchange(true)) { | 
 | 238 |             return; | 
 | 239 |         } | 
 | 240 |         stopped_ = true; | 
 | 241 |         uint64_t notify = 1; | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 242 |         ssize_t rc = adb_write(worker_event_fd_.get(), ¬ify, sizeof(notify)); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 243 |         if (rc < 0) { | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 244 |             PLOG(FATAL) << "failed to notify worker eventfd to stop UsbFfsConnection"; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 245 |         } | 
 | 246 |         CHECK_EQ(static_cast<size_t>(rc), sizeof(notify)); | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 247 |  | 
 | 248 |         rc = adb_write(monitor_event_fd_.get(), ¬ify, sizeof(notify)); | 
 | 249 |         if (rc < 0) { | 
 | 250 |             PLOG(FATAL) << "failed to notify monitor eventfd to stop UsbFfsConnection"; | 
 | 251 |         } | 
 | 252 |  | 
 | 253 |         CHECK_EQ(static_cast<size_t>(rc), sizeof(notify)); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 254 |     } | 
 | 255 |  | 
 | 256 |   private: | 
 | 257 |     void StartMonitor() { | 
 | 258 |         // This is a bit of a mess. | 
 | 259 |         // It's possible for io_submit to end up blocking, if we call it as the endpoint | 
 | 260 |         // becomes disabled. Work around this by having a monitor thread to listen for functionfs | 
 | 261 |         // lifecycle events. If we notice an error condition (either we've become disabled, or we | 
 | 262 |         // were never enabled in the first place), we send interruption signals to the worker thread | 
 | 263 |         // until it dies, and then report failure to the transport via HandleError, which will | 
 | 264 |         // eventually result in the transport being destroyed, which will result in UsbFfsConnection | 
 | 265 |         // being destroyed, which unblocks the open thread and restarts this entire process. | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 266 |         static std::once_flag handler_once; | 
 | 267 |         std::call_once(handler_once, []() { signal(kInterruptionSignal, [](int) {}); }); | 
 | 268 |  | 
 | 269 |         monitor_thread_ = std::thread([this]() { | 
 | 270 |             adb_thread_setname("UsbFfs-monitor"); | 
 | 271 |  | 
 | 272 |             bool bound = false; | 
| Josh Gao | 6933d54 | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 273 |             bool enabled = false; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 274 |             bool running = true; | 
 | 275 |             while (running) { | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 276 |                 adb_pollfd pfd[2] = { | 
 | 277 |                   { .fd = control_fd_.get(), .events = POLLIN, .revents = 0 }, | 
 | 278 |                   { .fd = monitor_event_fd_.get(), .events = POLLIN, .revents = 0 }, | 
 | 279 |                 }; | 
| Josh Gao | 19dc296 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 280 |  | 
 | 281 |                 // If we don't see our first bind within a second, try again. | 
 | 282 |                 int timeout_ms = bound ? -1 : 1000; | 
 | 283 |  | 
 | 284 |                 int rc = TEMP_FAILURE_RETRY(adb_poll(pfd, 2, timeout_ms)); | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 285 |                 if (rc == -1) { | 
 | 286 |                     PLOG(FATAL) << "poll on USB control fd failed"; | 
 | 287 |                 } else if (rc == 0) { | 
| Josh Gao | 19dc296 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 288 |                     LOG(WARNING) << "timed out while waiting for FUNCTIONFS_BIND, trying again"; | 
 | 289 |                     break; | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 290 |                 } | 
 | 291 |  | 
 | 292 |                 if (pfd[1].revents) { | 
 | 293 |                     // We were told to die. | 
 | 294 |                     break; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 295 |                 } | 
 | 296 |  | 
 | 297 |                 struct usb_functionfs_event event; | 
 | 298 |                 if (TEMP_FAILURE_RETRY(adb_read(control_fd_.get(), &event, sizeof(event))) != | 
 | 299 |                     sizeof(event)) { | 
 | 300 |                     PLOG(FATAL) << "failed to read functionfs event"; | 
 | 301 |                 } | 
 | 302 |  | 
 | 303 |                 LOG(INFO) << "USB event: " | 
 | 304 |                           << to_string(static_cast<usb_functionfs_event_type>(event.type)); | 
 | 305 |  | 
 | 306 |                 switch (event.type) { | 
 | 307 |                     case FUNCTIONFS_BIND: | 
| Josh Gao | 87afd52 | 2019-03-28 11:05:53 -0700 | [diff] [blame] | 308 |                         if (bound) { | 
 | 309 |                             LOG(WARNING) << "received FUNCTIONFS_BIND while already bound?"; | 
 | 310 |                             running = false; | 
 | 311 |                         } | 
| Josh Gao | 6933d54 | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 312 |  | 
| Josh Gao | 87afd52 | 2019-03-28 11:05:53 -0700 | [diff] [blame] | 313 |                         if (enabled) { | 
 | 314 |                             LOG(WARNING) << "received FUNCTIONFS_BIND while already enabled?"; | 
 | 315 |                             running = false; | 
 | 316 |                         } | 
 | 317 |  | 
 | 318 |                         bound = true; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 319 |                         break; | 
 | 320 |  | 
 | 321 |                     case FUNCTIONFS_ENABLE: | 
| Josh Gao | 87afd52 | 2019-03-28 11:05:53 -0700 | [diff] [blame] | 322 |                         if (!bound) { | 
 | 323 |                             LOG(WARNING) << "received FUNCTIONFS_ENABLE while not bound?"; | 
 | 324 |                             running = false; | 
 | 325 |                         } | 
| Josh Gao | 6933d54 | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 326 |  | 
| Josh Gao | 87afd52 | 2019-03-28 11:05:53 -0700 | [diff] [blame] | 327 |                         if (enabled) { | 
 | 328 |                             LOG(WARNING) << "received FUNCTIONFS_ENABLE while already enabled?"; | 
 | 329 |                             running = false; | 
 | 330 |                         } | 
 | 331 |  | 
 | 332 |                         enabled = true; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 333 |                         StartWorker(); | 
 | 334 |                         break; | 
 | 335 |  | 
 | 336 |                     case FUNCTIONFS_DISABLE: | 
| Josh Gao | 87afd52 | 2019-03-28 11:05:53 -0700 | [diff] [blame] | 337 |                         if (!bound) { | 
 | 338 |                             LOG(WARNING) << "received FUNCTIONFS_DISABLE while not bound?"; | 
 | 339 |                         } | 
| Josh Gao | 6933d54 | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 340 |  | 
| Josh Gao | 87afd52 | 2019-03-28 11:05:53 -0700 | [diff] [blame] | 341 |                         if (!enabled) { | 
 | 342 |                             LOG(WARNING) << "received FUNCTIONFS_DISABLE while not enabled?"; | 
 | 343 |                         } | 
 | 344 |  | 
 | 345 |                         enabled = false; | 
| Josh Gao | 6933d54 | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 346 |                         running = false; | 
 | 347 |                         break; | 
 | 348 |  | 
 | 349 |                     case FUNCTIONFS_UNBIND: | 
| Josh Gao | 87afd52 | 2019-03-28 11:05:53 -0700 | [diff] [blame] | 350 |                         if (enabled) { | 
 | 351 |                             LOG(WARNING) << "received FUNCTIONFS_UNBIND while still enabled?"; | 
 | 352 |                         } | 
| Josh Gao | 6933d54 | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 353 |  | 
| Josh Gao | 87afd52 | 2019-03-28 11:05:53 -0700 | [diff] [blame] | 354 |                         if (!bound) { | 
 | 355 |                             LOG(WARNING) << "received FUNCTIONFS_UNBIND when not bound?"; | 
 | 356 |                         } | 
 | 357 |  | 
 | 358 |                         bound = false; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 359 |                         running = false; | 
 | 360 |                         break; | 
 | 361 |                 } | 
 | 362 |             } | 
 | 363 |  | 
| Josh Gao | e778b3a | 2019-02-28 13:29:32 -0800 | [diff] [blame] | 364 |             StopWorker(); | 
| Josh Gao | 19dc296 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 365 |             HandleError("monitor thread finished"); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 366 |         }); | 
 | 367 |     } | 
 | 368 |  | 
 | 369 |     void StartWorker() { | 
| Josh Gao | 19dc296 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 370 |         CHECK(!worker_started_); | 
 | 371 |         worker_started_ = true; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 372 |         worker_thread_ = std::thread([this]() { | 
 | 373 |             adb_thread_setname("UsbFfs-worker"); | 
 | 374 |             for (size_t i = 0; i < kUsbReadQueueDepth; ++i) { | 
 | 375 |                 read_requests_[i] = CreateReadBlock(next_read_id_++); | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 376 |                 if (!SubmitRead(&read_requests_[i])) { | 
 | 377 |                     return; | 
 | 378 |                 } | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 379 |             } | 
 | 380 |  | 
 | 381 |             while (!stopped_) { | 
 | 382 |                 uint64_t dummy; | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 383 |                 ssize_t rc = adb_read(worker_event_fd_.get(), &dummy, sizeof(dummy)); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 384 |                 if (rc == -1) { | 
 | 385 |                     PLOG(FATAL) << "failed to read from eventfd"; | 
 | 386 |                 } else if (rc == 0) { | 
 | 387 |                     LOG(FATAL) << "hit EOF on eventfd"; | 
 | 388 |                 } | 
 | 389 |  | 
| Josh Gao | 6933d54 | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 390 |                 ReadEvents(); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 391 |             } | 
 | 392 |         }); | 
 | 393 |     } | 
 | 394 |  | 
| Josh Gao | e778b3a | 2019-02-28 13:29:32 -0800 | [diff] [blame] | 395 |     void StopWorker() { | 
| Josh Gao | 19dc296 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 396 |         if (!worker_started_) { | 
 | 397 |             return; | 
 | 398 |         } | 
 | 399 |  | 
| Josh Gao | e778b3a | 2019-02-28 13:29:32 -0800 | [diff] [blame] | 400 |         pthread_t worker_thread_handle = worker_thread_.native_handle(); | 
 | 401 |         while (true) { | 
 | 402 |             int rc = pthread_kill(worker_thread_handle, kInterruptionSignal); | 
 | 403 |             if (rc != 0) { | 
 | 404 |                 LOG(ERROR) << "failed to send interruption signal to worker: " << strerror(rc); | 
 | 405 |                 break; | 
 | 406 |             } | 
 | 407 |  | 
 | 408 |             std::this_thread::sleep_for(100ms); | 
 | 409 |  | 
 | 410 |             rc = pthread_kill(worker_thread_handle, 0); | 
 | 411 |             if (rc == 0) { | 
 | 412 |                 continue; | 
 | 413 |             } else if (rc == ESRCH) { | 
 | 414 |                 break; | 
 | 415 |             } else { | 
 | 416 |                 LOG(ERROR) << "failed to send interruption signal to worker: " << strerror(rc); | 
 | 417 |             } | 
 | 418 |         } | 
 | 419 |  | 
 | 420 |         worker_thread_.join(); | 
 | 421 |     } | 
 | 422 |  | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 423 |     void PrepareReadBlock(IoBlock* block, uint64_t id) { | 
 | 424 |         block->pending = false; | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 425 |         block->payload = std::make_shared<Block>(kUsbReadSize); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 426 |         block->control.aio_data = static_cast<uint64_t>(TransferId::read(id)); | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 427 |         block->control.aio_buf = reinterpret_cast<uintptr_t>(block->payload->data()); | 
 | 428 |         block->control.aio_nbytes = block->payload->size(); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 429 |     } | 
 | 430 |  | 
 | 431 |     IoBlock CreateReadBlock(uint64_t id) { | 
 | 432 |         IoBlock block; | 
 | 433 |         PrepareReadBlock(&block, id); | 
 | 434 |         block.control.aio_rw_flags = 0; | 
 | 435 |         block.control.aio_lio_opcode = IOCB_CMD_PREAD; | 
 | 436 |         block.control.aio_reqprio = 0; | 
 | 437 |         block.control.aio_fildes = read_fd_.get(); | 
 | 438 |         block.control.aio_offset = 0; | 
 | 439 |         block.control.aio_flags = IOCB_FLAG_RESFD; | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 440 |         block.control.aio_resfd = worker_event_fd_.get(); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 441 |         return block; | 
 | 442 |     } | 
 | 443 |  | 
| Josh Gao | 6933d54 | 2019-03-26 13:21:42 -0700 | [diff] [blame] | 444 |     void ReadEvents() { | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 445 |         static constexpr size_t kMaxEvents = kUsbReadQueueDepth + kUsbWriteQueueDepth; | 
 | 446 |         struct io_event events[kMaxEvents]; | 
 | 447 |         struct timespec timeout = {.tv_sec = 0, .tv_nsec = 0}; | 
 | 448 |         int rc = io_getevents(aio_context_.get(), 0, kMaxEvents, events, &timeout); | 
 | 449 |         if (rc == -1) { | 
 | 450 |             HandleError(StringPrintf("io_getevents failed while reading: %s", strerror(errno))); | 
 | 451 |             return; | 
 | 452 |         } | 
 | 453 |  | 
 | 454 |         for (int event_idx = 0; event_idx < rc; ++event_idx) { | 
 | 455 |             auto& event = events[event_idx]; | 
 | 456 |             TransferId id = TransferId::from_value(event.data); | 
 | 457 |  | 
 | 458 |             if (event.res < 0) { | 
 | 459 |                 std::string error = | 
 | 460 |                         StringPrintf("%s %" PRIu64 " failed with error %s", | 
 | 461 |                                      id.direction == TransferDirection::READ ? "read" : "write", | 
 | 462 |                                      id.id, strerror(-event.res)); | 
 | 463 |                 HandleError(error); | 
 | 464 |                 return; | 
 | 465 |             } | 
 | 466 |  | 
 | 467 |             if (id.direction == TransferDirection::READ) { | 
 | 468 |                 HandleRead(id, event.res); | 
 | 469 |             } else { | 
 | 470 |                 HandleWrite(id); | 
 | 471 |             } | 
 | 472 |         } | 
 | 473 |     } | 
 | 474 |  | 
 | 475 |     void HandleRead(TransferId id, int64_t size) { | 
 | 476 |         uint64_t read_idx = id.id % kUsbReadQueueDepth; | 
 | 477 |         IoBlock* block = &read_requests_[read_idx]; | 
 | 478 |         block->pending = false; | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 479 |         block->payload->resize(size); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 480 |  | 
 | 481 |         // Notification for completed reads can be received out of order. | 
 | 482 |         if (block->id().id != needed_read_id_) { | 
 | 483 |             LOG(VERBOSE) << "read " << block->id().id << " completed while waiting for " | 
 | 484 |                          << needed_read_id_; | 
 | 485 |             return; | 
 | 486 |         } | 
 | 487 |  | 
 | 488 |         for (uint64_t id = needed_read_id_;; ++id) { | 
 | 489 |             size_t read_idx = id % kUsbReadQueueDepth; | 
 | 490 |             IoBlock* current_block = &read_requests_[read_idx]; | 
 | 491 |             if (current_block->pending) { | 
 | 492 |                 break; | 
 | 493 |             } | 
 | 494 |             ProcessRead(current_block); | 
 | 495 |             ++needed_read_id_; | 
 | 496 |         } | 
 | 497 |     } | 
 | 498 |  | 
 | 499 |     void ProcessRead(IoBlock* block) { | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 500 |         if (!block->payload->empty()) { | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 501 |             if (!incoming_header_.has_value()) { | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 502 |                 CHECK_EQ(sizeof(amessage), block->payload->size()); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 503 |                 amessage msg; | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 504 |                 memcpy(&msg, block->payload->data(), sizeof(amessage)); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 505 |                 LOG(DEBUG) << "USB read:" << dump_header(&msg); | 
 | 506 |                 incoming_header_ = msg; | 
 | 507 |             } else { | 
 | 508 |                 size_t bytes_left = incoming_header_->data_length - incoming_payload_.size(); | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 509 |                 Block payload = std::move(*block->payload); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 510 |                 CHECK_LE(payload.size(), bytes_left); | 
 | 511 |                 incoming_payload_.append(std::make_unique<Block>(std::move(payload))); | 
 | 512 |             } | 
 | 513 |  | 
 | 514 |             if (incoming_header_->data_length == incoming_payload_.size()) { | 
 | 515 |                 auto packet = std::make_unique<apacket>(); | 
 | 516 |                 packet->msg = *incoming_header_; | 
 | 517 |  | 
 | 518 |                 // TODO: Make apacket contain an IOVector so we don't have to coalesce. | 
 | 519 |                 packet->payload = incoming_payload_.coalesce(); | 
 | 520 |                 read_callback_(this, std::move(packet)); | 
 | 521 |  | 
 | 522 |                 incoming_header_.reset(); | 
 | 523 |                 incoming_payload_.clear(); | 
 | 524 |             } | 
 | 525 |         } | 
 | 526 |  | 
 | 527 |         PrepareReadBlock(block, block->id().id + kUsbReadQueueDepth); | 
 | 528 |         SubmitRead(block); | 
 | 529 |     } | 
 | 530 |  | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 531 |     bool SubmitRead(IoBlock* block) { | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 532 |         block->pending = true; | 
 | 533 |         struct iocb* iocb = &block->control; | 
 | 534 |         if (io_submit(aio_context_.get(), 1, &iocb) != 1) { | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 535 |             if (errno == EINVAL && !gFfsAioSupported.has_value()) { | 
 | 536 |                 HandleError("failed to submit first read, AIO on FFS not supported"); | 
 | 537 |                 gFfsAioSupported = false; | 
 | 538 |                 return false; | 
 | 539 |             } | 
 | 540 |  | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 541 |             HandleError(StringPrintf("failed to submit read: %s", strerror(errno))); | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 542 |             return false; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 543 |         } | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 544 |  | 
 | 545 |         gFfsAioSupported = true; | 
 | 546 |         return true; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 547 |     } | 
 | 548 |  | 
 | 549 |     void HandleWrite(TransferId id) { | 
 | 550 |         std::lock_guard<std::mutex> lock(write_mutex_); | 
 | 551 |         auto it = | 
 | 552 |                 std::find_if(write_requests_.begin(), write_requests_.end(), [id](const auto& req) { | 
 | 553 |                     return static_cast<uint64_t>(req->id()) == static_cast<uint64_t>(id); | 
 | 554 |                 }); | 
 | 555 |         CHECK(it != write_requests_.end()); | 
 | 556 |  | 
 | 557 |         write_requests_.erase(it); | 
 | 558 |         size_t outstanding_writes = --writes_submitted_; | 
 | 559 |         LOG(DEBUG) << "USB write: reaped, down to " << outstanding_writes; | 
 | 560 |  | 
 | 561 |         SubmitWrites(); | 
 | 562 |     } | 
 | 563 |  | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 564 |     std::unique_ptr<IoBlock> CreateWriteBlock(std::shared_ptr<Block> payload, size_t offset, | 
 | 565 |                                               size_t len, uint64_t id) { | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 566 |         auto block = std::make_unique<IoBlock>(); | 
 | 567 |         block->payload = std::move(payload); | 
 | 568 |         block->control.aio_data = static_cast<uint64_t>(TransferId::write(id)); | 
 | 569 |         block->control.aio_rw_flags = 0; | 
 | 570 |         block->control.aio_lio_opcode = IOCB_CMD_PWRITE; | 
 | 571 |         block->control.aio_reqprio = 0; | 
 | 572 |         block->control.aio_fildes = write_fd_.get(); | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 573 |         block->control.aio_buf = reinterpret_cast<uintptr_t>(block->payload->data() + offset); | 
 | 574 |         block->control.aio_nbytes = len; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 575 |         block->control.aio_offset = 0; | 
 | 576 |         block->control.aio_flags = IOCB_FLAG_RESFD; | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 577 |         block->control.aio_resfd = worker_event_fd_.get(); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 578 |         return block; | 
 | 579 |     } | 
 | 580 |  | 
| Josh Gao | 86b33be | 2019-02-26 17:53:52 -0800 | [diff] [blame] | 581 |     std::unique_ptr<IoBlock> CreateWriteBlock(Block payload, uint64_t id) { | 
 | 582 |         std::shared_ptr<Block> block = std::make_shared<Block>(std::move(payload)); | 
 | 583 |         size_t len = block->size(); | 
 | 584 |         return CreateWriteBlock(std::move(block), 0, len, id); | 
 | 585 |     } | 
 | 586 |  | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 587 |     void SubmitWrites() REQUIRES(write_mutex_) { | 
 | 588 |         if (writes_submitted_ == kUsbWriteQueueDepth) { | 
 | 589 |             return; | 
 | 590 |         } | 
 | 591 |  | 
 | 592 |         ssize_t writes_to_submit = std::min(kUsbWriteQueueDepth - writes_submitted_, | 
 | 593 |                                             write_requests_.size() - writes_submitted_); | 
 | 594 |         CHECK_GE(writes_to_submit, 0); | 
 | 595 |         if (writes_to_submit == 0) { | 
 | 596 |             return; | 
 | 597 |         } | 
 | 598 |  | 
 | 599 |         struct iocb* iocbs[kUsbWriteQueueDepth]; | 
 | 600 |         for (int i = 0; i < writes_to_submit; ++i) { | 
 | 601 |             CHECK(!write_requests_[writes_submitted_ + i]->pending); | 
 | 602 |             write_requests_[writes_submitted_ + i]->pending = true; | 
 | 603 |             iocbs[i] = &write_requests_[writes_submitted_ + i]->control; | 
 | 604 |             LOG(VERBOSE) << "submitting write_request " << static_cast<void*>(iocbs[i]); | 
 | 605 |         } | 
 | 606 |  | 
| Josh Gao | 63b52ec | 2019-03-26 13:06:38 -0700 | [diff] [blame] | 607 |         writes_submitted_ += writes_to_submit; | 
 | 608 |  | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 609 |         int rc = io_submit(aio_context_.get(), writes_to_submit, iocbs); | 
 | 610 |         if (rc == -1) { | 
 | 611 |             HandleError(StringPrintf("failed to submit write requests: %s", strerror(errno))); | 
 | 612 |             return; | 
 | 613 |         } else if (rc != writes_to_submit) { | 
 | 614 |             LOG(FATAL) << "failed to submit all writes: wanted to submit " << writes_to_submit | 
 | 615 |                        << ", actually submitted " << rc; | 
 | 616 |         } | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 617 |     } | 
 | 618 |  | 
 | 619 |     void HandleError(const std::string& error) { | 
 | 620 |         std::call_once(error_flag_, [&]() { | 
 | 621 |             error_callback_(this, error); | 
 | 622 |             if (!stopped_) { | 
 | 623 |                 Stop(); | 
 | 624 |             } | 
 | 625 |         }); | 
 | 626 |     } | 
 | 627 |  | 
 | 628 |     std::thread monitor_thread_; | 
| Josh Gao | 19dc296 | 2019-03-26 18:47:45 -0700 | [diff] [blame] | 629 |  | 
 | 630 |     bool worker_started_; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 631 |     std::thread worker_thread_; | 
 | 632 |  | 
 | 633 |     std::atomic<bool> stopped_; | 
 | 634 |     std::promise<void> destruction_notifier_; | 
 | 635 |     std::once_flag error_flag_; | 
 | 636 |  | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 637 |     unique_fd worker_event_fd_; | 
 | 638 |     unique_fd monitor_event_fd_; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 639 |  | 
 | 640 |     ScopedAioContext aio_context_; | 
 | 641 |     unique_fd control_fd_; | 
 | 642 |     unique_fd read_fd_; | 
 | 643 |     unique_fd write_fd_; | 
 | 644 |  | 
 | 645 |     std::optional<amessage> incoming_header_; | 
 | 646 |     IOVector incoming_payload_; | 
 | 647 |  | 
 | 648 |     std::array<IoBlock, kUsbReadQueueDepth> read_requests_; | 
 | 649 |     IOVector read_data_; | 
 | 650 |  | 
 | 651 |     // ID of the next request that we're going to send out. | 
 | 652 |     size_t next_read_id_ = 0; | 
 | 653 |  | 
 | 654 |     // ID of the next packet we're waiting for. | 
 | 655 |     size_t needed_read_id_ = 0; | 
 | 656 |  | 
 | 657 |     std::mutex write_mutex_; | 
 | 658 |     std::deque<std::unique_ptr<IoBlock>> write_requests_ GUARDED_BY(write_mutex_); | 
 | 659 |     size_t next_write_id_ GUARDED_BY(write_mutex_) = 0; | 
 | 660 |     size_t writes_submitted_ GUARDED_BY(write_mutex_) = 0; | 
| Josh Gao | e778b3a | 2019-02-28 13:29:32 -0800 | [diff] [blame] | 661 |  | 
 | 662 |     static constexpr int kInterruptionSignal = SIGUSR1; | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 663 | }; | 
 | 664 |  | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 665 | void usb_init_legacy(); | 
 | 666 |  | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 667 | static void usb_ffs_open_thread() { | 
 | 668 |     adb_thread_setname("usb ffs open"); | 
 | 669 |  | 
 | 670 |     while (true) { | 
| Josh Gao | c0b831b | 2019-02-13 15:27:28 -0800 | [diff] [blame] | 671 |         if (gFfsAioSupported.has_value() && !gFfsAioSupported.value()) { | 
 | 672 |             LOG(INFO) << "failed to use nonblocking ffs, falling back to legacy"; | 
 | 673 |             return usb_init_legacy(); | 
 | 674 |         } | 
 | 675 |  | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 676 |         unique_fd control; | 
 | 677 |         unique_fd bulk_out; | 
 | 678 |         unique_fd bulk_in; | 
 | 679 |         if (!open_functionfs(&control, &bulk_out, &bulk_in)) { | 
 | 680 |             std::this_thread::sleep_for(1s); | 
 | 681 |             continue; | 
 | 682 |         } | 
 | 683 |  | 
 | 684 |         atransport* transport = new atransport(); | 
 | 685 |         transport->serial = "UsbFfs"; | 
 | 686 |         std::promise<void> destruction_notifier; | 
 | 687 |         std::future<void> future = destruction_notifier.get_future(); | 
 | 688 |         transport->SetConnection(std::make_unique<UsbFfsConnection>( | 
 | 689 |                 std::move(control), std::move(bulk_out), std::move(bulk_in), | 
 | 690 |                 std::move(destruction_notifier))); | 
 | 691 |         register_transport(transport); | 
 | 692 |         future.wait(); | 
 | 693 |     } | 
 | 694 | } | 
 | 695 |  | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 696 | void usb_init() { | 
| Josh Gao | 8038e35 | 2019-03-18 16:33:18 -0700 | [diff] [blame] | 697 |     bool use_nonblocking = android::base::GetBoolProperty( | 
 | 698 |             "persist.adb.nonblocking_ffs", | 
 | 699 |             android::base::GetBoolProperty("ro.adb.nonblocking_ffs", true)); | 
 | 700 |  | 
| Josh Gao | 02e94a4 | 2019-02-28 07:26:20 +0000 | [diff] [blame] | 701 |     if (use_nonblocking) { | 
| Josh Gao | 0d78039 | 2019-02-26 22:10:33 +0000 | [diff] [blame] | 702 |         std::thread(usb_ffs_open_thread).detach(); | 
| Josh Gao | 02e94a4 | 2019-02-28 07:26:20 +0000 | [diff] [blame] | 703 |     } else { | 
 | 704 |         usb_init_legacy(); | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 705 |     } | 
 | 706 | } |