blob: 8a5000336dc98092b316e3ffae47166b944de99a [file] [log] [blame]
Josh Gaoc51726c2018-10-11 16:33:05 -07001/*
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 Gao86b33be2019-02-26 17:53:52 -080032#include <algorithm>
Josh Gaoc51726c2018-10-11 16:33:05 -070033#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
55using android::base::StringPrintf;
56
Josh Gaoc0b831b2019-02-13 15:27:28 -080057// We can't find out whether we have support for AIO on ffs endpoints until we submit a read.
58static std::optional<bool> gFfsAioSupported;
59
Josh Gao5841a962019-02-28 15:44:05 -080060static constexpr size_t kUsbReadQueueDepth = 32;
61static constexpr size_t kUsbReadSize = 8 * PAGE_SIZE;
Josh Gaoc51726c2018-10-11 16:33:05 -070062
Josh Gao5841a962019-02-28 15:44:05 -080063static constexpr size_t kUsbWriteQueueDepth = 32;
64static constexpr size_t kUsbWriteSize = 8 * PAGE_SIZE;
Josh Gaoc51726c2018-10-11 16:33:05 -070065
66static const char* to_string(enum usb_functionfs_event_type type) {
67 switch (type) {
68 case FUNCTIONFS_BIND:
69 return "FUNCTIONFS_BIND";
70 case FUNCTIONFS_UNBIND:
71 return "FUNCTIONFS_UNBIND";
72 case FUNCTIONFS_ENABLE:
73 return "FUNCTIONFS_ENABLE";
74 case FUNCTIONFS_DISABLE:
75 return "FUNCTIONFS_DISABLE";
76 case FUNCTIONFS_SETUP:
77 return "FUNCTIONFS_SETUP";
78 case FUNCTIONFS_SUSPEND:
79 return "FUNCTIONFS_SUSPEND";
80 case FUNCTIONFS_RESUME:
81 return "FUNCTIONFS_RESUME";
82 }
83}
84
85enum class TransferDirection : uint64_t {
86 READ = 0,
87 WRITE = 1,
88};
89
90struct TransferId {
91 TransferDirection direction : 1;
92 uint64_t id : 63;
93
94 TransferId() : TransferId(TransferDirection::READ, 0) {}
95
96 private:
97 TransferId(TransferDirection direction, uint64_t id) : direction(direction), id(id) {}
98
99 public:
100 explicit operator uint64_t() const {
101 uint64_t result;
102 static_assert(sizeof(*this) == sizeof(result));
103 memcpy(&result, this, sizeof(*this));
104 return result;
105 }
106
107 static TransferId read(uint64_t id) { return TransferId(TransferDirection::READ, id); }
108 static TransferId write(uint64_t id) { return TransferId(TransferDirection::WRITE, id); }
109
110 static TransferId from_value(uint64_t value) {
111 TransferId result;
112 memcpy(&result, &value, sizeof(value));
113 return result;
114 }
115};
116
117struct IoBlock {
Josh Gaob0195742019-03-18 14:11:28 -0700118 bool pending = false;
Josh Gaoc51726c2018-10-11 16:33:05 -0700119 struct iocb control;
Josh Gao86b33be2019-02-26 17:53:52 -0800120 std::shared_ptr<Block> payload;
Josh Gaoc51726c2018-10-11 16:33:05 -0700121
122 TransferId id() const { return TransferId::from_value(control.aio_data); }
123};
124
125struct ScopedAioContext {
126 ScopedAioContext() = default;
127 ~ScopedAioContext() { reset(); }
128
129 ScopedAioContext(ScopedAioContext&& move) { reset(move.release()); }
130 ScopedAioContext(const ScopedAioContext& copy) = delete;
131
132 ScopedAioContext& operator=(ScopedAioContext&& move) {
133 reset(move.release());
134 return *this;
135 }
136 ScopedAioContext& operator=(const ScopedAioContext& copy) = delete;
137
138 static ScopedAioContext Create(size_t max_events) {
139 aio_context_t ctx = 0;
140 if (io_setup(max_events, &ctx) != 0) {
141 PLOG(FATAL) << "failed to create aio_context_t";
142 }
143 ScopedAioContext result;
144 result.reset(ctx);
145 return result;
146 }
147
148 aio_context_t release() {
149 aio_context_t result = context_;
150 context_ = 0;
151 return result;
152 }
153
154 void reset(aio_context_t new_context = 0) {
155 if (context_ != 0) {
156 io_destroy(context_);
157 }
158
159 context_ = new_context;
160 }
161
162 aio_context_t get() { return context_; }
163
164 private:
165 aio_context_t context_ = 0;
166};
167
168struct UsbFfsConnection : public Connection {
169 UsbFfsConnection(unique_fd control, unique_fd read, unique_fd write,
170 std::promise<void> destruction_notifier)
171 : stopped_(false),
172 destruction_notifier_(std::move(destruction_notifier)),
173 control_fd_(std::move(control)),
174 read_fd_(std::move(read)),
175 write_fd_(std::move(write)) {
176 LOG(INFO) << "UsbFfsConnection constructed";
Josh Gaoc0b831b2019-02-13 15:27:28 -0800177 worker_event_fd_.reset(eventfd(0, EFD_CLOEXEC));
178 if (worker_event_fd_ == -1) {
179 PLOG(FATAL) << "failed to create eventfd";
180 }
181
182 monitor_event_fd_.reset(eventfd(0, EFD_CLOEXEC));
183 if (monitor_event_fd_ == -1) {
Josh Gaoc51726c2018-10-11 16:33:05 -0700184 PLOG(FATAL) << "failed to create eventfd";
185 }
186
187 aio_context_ = ScopedAioContext::Create(kUsbReadQueueDepth + kUsbWriteQueueDepth);
188 }
189
190 ~UsbFfsConnection() {
191 LOG(INFO) << "UsbFfsConnection being destroyed";
192 Stop();
193 monitor_thread_.join();
Josh Gaoc0b831b2019-02-13 15:27:28 -0800194
195 // We need to explicitly close our file descriptors before we notify our destruction,
196 // because the thread listening on the future will immediately try to reopen the endpoint.
197 control_fd_.reset();
198 read_fd_.reset();
199 write_fd_.reset();
200
Josh Gaoc51726c2018-10-11 16:33:05 -0700201 destruction_notifier_.set_value();
202 }
203
204 virtual bool Write(std::unique_ptr<apacket> packet) override final {
205 LOG(DEBUG) << "USB write: " << dump_header(&packet->msg);
206 Block header(sizeof(packet->msg));
207 memcpy(header.data(), &packet->msg, sizeof(packet->msg));
208
209 std::lock_guard<std::mutex> lock(write_mutex_);
210 write_requests_.push_back(CreateWriteBlock(std::move(header), next_write_id_++));
211 if (!packet->payload.empty()) {
Josh Gao86b33be2019-02-26 17:53:52 -0800212 // The kernel attempts to allocate a contiguous block of memory for each write,
213 // which can fail if the write is large and the kernel heap is fragmented.
214 // Split large writes into smaller chunks to avoid this.
215 std::shared_ptr<Block> payload = std::make_shared<Block>(std::move(packet->payload));
216 size_t offset = 0;
217 size_t len = payload->size();
218
219 while (len > 0) {
220 size_t write_size = std::min(kUsbWriteSize, len);
221 write_requests_.push_back(
222 CreateWriteBlock(payload, offset, write_size, next_write_id_++));
223 len -= write_size;
224 offset += write_size;
225 }
Josh Gaoc51726c2018-10-11 16:33:05 -0700226 }
227 SubmitWrites();
228 return true;
229 }
230
231 virtual void Start() override final { StartMonitor(); }
232
233 virtual void Stop() override final {
234 if (stopped_.exchange(true)) {
235 return;
236 }
237 stopped_ = true;
238 uint64_t notify = 1;
Josh Gaoc0b831b2019-02-13 15:27:28 -0800239 ssize_t rc = adb_write(worker_event_fd_.get(), &notify, sizeof(notify));
Josh Gaoc51726c2018-10-11 16:33:05 -0700240 if (rc < 0) {
Josh Gaoc0b831b2019-02-13 15:27:28 -0800241 PLOG(FATAL) << "failed to notify worker eventfd to stop UsbFfsConnection";
Josh Gaoc51726c2018-10-11 16:33:05 -0700242 }
243 CHECK_EQ(static_cast<size_t>(rc), sizeof(notify));
Josh Gaoc0b831b2019-02-13 15:27:28 -0800244
245 rc = adb_write(monitor_event_fd_.get(), &notify, sizeof(notify));
246 if (rc < 0) {
247 PLOG(FATAL) << "failed to notify monitor eventfd to stop UsbFfsConnection";
248 }
249
250 CHECK_EQ(static_cast<size_t>(rc), sizeof(notify));
Josh Gaoc51726c2018-10-11 16:33:05 -0700251 }
252
253 private:
254 void StartMonitor() {
255 // This is a bit of a mess.
256 // It's possible for io_submit to end up blocking, if we call it as the endpoint
257 // becomes disabled. Work around this by having a monitor thread to listen for functionfs
258 // lifecycle events. If we notice an error condition (either we've become disabled, or we
259 // were never enabled in the first place), we send interruption signals to the worker thread
260 // until it dies, and then report failure to the transport via HandleError, which will
261 // eventually result in the transport being destroyed, which will result in UsbFfsConnection
262 // being destroyed, which unblocks the open thread and restarts this entire process.
Josh Gaoc51726c2018-10-11 16:33:05 -0700263 static std::once_flag handler_once;
264 std::call_once(handler_once, []() { signal(kInterruptionSignal, [](int) {}); });
265
266 monitor_thread_ = std::thread([this]() {
267 adb_thread_setname("UsbFfs-monitor");
268
269 bool bound = false;
Josh Gao6933d542019-03-26 13:21:42 -0700270 bool enabled = false;
Josh Gaoc51726c2018-10-11 16:33:05 -0700271 bool running = true;
272 while (running) {
Josh Gaoc0b831b2019-02-13 15:27:28 -0800273 adb_pollfd pfd[2] = {
274 { .fd = control_fd_.get(), .events = POLLIN, .revents = 0 },
275 { .fd = monitor_event_fd_.get(), .events = POLLIN, .revents = 0 },
276 };
Josh Gao007a4dc2019-03-11 13:03:08 -0700277 int rc = TEMP_FAILURE_RETRY(adb_poll(pfd, 2, -1));
Josh Gaoc0b831b2019-02-13 15:27:28 -0800278 if (rc == -1) {
279 PLOG(FATAL) << "poll on USB control fd failed";
280 } else if (rc == 0) {
Josh Gao007a4dc2019-03-11 13:03:08 -0700281 LOG(FATAL) << "poll on USB control fd returned 0";
Josh Gaoc0b831b2019-02-13 15:27:28 -0800282 }
283
284 if (pfd[1].revents) {
285 // We were told to die.
286 break;
Josh Gaoc51726c2018-10-11 16:33:05 -0700287 }
288
289 struct usb_functionfs_event event;
290 if (TEMP_FAILURE_RETRY(adb_read(control_fd_.get(), &event, sizeof(event))) !=
291 sizeof(event)) {
292 PLOG(FATAL) << "failed to read functionfs event";
293 }
294
295 LOG(INFO) << "USB event: "
296 << to_string(static_cast<usb_functionfs_event_type>(event.type));
297
298 switch (event.type) {
299 case FUNCTIONFS_BIND:
Josh Gaoe778b3a2019-02-28 13:29:32 -0800300 CHECK(!bound) << "received FUNCTIONFS_BIND while already bound?";
Josh Gao6933d542019-03-26 13:21:42 -0700301 CHECK(!enabled) << "received FUNCTIONFS_BIND while already enabled?";
Josh Gaoc51726c2018-10-11 16:33:05 -0700302 bound = true;
Josh Gao6933d542019-03-26 13:21:42 -0700303
Josh Gaoc51726c2018-10-11 16:33:05 -0700304 break;
305
306 case FUNCTIONFS_ENABLE:
Josh Gao6933d542019-03-26 13:21:42 -0700307 CHECK(bound) << "received FUNCTIONFS_ENABLE while not bound?";
308 CHECK(!enabled) << "received FUNCTIONFS_ENABLE while already enabled?";
309 enabled = true;
310
Josh Gaoc51726c2018-10-11 16:33:05 -0700311 StartWorker();
312 break;
313
314 case FUNCTIONFS_DISABLE:
Josh Gao6933d542019-03-26 13:21:42 -0700315 CHECK(bound) << "received FUNCTIONFS_DISABLE while not bound?";
316 CHECK(enabled) << "received FUNCTIONFS_DISABLE while not enabled?";
317 enabled = false;
318
319 running = false;
320 break;
321
322 case FUNCTIONFS_UNBIND:
323 CHECK(!enabled) << "received FUNCTIONFS_UNBIND while still enabled?";
324 CHECK(bound) << "received FUNCTIONFS_UNBIND when not bound?";
325 bound = false;
326
Josh Gaoc51726c2018-10-11 16:33:05 -0700327 running = false;
328 break;
329 }
330 }
331
Josh Gaoe778b3a2019-02-28 13:29:32 -0800332 StopWorker();
Josh Gaoc51726c2018-10-11 16:33:05 -0700333 aio_context_.reset();
334 read_fd_.reset();
335 write_fd_.reset();
336 });
337 }
338
339 void StartWorker() {
340 worker_thread_ = std::thread([this]() {
341 adb_thread_setname("UsbFfs-worker");
342 for (size_t i = 0; i < kUsbReadQueueDepth; ++i) {
343 read_requests_[i] = CreateReadBlock(next_read_id_++);
Josh Gaoc0b831b2019-02-13 15:27:28 -0800344 if (!SubmitRead(&read_requests_[i])) {
345 return;
346 }
Josh Gaoc51726c2018-10-11 16:33:05 -0700347 }
348
349 while (!stopped_) {
350 uint64_t dummy;
Josh Gaoc0b831b2019-02-13 15:27:28 -0800351 ssize_t rc = adb_read(worker_event_fd_.get(), &dummy, sizeof(dummy));
Josh Gaoc51726c2018-10-11 16:33:05 -0700352 if (rc == -1) {
353 PLOG(FATAL) << "failed to read from eventfd";
354 } else if (rc == 0) {
355 LOG(FATAL) << "hit EOF on eventfd";
356 }
357
Josh Gao6933d542019-03-26 13:21:42 -0700358 ReadEvents();
Josh Gaoc51726c2018-10-11 16:33:05 -0700359 }
360 });
361 }
362
Josh Gaoe778b3a2019-02-28 13:29:32 -0800363 void StopWorker() {
364 pthread_t worker_thread_handle = worker_thread_.native_handle();
365 while (true) {
366 int rc = pthread_kill(worker_thread_handle, kInterruptionSignal);
367 if (rc != 0) {
368 LOG(ERROR) << "failed to send interruption signal to worker: " << strerror(rc);
369 break;
370 }
371
372 std::this_thread::sleep_for(100ms);
373
374 rc = pthread_kill(worker_thread_handle, 0);
375 if (rc == 0) {
376 continue;
377 } else if (rc == ESRCH) {
378 break;
379 } else {
380 LOG(ERROR) << "failed to send interruption signal to worker: " << strerror(rc);
381 }
382 }
383
384 worker_thread_.join();
385 }
386
Josh Gaoc51726c2018-10-11 16:33:05 -0700387 void PrepareReadBlock(IoBlock* block, uint64_t id) {
388 block->pending = false;
Josh Gao86b33be2019-02-26 17:53:52 -0800389 block->payload = std::make_shared<Block>(kUsbReadSize);
Josh Gaoc51726c2018-10-11 16:33:05 -0700390 block->control.aio_data = static_cast<uint64_t>(TransferId::read(id));
Josh Gao86b33be2019-02-26 17:53:52 -0800391 block->control.aio_buf = reinterpret_cast<uintptr_t>(block->payload->data());
392 block->control.aio_nbytes = block->payload->size();
Josh Gaoc51726c2018-10-11 16:33:05 -0700393 }
394
395 IoBlock CreateReadBlock(uint64_t id) {
396 IoBlock block;
397 PrepareReadBlock(&block, id);
398 block.control.aio_rw_flags = 0;
399 block.control.aio_lio_opcode = IOCB_CMD_PREAD;
400 block.control.aio_reqprio = 0;
401 block.control.aio_fildes = read_fd_.get();
402 block.control.aio_offset = 0;
403 block.control.aio_flags = IOCB_FLAG_RESFD;
Josh Gaoc0b831b2019-02-13 15:27:28 -0800404 block.control.aio_resfd = worker_event_fd_.get();
Josh Gaoc51726c2018-10-11 16:33:05 -0700405 return block;
406 }
407
Josh Gao6933d542019-03-26 13:21:42 -0700408 void ReadEvents() {
Josh Gaoc51726c2018-10-11 16:33:05 -0700409 static constexpr size_t kMaxEvents = kUsbReadQueueDepth + kUsbWriteQueueDepth;
410 struct io_event events[kMaxEvents];
411 struct timespec timeout = {.tv_sec = 0, .tv_nsec = 0};
412 int rc = io_getevents(aio_context_.get(), 0, kMaxEvents, events, &timeout);
413 if (rc == -1) {
414 HandleError(StringPrintf("io_getevents failed while reading: %s", strerror(errno)));
415 return;
416 }
417
418 for (int event_idx = 0; event_idx < rc; ++event_idx) {
419 auto& event = events[event_idx];
420 TransferId id = TransferId::from_value(event.data);
421
422 if (event.res < 0) {
423 std::string error =
424 StringPrintf("%s %" PRIu64 " failed with error %s",
425 id.direction == TransferDirection::READ ? "read" : "write",
426 id.id, strerror(-event.res));
427 HandleError(error);
428 return;
429 }
430
431 if (id.direction == TransferDirection::READ) {
432 HandleRead(id, event.res);
433 } else {
434 HandleWrite(id);
435 }
436 }
437 }
438
439 void HandleRead(TransferId id, int64_t size) {
440 uint64_t read_idx = id.id % kUsbReadQueueDepth;
441 IoBlock* block = &read_requests_[read_idx];
442 block->pending = false;
Josh Gao86b33be2019-02-26 17:53:52 -0800443 block->payload->resize(size);
Josh Gaoc51726c2018-10-11 16:33:05 -0700444
445 // Notification for completed reads can be received out of order.
446 if (block->id().id != needed_read_id_) {
447 LOG(VERBOSE) << "read " << block->id().id << " completed while waiting for "
448 << needed_read_id_;
449 return;
450 }
451
452 for (uint64_t id = needed_read_id_;; ++id) {
453 size_t read_idx = id % kUsbReadQueueDepth;
454 IoBlock* current_block = &read_requests_[read_idx];
455 if (current_block->pending) {
456 break;
457 }
458 ProcessRead(current_block);
459 ++needed_read_id_;
460 }
461 }
462
463 void ProcessRead(IoBlock* block) {
Josh Gao86b33be2019-02-26 17:53:52 -0800464 if (!block->payload->empty()) {
Josh Gaoc51726c2018-10-11 16:33:05 -0700465 if (!incoming_header_.has_value()) {
Josh Gao86b33be2019-02-26 17:53:52 -0800466 CHECK_EQ(sizeof(amessage), block->payload->size());
Josh Gaoc51726c2018-10-11 16:33:05 -0700467 amessage msg;
Josh Gao86b33be2019-02-26 17:53:52 -0800468 memcpy(&msg, block->payload->data(), sizeof(amessage));
Josh Gaoc51726c2018-10-11 16:33:05 -0700469 LOG(DEBUG) << "USB read:" << dump_header(&msg);
470 incoming_header_ = msg;
471 } else {
472 size_t bytes_left = incoming_header_->data_length - incoming_payload_.size();
Josh Gao86b33be2019-02-26 17:53:52 -0800473 Block payload = std::move(*block->payload);
Josh Gaoc51726c2018-10-11 16:33:05 -0700474 CHECK_LE(payload.size(), bytes_left);
475 incoming_payload_.append(std::make_unique<Block>(std::move(payload)));
476 }
477
478 if (incoming_header_->data_length == incoming_payload_.size()) {
479 auto packet = std::make_unique<apacket>();
480 packet->msg = *incoming_header_;
481
482 // TODO: Make apacket contain an IOVector so we don't have to coalesce.
483 packet->payload = incoming_payload_.coalesce();
484 read_callback_(this, std::move(packet));
485
486 incoming_header_.reset();
487 incoming_payload_.clear();
488 }
489 }
490
491 PrepareReadBlock(block, block->id().id + kUsbReadQueueDepth);
492 SubmitRead(block);
493 }
494
Josh Gaoc0b831b2019-02-13 15:27:28 -0800495 bool SubmitRead(IoBlock* block) {
Josh Gaoc51726c2018-10-11 16:33:05 -0700496 block->pending = true;
497 struct iocb* iocb = &block->control;
498 if (io_submit(aio_context_.get(), 1, &iocb) != 1) {
Josh Gaoc0b831b2019-02-13 15:27:28 -0800499 if (errno == EINVAL && !gFfsAioSupported.has_value()) {
500 HandleError("failed to submit first read, AIO on FFS not supported");
501 gFfsAioSupported = false;
502 return false;
503 }
504
Josh Gaoc51726c2018-10-11 16:33:05 -0700505 HandleError(StringPrintf("failed to submit read: %s", strerror(errno)));
Josh Gaoc0b831b2019-02-13 15:27:28 -0800506 return false;
Josh Gaoc51726c2018-10-11 16:33:05 -0700507 }
Josh Gaoc0b831b2019-02-13 15:27:28 -0800508
509 gFfsAioSupported = true;
510 return true;
Josh Gaoc51726c2018-10-11 16:33:05 -0700511 }
512
513 void HandleWrite(TransferId id) {
514 std::lock_guard<std::mutex> lock(write_mutex_);
515 auto it =
516 std::find_if(write_requests_.begin(), write_requests_.end(), [id](const auto& req) {
517 return static_cast<uint64_t>(req->id()) == static_cast<uint64_t>(id);
518 });
519 CHECK(it != write_requests_.end());
520
521 write_requests_.erase(it);
522 size_t outstanding_writes = --writes_submitted_;
523 LOG(DEBUG) << "USB write: reaped, down to " << outstanding_writes;
524
525 SubmitWrites();
526 }
527
Josh Gao86b33be2019-02-26 17:53:52 -0800528 std::unique_ptr<IoBlock> CreateWriteBlock(std::shared_ptr<Block> payload, size_t offset,
529 size_t len, uint64_t id) {
Josh Gaoc51726c2018-10-11 16:33:05 -0700530 auto block = std::make_unique<IoBlock>();
531 block->payload = std::move(payload);
532 block->control.aio_data = static_cast<uint64_t>(TransferId::write(id));
533 block->control.aio_rw_flags = 0;
534 block->control.aio_lio_opcode = IOCB_CMD_PWRITE;
535 block->control.aio_reqprio = 0;
536 block->control.aio_fildes = write_fd_.get();
Josh Gao86b33be2019-02-26 17:53:52 -0800537 block->control.aio_buf = reinterpret_cast<uintptr_t>(block->payload->data() + offset);
538 block->control.aio_nbytes = len;
Josh Gaoc51726c2018-10-11 16:33:05 -0700539 block->control.aio_offset = 0;
540 block->control.aio_flags = IOCB_FLAG_RESFD;
Josh Gaoc0b831b2019-02-13 15:27:28 -0800541 block->control.aio_resfd = worker_event_fd_.get();
Josh Gaoc51726c2018-10-11 16:33:05 -0700542 return block;
543 }
544
Josh Gao86b33be2019-02-26 17:53:52 -0800545 std::unique_ptr<IoBlock> CreateWriteBlock(Block payload, uint64_t id) {
546 std::shared_ptr<Block> block = std::make_shared<Block>(std::move(payload));
547 size_t len = block->size();
548 return CreateWriteBlock(std::move(block), 0, len, id);
549 }
550
Josh Gaoc51726c2018-10-11 16:33:05 -0700551 void SubmitWrites() REQUIRES(write_mutex_) {
552 if (writes_submitted_ == kUsbWriteQueueDepth) {
553 return;
554 }
555
556 ssize_t writes_to_submit = std::min(kUsbWriteQueueDepth - writes_submitted_,
557 write_requests_.size() - writes_submitted_);
558 CHECK_GE(writes_to_submit, 0);
559 if (writes_to_submit == 0) {
560 return;
561 }
562
563 struct iocb* iocbs[kUsbWriteQueueDepth];
564 for (int i = 0; i < writes_to_submit; ++i) {
565 CHECK(!write_requests_[writes_submitted_ + i]->pending);
566 write_requests_[writes_submitted_ + i]->pending = true;
567 iocbs[i] = &write_requests_[writes_submitted_ + i]->control;
568 LOG(VERBOSE) << "submitting write_request " << static_cast<void*>(iocbs[i]);
569 }
570
Josh Gao63b52ec2019-03-26 13:06:38 -0700571 writes_submitted_ += writes_to_submit;
572
Josh Gaoc51726c2018-10-11 16:33:05 -0700573 int rc = io_submit(aio_context_.get(), writes_to_submit, iocbs);
574 if (rc == -1) {
575 HandleError(StringPrintf("failed to submit write requests: %s", strerror(errno)));
576 return;
577 } else if (rc != writes_to_submit) {
578 LOG(FATAL) << "failed to submit all writes: wanted to submit " << writes_to_submit
579 << ", actually submitted " << rc;
580 }
Josh Gaoc51726c2018-10-11 16:33:05 -0700581 }
582
583 void HandleError(const std::string& error) {
584 std::call_once(error_flag_, [&]() {
585 error_callback_(this, error);
586 if (!stopped_) {
587 Stop();
588 }
589 });
590 }
591
592 std::thread monitor_thread_;
593 std::thread worker_thread_;
594
595 std::atomic<bool> stopped_;
596 std::promise<void> destruction_notifier_;
597 std::once_flag error_flag_;
598
Josh Gaoc0b831b2019-02-13 15:27:28 -0800599 unique_fd worker_event_fd_;
600 unique_fd monitor_event_fd_;
Josh Gaoc51726c2018-10-11 16:33:05 -0700601
602 ScopedAioContext aio_context_;
603 unique_fd control_fd_;
604 unique_fd read_fd_;
605 unique_fd write_fd_;
606
607 std::optional<amessage> incoming_header_;
608 IOVector incoming_payload_;
609
610 std::array<IoBlock, kUsbReadQueueDepth> read_requests_;
611 IOVector read_data_;
612
613 // ID of the next request that we're going to send out.
614 size_t next_read_id_ = 0;
615
616 // ID of the next packet we're waiting for.
617 size_t needed_read_id_ = 0;
618
619 std::mutex write_mutex_;
620 std::deque<std::unique_ptr<IoBlock>> write_requests_ GUARDED_BY(write_mutex_);
621 size_t next_write_id_ GUARDED_BY(write_mutex_) = 0;
622 size_t writes_submitted_ GUARDED_BY(write_mutex_) = 0;
Josh Gaoe778b3a2019-02-28 13:29:32 -0800623
624 static constexpr int kInterruptionSignal = SIGUSR1;
Josh Gaoc51726c2018-10-11 16:33:05 -0700625};
626
Josh Gaoc0b831b2019-02-13 15:27:28 -0800627void usb_init_legacy();
628
Josh Gaoc51726c2018-10-11 16:33:05 -0700629static void usb_ffs_open_thread() {
630 adb_thread_setname("usb ffs open");
631
632 while (true) {
Josh Gaoc0b831b2019-02-13 15:27:28 -0800633 if (gFfsAioSupported.has_value() && !gFfsAioSupported.value()) {
634 LOG(INFO) << "failed to use nonblocking ffs, falling back to legacy";
635 return usb_init_legacy();
636 }
637
Josh Gaoc51726c2018-10-11 16:33:05 -0700638 unique_fd control;
639 unique_fd bulk_out;
640 unique_fd bulk_in;
641 if (!open_functionfs(&control, &bulk_out, &bulk_in)) {
642 std::this_thread::sleep_for(1s);
643 continue;
644 }
645
646 atransport* transport = new atransport();
647 transport->serial = "UsbFfs";
648 std::promise<void> destruction_notifier;
649 std::future<void> future = destruction_notifier.get_future();
650 transport->SetConnection(std::make_unique<UsbFfsConnection>(
651 std::move(control), std::move(bulk_out), std::move(bulk_in),
652 std::move(destruction_notifier)));
653 register_transport(transport);
654 future.wait();
655 }
656}
657
Josh Gaoc51726c2018-10-11 16:33:05 -0700658void usb_init() {
Josh Gao8038e352019-03-18 16:33:18 -0700659 bool use_nonblocking = android::base::GetBoolProperty(
660 "persist.adb.nonblocking_ffs",
661 android::base::GetBoolProperty("ro.adb.nonblocking_ffs", true));
662
Josh Gao02e94a42019-02-28 07:26:20 +0000663 if (use_nonblocking) {
Josh Gao0d780392019-02-26 22:10:33 +0000664 std::thread(usb_ffs_open_thread).detach();
Josh Gao02e94a42019-02-28 07:26:20 +0000665 } else {
666 usb_init_legacy();
Josh Gaoc51726c2018-10-11 16:33:05 -0700667 }
668}