blob: 6c3f735e2808ff810c58e16c9661aa0233ef5121 [file] [log] [blame]
Josh Gao3a34bc52018-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>
Josh Gaoa4dfc142020-02-19 13:50:57 -080022#include <inttypes.h>
Josh Gao3a34bc52018-10-11 16:33:05 -070023#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 Gaob9727b02019-02-26 17:53:52 -080033#include <algorithm>
Josh Gao3a34bc52018-10-11 16:33:05 -070034#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 Gaoa42a56d2021-03-22 13:56:40 -070045#include <android-base/parsebool.h>
Josh Gao3a34bc52018-10-11 16:33:05 -070046#include <android-base/properties.h>
47#include <android-base/thread_annotations.h>
48
Josh Gao3a34bc52018-10-11 16:33:05 -070049#include "adb_unique_fd.h"
50#include "adb_utils.h"
Josh Gaoa42a56d2021-03-22 13:56:40 -070051#include "daemon/property_monitor.h"
Josh Gao6b55e752020-03-27 18:09:56 -070052#include "daemon/usb_ffs.h"
Josh Gao3a34bc52018-10-11 16:33:05 -070053#include "sysdeps/chrono.h"
Josh Gaod5417bc2021-04-27 20:32:01 -070054#include "transfer_id.h"
Josh Gao3a34bc52018-10-11 16:33:05 -070055#include "transport.h"
56#include "types.h"
57
58using android::base::StringPrintf;
59
Josh Gao641c7082019-04-16 11:20:04 -070060// Not all USB controllers support operations larger than 16k, so don't go above that.
Josh Gao3b4be352019-04-24 14:28:25 -070061// 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.
63static constexpr size_t kUsbReadQueueDepth = 8;
Josh Gao641c7082019-04-16 11:20:04 -070064static constexpr size_t kUsbReadSize = 4 * PAGE_SIZE;
Josh Gao3a34bc52018-10-11 16:33:05 -070065
Josh Gao3b4be352019-04-24 14:28:25 -070066static constexpr size_t kUsbWriteQueueDepth = 8;
Josh Gao641c7082019-04-16 11:20:04 -070067static constexpr size_t kUsbWriteSize = 4 * PAGE_SIZE;
Josh Gao3a34bc52018-10-11 16:33:05 -070068
Dan Albert4c5a66b2019-06-24 14:35:35 -070069static 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 Zubrytskyie5e6b0d2019-07-12 14:11:54 -070088template <class Payload>
Josh Gao3a34bc52018-10-11 16:33:05 -070089struct IoBlock {
Josh Gao9f608462019-03-18 14:11:28 -070090 bool pending = false;
Evgenii Stepanov6e03b682019-05-15 18:45:01 -070091 struct iocb control = {};
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -070092 Payload payload;
Josh Gao3a34bc52018-10-11 16:33:05 -070093
94 TransferId id() const { return TransferId::from_value(control.aio_data); }
95};
96
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -070097using IoReadBlock = IoBlock<Block>;
98using IoWriteBlock = IoBlock<std::shared_ptr<Block>>;
99
Josh Gao3a34bc52018-10-11 16:33:05 -0700100struct 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
143struct UsbFfsConnection : public Connection {
Dan Albert4c5a66b2019-06-24 14:35:35 -0700144 UsbFfsConnection(unique_fd control, unique_fd read, unique_fd write,
Josh Gao3a34bc52018-10-11 16:33:05 -0700145 std::promise<void> destruction_notifier)
Josh Gaodcf36882019-03-26 18:47:45 -0700146 : worker_started_(false),
147 stopped_(false),
Josh Gao3a34bc52018-10-11 16:33:05 -0700148 destruction_notifier_(std::move(destruction_notifier)),
Dan Albert4c5a66b2019-06-24 14:35:35 -0700149 control_fd_(std::move(control)),
Josh Gao3a34bc52018-10-11 16:33:05 -0700150 read_fd_(std::move(read)),
151 write_fd_(std::move(write)) {
152 LOG(INFO) << "UsbFfsConnection constructed";
Josh Gaob8afeea2019-02-13 15:27:28 -0800153 worker_event_fd_.reset(eventfd(0, EFD_CLOEXEC));
154 if (worker_event_fd_ == -1) {
155 PLOG(FATAL) << "failed to create eventfd";
156 }
157
Dan Albert4c5a66b2019-06-24 14:35:35 -0700158 monitor_event_fd_.reset(eventfd(0, EFD_CLOEXEC));
159 if (monitor_event_fd_ == -1) {
160 PLOG(FATAL) << "failed to create eventfd";
161 }
162
Josh Gao3a34bc52018-10-11 16:33:05 -0700163 aio_context_ = ScopedAioContext::Create(kUsbReadQueueDepth + kUsbWriteQueueDepth);
164 }
165
166 ~UsbFfsConnection() {
167 LOG(INFO) << "UsbFfsConnection being destroyed";
168 Stop();
169 monitor_thread_.join();
Josh Gaob8afeea2019-02-13 15:27:28 -0800170
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 Gaodcf36882019-03-26 18:47:45 -0700173 aio_context_.reset();
Dan Albert4c5a66b2019-06-24 14:35:35 -0700174 control_fd_.reset();
Josh Gaob8afeea2019-02-13 15:27:28 -0800175 read_fd_.reset();
176 write_fd_.reset();
177
Josh Gao3a34bc52018-10-11 16:33:05 -0700178 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 Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700183 auto header = std::make_shared<Block>(sizeof(packet->msg));
184 memcpy(header->data(), &packet->msg, sizeof(packet->msg));
Josh Gao3a34bc52018-10-11 16:33:05 -0700185
186 std::lock_guard<std::mutex> lock(write_mutex_);
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700187 write_requests_.push_back(
188 CreateWriteBlock(std::move(header), 0, sizeof(packet->msg), next_write_id_++));
Josh Gao3a34bc52018-10-11 16:33:05 -0700189 if (!packet->payload.empty()) {
Josh Gaob9727b02019-02-26 17:53:52 -0800190 // 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 Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700193 auto payload = std::make_shared<Block>(std::move(packet->payload));
Josh Gaob9727b02019-02-26 17:53:52 -0800194 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 Gao3a34bc52018-10-11 16:33:05 -0700204 }
Josh Gao58cc5152020-05-19 15:57:06 -0700205
206 // Wake up the worker thread to submit writes.
207 uint64_t notify = 1;
208 ssize_t rc = adb_write(worker_event_fd_.get(), &notify, sizeof(notify));
209 if (rc < 0) {
210 PLOG(FATAL) << "failed to notify worker eventfd to submit writes";
211 }
212
Josh Gao3a34bc52018-10-11 16:33:05 -0700213 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 Gaob8afeea2019-02-13 15:27:28 -0800224 ssize_t rc = adb_write(worker_event_fd_.get(), &notify, sizeof(notify));
Josh Gao3a34bc52018-10-11 16:33:05 -0700225 if (rc < 0) {
Josh Gaob8afeea2019-02-13 15:27:28 -0800226 PLOG(FATAL) << "failed to notify worker eventfd to stop UsbFfsConnection";
Josh Gao3a34bc52018-10-11 16:33:05 -0700227 }
228 CHECK_EQ(static_cast<size_t>(rc), sizeof(notify));
Dan Albert4c5a66b2019-06-24 14:35:35 -0700229
230 rc = adb_write(monitor_event_fd_.get(), &notify, 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 Gao3a34bc52018-10-11 16:33:05 -0700236 }
237
Joshua Duong64fab752020-01-21 13:19:42 -0800238 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 Gao3a34bc52018-10-11 16:33:05 -0700244 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 Gao3a34bc52018-10-11 16:33:05 -0700254 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 Gaobd4fb2d2020-02-26 15:44:30 -0800259 LOG(INFO) << "UsbFfs-monitor thread spawned";
Josh Gao3a34bc52018-10-11 16:33:05 -0700260
Dan Albert4c5a66b2019-06-24 14:35:35 -0700261 bool bound = false;
Josh Gao961496c2019-03-26 13:21:42 -0700262 bool enabled = false;
Josh Gao3a34bc52018-10-11 16:33:05 -0700263 bool running = true;
264 while (running) {
Josh Gaob8afeea2019-02-13 15:27:28 -0800265 adb_pollfd pfd[2] = {
Dan Albert4c5a66b2019-06-24 14:35:35 -0700266 { .fd = control_fd_.get(), .events = POLLIN, .revents = 0 },
267 { .fd = monitor_event_fd_.get(), .events = POLLIN, .revents = 0 },
Josh Gaob8afeea2019-02-13 15:27:28 -0800268 };
Josh Gaodcf36882019-03-26 18:47:45 -0700269
Dan Albert4c5a66b2019-06-24 14:35:35 -0700270 // 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 Gaob8afeea2019-02-13 15:27:28 -0800274 if (rc == -1) {
275 PLOG(FATAL) << "poll on USB control fd failed";
Dan Albert4c5a66b2019-06-24 14:35:35 -0700276 } else if (rc == 0) {
277 LOG(WARNING) << "timed out while waiting for FUNCTIONFS_BIND, trying again";
278 break;
Josh Gaob8afeea2019-02-13 15:27:28 -0800279 }
280
281 if (pfd[1].revents) {
Dan Albert4c5a66b2019-06-24 14:35:35 -0700282 // We were told to die.
283 break;
Josh Gao3a34bc52018-10-11 16:33:05 -0700284 }
285
286 struct usb_functionfs_event event;
Dan Albert4c5a66b2019-06-24 14:35:35 -0700287 rc = TEMP_FAILURE_RETRY(adb_read(control_fd_.get(), &event, sizeof(event)));
Josh Gao370dbb02019-05-10 11:37:34 -0700288 if (rc == -1) {
Josh Gao3a34bc52018-10-11 16:33:05 -0700289 PLOG(FATAL) << "failed to read functionfs event";
Josh Gao370dbb02019-05-10 11:37:34 -0700290 } 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 Gao3a34bc52018-10-11 16:33:05 -0700296 }
297
298 LOG(INFO) << "USB event: "
Dan Albert4c5a66b2019-06-24 14:35:35 -0700299 << to_string(static_cast<usb_functionfs_event_type>(event.type));
Josh Gao3a34bc52018-10-11 16:33:05 -0700300
301 switch (event.type) {
302 case FUNCTIONFS_BIND:
Dan Albert4c5a66b2019-06-24 14:35:35 -0700303 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 Gao3a34bc52018-10-11 16:33:05 -0700316 break;
317
318 case FUNCTIONFS_ENABLE:
Dan Albert4c5a66b2019-06-24 14:35:35 -0700319 if (!bound) {
320 LOG(WARNING) << "received FUNCTIONFS_ENABLE while not bound?";
321 running = false;
322 break;
323 }
324
Josh Gaoe3fa0c82019-03-28 11:05:53 -0700325 if (enabled) {
326 LOG(WARNING) << "received FUNCTIONFS_ENABLE while already enabled?";
327 running = false;
Josh Gao56463fc2019-05-01 16:53:53 -0700328 break;
Josh Gaoe3fa0c82019-03-28 11:05:53 -0700329 }
330
331 enabled = true;
Josh Gao3a34bc52018-10-11 16:33:05 -0700332 StartWorker();
333 break;
334
335 case FUNCTIONFS_DISABLE:
Dan Albert4c5a66b2019-06-24 14:35:35 -0700336 if (!bound) {
337 LOG(WARNING) << "received FUNCTIONFS_DISABLE while not bound?";
338 }
339
Josh Gaoe3fa0c82019-03-28 11:05:53 -0700340 if (!enabled) {
341 LOG(WARNING) << "received FUNCTIONFS_DISABLE while not enabled?";
342 }
343
344 enabled = false;
Josh Gao961496c2019-03-26 13:21:42 -0700345 running = false;
346 break;
347
348 case FUNCTIONFS_UNBIND:
Josh Gaoe3fa0c82019-03-28 11:05:53 -0700349 if (enabled) {
350 LOG(WARNING) << "received FUNCTIONFS_UNBIND while still enabled?";
351 }
Josh Gao961496c2019-03-26 13:21:42 -0700352
Dan Albert4c5a66b2019-06-24 14:35:35 -0700353 if (!bound) {
354 LOG(WARNING) << "received FUNCTIONFS_UNBIND when not bound?";
355 }
356
357 bound = false;
Josh Gao3a34bc52018-10-11 16:33:05 -0700358 running = false;
359 break;
Josh Gao7be217e2019-05-15 18:03:29 -0700360
361 case FUNCTIONFS_SETUP: {
Dan Albert4c5a66b2019-06-24 14:35:35 -0700362 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 Gao7be217e2019-05-15 18:03:29 -0700391 }
Josh Gao3a34bc52018-10-11 16:33:05 -0700392 }
393 }
394
Josh Gao5f572772019-02-28 13:29:32 -0800395 StopWorker();
Josh Gaodcf36882019-03-26 18:47:45 -0700396 HandleError("monitor thread finished");
Josh Gao3a34bc52018-10-11 16:33:05 -0700397 });
398 }
399
400 void StartWorker() {
Josh Gaodcf36882019-03-26 18:47:45 -0700401 CHECK(!worker_started_);
402 worker_started_ = true;
Josh Gao3a34bc52018-10-11 16:33:05 -0700403 worker_thread_ = std::thread([this]() {
404 adb_thread_setname("UsbFfs-worker");
Josh Gaobd4fb2d2020-02-26 15:44:30 -0800405 LOG(INFO) << "UsbFfs-worker thread spawned";
406
Josh Gao3a34bc52018-10-11 16:33:05 -0700407 for (size_t i = 0; i < kUsbReadQueueDepth; ++i) {
408 read_requests_[i] = CreateReadBlock(next_read_id_++);
Josh Gaob8afeea2019-02-13 15:27:28 -0800409 if (!SubmitRead(&read_requests_[i])) {
410 return;
411 }
Josh Gao3a34bc52018-10-11 16:33:05 -0700412 }
413
414 while (!stopped_) {
415 uint64_t dummy;
Josh Gaob8afeea2019-02-13 15:27:28 -0800416 ssize_t rc = adb_read(worker_event_fd_.get(), &dummy, sizeof(dummy));
Josh Gao3a34bc52018-10-11 16:33:05 -0700417 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 Gao961496c2019-03-26 13:21:42 -0700423 ReadEvents();
Josh Gao58cc5152020-05-19 15:57:06 -0700424
425 std::lock_guard<std::mutex> lock(write_mutex_);
426 SubmitWrites();
Josh Gao3a34bc52018-10-11 16:33:05 -0700427 }
428 });
429 }
430
Josh Gao5f572772019-02-28 13:29:32 -0800431 void StopWorker() {
Josh Gaodcf36882019-03-26 18:47:45 -0700432 if (!worker_started_) {
433 return;
434 }
435
Josh Gao5f572772019-02-28 13:29:32 -0800436 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 Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700459 void PrepareReadBlock(IoReadBlock* block, uint64_t id) {
Josh Gao3a34bc52018-10-11 16:33:05 -0700460 block->pending = false;
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700461 if (block->payload.capacity() >= kUsbReadSize) {
462 block->payload.resize(kUsbReadSize);
463 } else {
464 block->payload = Block(kUsbReadSize);
465 }
Josh Gao3a34bc52018-10-11 16:33:05 -0700466 block->control.aio_data = static_cast<uint64_t>(TransferId::read(id));
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700467 block->control.aio_buf = reinterpret_cast<uintptr_t>(block->payload.data());
468 block->control.aio_nbytes = block->payload.size();
Josh Gao3a34bc52018-10-11 16:33:05 -0700469 }
470
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700471 IoReadBlock CreateReadBlock(uint64_t id) {
472 IoReadBlock block;
Josh Gao3a34bc52018-10-11 16:33:05 -0700473 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 Gaob8afeea2019-02-13 15:27:28 -0800480 block.control.aio_resfd = worker_event_fd_.get();
Josh Gao3a34bc52018-10-11 16:33:05 -0700481 return block;
482 }
483
Josh Gao961496c2019-03-26 13:21:42 -0700484 void ReadEvents() {
Josh Gao3a34bc52018-10-11 16:33:05 -0700485 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 Gao40c84a32021-05-06 16:31:44 -0700499 // 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 Gao3a34bc52018-10-11 16:33:05 -0700518 }
519
520 if (id.direction == TransferDirection::READ) {
Josh Gao40c84a32021-05-06 16:31:44 -0700521 connection_started_ = true;
Josh Gao69482442020-02-28 14:53:56 -0800522 if (!HandleRead(id, event.res)) {
523 return;
524 }
Josh Gao3a34bc52018-10-11 16:33:05 -0700525 } else {
526 HandleWrite(id);
527 }
528 }
529 }
530
Josh Gao69482442020-02-28 14:53:56 -0800531 bool HandleRead(TransferId id, int64_t size) {
Josh Gao3a34bc52018-10-11 16:33:05 -0700532 uint64_t read_idx = id.id % kUsbReadQueueDepth;
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700533 IoReadBlock* block = &read_requests_[read_idx];
Josh Gao3a34bc52018-10-11 16:33:05 -0700534 block->pending = false;
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700535 block->payload.resize(size);
Josh Gao3a34bc52018-10-11 16:33:05 -0700536
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 Gao69482442020-02-28 14:53:56 -0800541 return true;
Josh Gao3a34bc52018-10-11 16:33:05 -0700542 }
543
544 for (uint64_t id = needed_read_id_;; ++id) {
545 size_t read_idx = id % kUsbReadQueueDepth;
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700546 IoReadBlock* current_block = &read_requests_[read_idx];
Josh Gao3a34bc52018-10-11 16:33:05 -0700547 if (current_block->pending) {
548 break;
549 }
Josh Gao69482442020-02-28 14:53:56 -0800550 if (!ProcessRead(current_block)) {
551 return false;
552 }
Josh Gao3a34bc52018-10-11 16:33:05 -0700553 ++needed_read_id_;
554 }
Josh Gao69482442020-02-28 14:53:56 -0800555
556 return true;
Josh Gao3a34bc52018-10-11 16:33:05 -0700557 }
558
Josh Gao69482442020-02-28 14:53:56 -0800559 bool ProcessRead(IoReadBlock* block) {
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700560 if (!block->payload.empty()) {
Josh Gao3a34bc52018-10-11 16:33:05 -0700561 if (!incoming_header_.has_value()) {
Josh Gao69482442020-02-28 14:53:56 -0800562 if (block->payload.size() != sizeof(amessage)) {
563 HandleError("received packet of unexpected length while reading header");
564 return false;
565 }
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700566 amessage& msg = incoming_header_.emplace();
567 memcpy(&msg, block->payload.data(), sizeof(msg));
Josh Gao3a34bc52018-10-11 16:33:05 -0700568 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 Gao69482442020-02-28 14:53:56 -0800572 if (block->payload.size() > bytes_left) {
573 HandleError("received too many bytes while waiting for payload");
574 return false;
575 }
Mayank Rana1cc42d32020-09-11 11:40:00 -0700576 incoming_payload_.append(std::move(block->payload));
Josh Gao3a34bc52018-10-11 16:33:05 -0700577 }
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 Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700584 packet->payload = std::move(incoming_payload_).coalesce();
Josh Gao7852ca42021-06-17 04:17:25 -0700585 transport_->HandleRead(std::move(packet));
Josh Gao3a34bc52018-10-11 16:33:05 -0700586
587 incoming_header_.reset();
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700588 // 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 Gao3a34bc52018-10-11 16:33:05 -0700593 }
594 }
595
596 PrepareReadBlock(block, block->id().id + kUsbReadQueueDepth);
597 SubmitRead(block);
Josh Gao69482442020-02-28 14:53:56 -0800598 return true;
Josh Gao3a34bc52018-10-11 16:33:05 -0700599 }
600
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700601 bool SubmitRead(IoReadBlock* block) {
Josh Gao3a34bc52018-10-11 16:33:05 -0700602 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 Gaob8afeea2019-02-13 15:27:28 -0800606 return false;
Josh Gao3a34bc52018-10-11 16:33:05 -0700607 }
Josh Gaob8afeea2019-02-13 15:27:28 -0800608
Josh Gaob8afeea2019-02-13 15:27:28 -0800609 return true;
Josh Gao3a34bc52018-10-11 16:33:05 -0700610 }
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 Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700616 return static_cast<uint64_t>(req.id()) == static_cast<uint64_t>(id);
Josh Gao3a34bc52018-10-11 16:33:05 -0700617 });
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 Gao3a34bc52018-10-11 16:33:05 -0700623 }
624
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700625 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 Gao3a34bc52018-10-11 16:33:05 -0700639 return block;
640 }
641
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700642 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 Gaob9727b02019-02-26 17:53:52 -0800645 }
646
Josh Gao3a34bc52018-10-11 16:33:05 -0700647 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 Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700661 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 Gao3a34bc52018-10-11 16:33:05 -0700664 LOG(VERBOSE) << "submitting write_request " << static_cast<void*>(iocbs[i]);
665 }
666
Josh Gaocc14d392019-03-26 13:06:38 -0700667 writes_submitted_ += writes_to_submit;
668
Josh Gao3a34bc52018-10-11 16:33:05 -0700669 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 Gao3a34bc52018-10-11 16:33:05 -0700677 }
678
679 void HandleError(const std::string& error) {
680 std::call_once(error_flag_, [&]() {
Josh Gao7852ca42021-06-17 04:17:25 -0700681 if (transport_) {
682 transport_->HandleError(error);
683 }
684
Josh Gao3a34bc52018-10-11 16:33:05 -0700685 if (!stopped_) {
686 Stop();
687 }
688 });
689 }
690
691 std::thread monitor_thread_;
Josh Gaodcf36882019-03-26 18:47:45 -0700692
693 bool worker_started_;
Josh Gao3a34bc52018-10-11 16:33:05 -0700694 std::thread worker_thread_;
695
696 std::atomic<bool> stopped_;
697 std::promise<void> destruction_notifier_;
698 std::once_flag error_flag_;
699
Josh Gaob8afeea2019-02-13 15:27:28 -0800700 unique_fd worker_event_fd_;
Dan Albert4c5a66b2019-06-24 14:35:35 -0700701 unique_fd monitor_event_fd_;
Josh Gao3a34bc52018-10-11 16:33:05 -0700702
703 ScopedAioContext aio_context_;
Dan Albert4c5a66b2019-06-24 14:35:35 -0700704 unique_fd control_fd_;
Josh Gao3a34bc52018-10-11 16:33:05 -0700705 unique_fd read_fd_;
706 unique_fd write_fd_;
707
Josh Gao40c84a32021-05-06 16:31:44 -0700708 bool connection_started_ = false;
Josh Gao3a34bc52018-10-11 16:33:05 -0700709 std::optional<amessage> incoming_header_;
710 IOVector incoming_payload_;
711
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700712 std::array<IoReadBlock, kUsbReadQueueDepth> read_requests_;
Josh Gao3a34bc52018-10-11 16:33:05 -0700713 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 Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700722 std::deque<IoWriteBlock> write_requests_ GUARDED_BY(write_mutex_);
Josh Gao3a34bc52018-10-11 16:33:05 -0700723 size_t next_write_id_ GUARDED_BY(write_mutex_) = 0;
724 size_t writes_submitted_ GUARDED_BY(write_mutex_) = 0;
Josh Gao5f572772019-02-28 13:29:32 -0800725
726 static constexpr int kInterruptionSignal = SIGUSR1;
Josh Gao3a34bc52018-10-11 16:33:05 -0700727};
728
729static void usb_ffs_open_thread() {
730 adb_thread_setname("usb ffs open");
731
Josh Gaoa42a56d2021-03-22 13:56:40 -0700732 // 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 Gao3a34bc52018-10-11 16:33:05 -0700746 while (true) {
Dan Albert4c5a66b2019-06-24 14:35:35 -0700747 unique_fd control;
748 unique_fd bulk_out;
749 unique_fd bulk_in;
Josh Gao3a34bc52018-10-11 16:33:05 -0700750 if (!open_functionfs(&control, &bulk_out, &bulk_in)) {
751 std::this_thread::sleep_for(1s);
752 continue;
753 }
754
Jack Phame44a4e72021-05-24 21:32:50 -0700755 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 Gao3a34bc52018-10-11 16:33:05 -0700761 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 Albert4c5a66b2019-06-24 14:35:35 -0700766 std::move(control), std::move(bulk_out), std::move(bulk_in),
Josh Gao3a34bc52018-10-11 16:33:05 -0700767 std::move(destruction_notifier)));
768 register_transport(transport);
769 future.wait();
770 }
771}
772
Josh Gao3a34bc52018-10-11 16:33:05 -0700773void usb_init() {
Josh Gao6b55e752020-03-27 18:09:56 -0700774 std::thread(usb_ffs_open_thread).detach();
Josh Gao3a34bc52018-10-11 16:33:05 -0700775}