blob: fe80e7d5962a0e70953a023909eace62dfb6feca [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG USB
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080021#include <dirent.h>
22#include <errno.h>
Dan Albertb302d122015-02-24 15:51:19 -080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/ioctl.h>
Jerry Zhanga2cb8de2017-07-18 14:07:57 -070027#include <sys/mman.h>
Dan Albertb302d122015-02-24 15:51:19 -080028#include <sys/types.h>
29#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080030
Josh Gaoe485ce82018-10-08 14:20:29 -070031#include <linux/usb/ch9.h>
32#include <linux/usb/functionfs.h>
33
Josh Gaoc95bb3d2015-12-17 13:45:18 -080034#include <algorithm>
Yabin Cui6760b2a2016-04-05 14:51:52 -070035#include <atomic>
Elliott Hughes73925982016-11-15 12:37:32 -080036#include <chrono>
Josh Gaoe7daf572016-09-21 12:37:10 -070037#include <condition_variable>
38#include <mutex>
Elliott Hughes73925982016-11-15 12:37:32 -080039#include <thread>
Yabin Cui6760b2a2016-04-05 14:51:52 -070040
41#include <android-base/logging.h>
Elliott Hughes8b249d22016-09-23 15:40:03 -070042#include <android-base/properties.h>
Josh Gaoc95bb3d2015-12-17 13:45:18 -080043
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080044#include "adb.h"
Jerry Zhangd4fe8b62018-05-07 15:14:47 -070045#include "adbd/usb.h"
Dan Albertb302d122015-02-24 15:51:19 -080046#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080047
Elliott Hughes73925982016-11-15 12:37:32 -080048using namespace std::chrono_literals;
49
Josh Gao484aaba2017-01-11 17:37:35 -080050#define MAX_PACKET_SIZE_FS 64
51#define MAX_PACKET_SIZE_HS 512
52#define MAX_PACKET_SIZE_SS 1024
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010053
Jerry Zhang76f9c722017-01-04 12:34:38 -080054#define USB_FFS_BULK_SIZE 16384
Josh Gaoc95bb3d2015-12-17 13:45:18 -080055
Jerry Zhanga2cb8de2017-07-18 14:07:57 -070056// Number of buffers needed to fit MAX_PAYLOAD, with an extra for ZLPs.
Jerry Zhang01ee27f2018-05-21 14:22:43 -070057#define USB_FFS_NUM_BUFS ((4 * MAX_PAYLOAD / USB_FFS_BULK_SIZE) + 1)
Jerry Zhanga2cb8de2017-07-18 14:07:57 -070058
Josh Gao9fce5642018-08-21 15:44:49 -070059static unique_fd& dummy_fd = *new unique_fd();
Yabin Cui6760b2a2016-04-05 14:51:52 -070060
Jerry Zhang01ee27f2018-05-21 14:22:43 -070061static void aio_block_init(aio_block* aiob, unsigned num_bufs) {
62 aiob->iocb.resize(num_bufs);
63 aiob->iocbs.resize(num_bufs);
64 aiob->events.resize(num_bufs);
Jerry Zhanga2cb8de2017-07-18 14:07:57 -070065 aiob->num_submitted = 0;
Jerry Zhang01ee27f2018-05-21 14:22:43 -070066 for (unsigned i = 0; i < num_bufs; i++) {
Jerry Zhanga2cb8de2017-07-18 14:07:57 -070067 aiob->iocbs[i] = &aiob->iocb[i];
68 }
Jerry Zhangcc20cc72018-05-15 16:30:10 -070069 memset(&aiob->ctx, 0, sizeof(aiob->ctx));
Jerry Zhang01ee27f2018-05-21 14:22:43 -070070 if (io_setup(num_bufs, &aiob->ctx)) {
Jerry Zhangcc20cc72018-05-15 16:30:10 -070071 D("[ aio: got error on io_setup (%d) ]", errno);
72 }
Jerry Zhanga2cb8de2017-07-18 14:07:57 -070073}
74
75static int getMaxPacketSize(int ffs_fd) {
76 usb_endpoint_descriptor desc;
77 if (ioctl(ffs_fd, FUNCTIONFS_ENDPOINT_DESC, reinterpret_cast<unsigned long>(&desc))) {
78 D("[ could not get endpoint descriptor! (%d) ]", errno);
79 return MAX_PACKET_SIZE_HS;
80 } else {
81 return desc.wMaxPacketSize;
82 }
83}
84
Jerry Zhangd4fe8b62018-05-07 15:14:47 -070085static bool init_functionfs(struct usb_handle* h) {
Josh Gao03eba902017-07-26 11:06:55 -070086 LOG(INFO) << "initializing functionfs";
Josh Gaoe485ce82018-10-08 14:20:29 -070087 if (!open_functionfs(&h->control, &h->bulk_out, &h->bulk_in)) {
88 return false;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010089 }
90
Josh Gaoe485ce82018-10-08 14:20:29 -070091 h->read_aiob.fd = h->bulk_out.get();
92 h->write_aiob.fd = h->bulk_in.get();
Jerry Zhang01ee27f2018-05-21 14:22:43 -070093 h->reads_zero_packets = true;
Yabin Cui6760b2a2016-04-05 14:51:52 -070094 return true;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010095}
96
Josh Gao3a34bc52018-10-11 16:33:05 -070097static void usb_legacy_ffs_open_thread(usb_handle* usb) {
98 adb_thread_setname("usb legacy ffs open");
Siva Velusamy2669cf92015-08-28 16:37:29 -070099
Elliott Hughes7cf35752015-04-17 17:03:59 -0700100 while (true) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100101 // wait until the USB device needs opening
Josh Gaoe7daf572016-09-21 12:37:10 -0700102 std::unique_lock<std::mutex> lock(usb->lock);
Yabin Cui6760b2a2016-04-05 14:51:52 -0700103 while (!usb->open_new_connection) {
Josh Gaoe7daf572016-09-21 12:37:10 -0700104 usb->notify.wait(lock);
Yabin Cui6760b2a2016-04-05 14:51:52 -0700105 }
106 usb->open_new_connection = false;
Josh Gaoe7daf572016-09-21 12:37:10 -0700107 lock.unlock();
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100108
Elliott Hughes7cf35752015-04-17 17:03:59 -0700109 while (true) {
Yabin Cui6760b2a2016-04-05 14:51:52 -0700110 if (init_functionfs(usb)) {
Josh Gao03eba902017-07-26 11:06:55 -0700111 LOG(INFO) << "functionfs successfully initialized";
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100112 break;
Yabin Cui6760b2a2016-04-05 14:51:52 -0700113 }
Elliott Hughes73925982016-11-15 12:37:32 -0800114 std::this_thread::sleep_for(1s);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100115 }
116
Josh Gao03eba902017-07-26 11:06:55 -0700117 LOG(INFO) << "registering usb transport";
Yi Kong86e67182018-07-13 18:15:16 -0700118 register_usb_transport(usb, nullptr, nullptr, 1);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100119 }
120
121 // never gets here
Josh Gao382ea6e2016-02-12 14:31:15 -0800122 abort();
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100123}
124
Josh Gao8e701252015-12-02 17:30:58 -0800125static int usb_ffs_write(usb_handle* h, const void* data, int len) {
Josh Gao9fce5642018-08-21 15:44:49 -0700126 D("about to write (fd=%d, len=%d)", h->bulk_in.get(), len);
Josh Gao8e701252015-12-02 17:30:58 -0800127
Josh Gao8e701252015-12-02 17:30:58 -0800128 const char* buf = static_cast<const char*>(data);
Jerry Zhang76d17db2018-05-15 16:20:41 -0700129 int orig_len = len;
Josh Gao8e701252015-12-02 17:30:58 -0800130 while (len > 0) {
Jerry Zhang7596c422018-03-14 14:57:31 -0700131 int write_len = std::min(USB_FFS_BULK_SIZE, len);
Josh Gao8e701252015-12-02 17:30:58 -0800132 int n = adb_write(h->bulk_in, buf, write_len);
133 if (n < 0) {
Josh Gao9fce5642018-08-21 15:44:49 -0700134 D("ERROR: fd = %d, n = %d: %s", h->bulk_in.get(), n, strerror(errno));
Josh Gao8e701252015-12-02 17:30:58 -0800135 return -1;
136 }
137 buf += n;
138 len -= n;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100139 }
Josh Gao8e701252015-12-02 17:30:58 -0800140
Josh Gao9fce5642018-08-21 15:44:49 -0700141 D("[ done fd=%d ]", h->bulk_in.get());
Jerry Zhang76d17db2018-05-15 16:20:41 -0700142 return orig_len;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100143}
144
chihhao.chen6f8d0f72019-05-20 16:55:55 +0800145static int usb_ffs_read(usb_handle* h, void* data, int len, bool allow_partial) {
Josh Gao9fce5642018-08-21 15:44:49 -0700146 D("about to read (fd=%d, len=%d)", h->bulk_out.get(), len);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100147
Josh Gao8e701252015-12-02 17:30:58 -0800148 char* buf = static_cast<char*>(data);
Jerry Zhang76d17db2018-05-15 16:20:41 -0700149 int orig_len = len;
chihhao.chen6f8d0f72019-05-20 16:55:55 +0800150 unsigned count = 0;
Josh Gao8e701252015-12-02 17:30:58 -0800151 while (len > 0) {
Jerry Zhang7596c422018-03-14 14:57:31 -0700152 int read_len = std::min(USB_FFS_BULK_SIZE, len);
Josh Gao83e329e2015-12-16 11:00:08 -0800153 int n = adb_read(h->bulk_out, buf, read_len);
Josh Gao8e701252015-12-02 17:30:58 -0800154 if (n < 0) {
Josh Gao9fce5642018-08-21 15:44:49 -0700155 D("ERROR: fd = %d, n = %d: %s", h->bulk_out.get(), n, strerror(errno));
Elliott Hughes0bd85872015-08-25 10:59:45 -0700156 return -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100157 }
Josh Gao8e701252015-12-02 17:30:58 -0800158 buf += n;
159 len -= n;
chihhao.chen6f8d0f72019-05-20 16:55:55 +0800160 count += n;
161
162 // For fastbootd command such as "getvar all", len parameter is always set 64.
163 // But what we read is actually less than 64.
164 // For example, length 10 for "getvar all" command.
165 // If we get less data than expected, this means there should be no more data.
166 if (allow_partial && n < read_len) {
167 orig_len = count;
168 break;
169 }
Elliott Hughes0bd85872015-08-25 10:59:45 -0700170 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100171
Josh Gao9fce5642018-08-21 15:44:49 -0700172 D("[ done fd=%d ]", h->bulk_out.get());
Jerry Zhang76d17db2018-05-15 16:20:41 -0700173 return orig_len;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100174}
175
Jerry Zhanga2cb8de2017-07-18 14:07:57 -0700176static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
177 aio_block* aiob = read ? &h->read_aiob : &h->write_aiob;
178 bool zero_packet = false;
179
Jerry Zhang01ee27f2018-05-21 14:22:43 -0700180 int num_bufs = len / h->io_size + (len % h->io_size == 0 ? 0 : 1);
Jerry Zhanga2cb8de2017-07-18 14:07:57 -0700181 const char* cur_data = reinterpret_cast<const char*>(data);
182 int packet_size = getMaxPacketSize(aiob->fd);
183
184 if (posix_madvise(const_cast<void*>(data), len, POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED) <
185 0) {
186 D("[ Failed to madvise: %d ]", errno);
187 }
188
189 for (int i = 0; i < num_bufs; i++) {
Jerry Zhang01ee27f2018-05-21 14:22:43 -0700190 int buf_len = std::min(len, static_cast<int>(h->io_size));
Jerry Zhanga2cb8de2017-07-18 14:07:57 -0700191 io_prep(&aiob->iocb[i], aiob->fd, cur_data, buf_len, 0, read);
192
193 len -= buf_len;
194 cur_data += buf_len;
195
196 if (len == 0 && buf_len % packet_size == 0 && read) {
197 // adb does not expect the device to send a zero packet after data transfer,
198 // but the host *does* send a zero packet for the device to read.
Jerry Zhang01ee27f2018-05-21 14:22:43 -0700199 zero_packet = h->reads_zero_packets;
Jerry Zhanga2cb8de2017-07-18 14:07:57 -0700200 }
201 }
202 if (zero_packet) {
203 io_prep(&aiob->iocb[num_bufs], aiob->fd, reinterpret_cast<const void*>(cur_data),
204 packet_size, 0, read);
205 num_bufs += 1;
206 }
207
Jerry Zhang92490e62018-03-19 17:54:39 -0700208 while (true) {
209 if (TEMP_FAILURE_RETRY(io_submit(aiob->ctx, num_bufs, aiob->iocbs.data())) < num_bufs) {
210 PLOG(ERROR) << "aio: got error submitting " << (read ? "read" : "write");
Jerry Zhanga2cb8de2017-07-18 14:07:57 -0700211 return -1;
212 }
Jerry Zhang92490e62018-03-19 17:54:39 -0700213 if (TEMP_FAILURE_RETRY(io_getevents(aiob->ctx, num_bufs, num_bufs, aiob->events.data(),
214 nullptr)) < num_bufs) {
215 PLOG(ERROR) << "aio: got error waiting " << (read ? "read" : "write");
216 return -1;
217 }
218 if (num_bufs == 1 && aiob->events[0].res == -EINTR) {
219 continue;
220 }
Jerry Zhang76d17db2018-05-15 16:20:41 -0700221 int ret = 0;
Jerry Zhang92490e62018-03-19 17:54:39 -0700222 for (int i = 0; i < num_bufs; i++) {
223 if (aiob->events[i].res < 0) {
224 errno = -aiob->events[i].res;
225 PLOG(ERROR) << "aio: got error event on " << (read ? "read" : "write")
226 << " total bufs " << num_bufs;
227 return -1;
228 }
Jerry Zhang76d17db2018-05-15 16:20:41 -0700229 ret += aiob->events[i].res;
Jerry Zhang92490e62018-03-19 17:54:39 -0700230 }
Jerry Zhang76d17db2018-05-15 16:20:41 -0700231 return ret;
Jerry Zhanga2cb8de2017-07-18 14:07:57 -0700232 }
Jerry Zhanga2cb8de2017-07-18 14:07:57 -0700233}
234
chihhao.chen6f8d0f72019-05-20 16:55:55 +0800235static int usb_ffs_aio_read(usb_handle* h, void* data, int len, bool allow_partial) {
Jerry Zhanga2cb8de2017-07-18 14:07:57 -0700236 return usb_ffs_do_aio(h, data, len, true);
237}
238
239static int usb_ffs_aio_write(usb_handle* h, const void* data, int len) {
240 return usb_ffs_do_aio(h, data, len, false);
241}
242
Josh Gao0dde9c82017-01-11 14:39:19 -0800243static void usb_ffs_kick(usb_handle* h) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100244 int err;
245
Josh Gao9fce5642018-08-21 15:44:49 -0700246 err = ioctl(h->bulk_in.get(), FUNCTIONFS_CLEAR_HALT);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700247 if (err < 0) {
Josh Gao9fce5642018-08-21 15:44:49 -0700248 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in.get(), errno);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700249 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100250
Josh Gao9fce5642018-08-21 15:44:49 -0700251 err = ioctl(h->bulk_out.get(), FUNCTIONFS_CLEAR_HALT);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700252 if (err < 0) {
Josh Gao9fce5642018-08-21 15:44:49 -0700253 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out.get(), errno);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700254 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100255
Jack Pham6c3cef52013-12-23 17:46:10 -0800256 // don't close ep0 here, since we may not need to reinitialize it with
257 // the same descriptors again. if however ep1/ep2 fail to re-open in
258 // init_functionfs, only then would we close and open ep0 again.
Yabin Cui6760b2a2016-04-05 14:51:52 -0700259 // Ditto the comment in usb_adb_kick.
260 h->kicked = true;
Josh Gao9fce5642018-08-21 15:44:49 -0700261 TEMP_FAILURE_RETRY(dup2(dummy_fd.get(), h->bulk_out.get()));
262 TEMP_FAILURE_RETRY(dup2(dummy_fd.get(), h->bulk_in.get()));
Yabin Cui6760b2a2016-04-05 14:51:52 -0700263}
264
Josh Gao0dde9c82017-01-11 14:39:19 -0800265static void usb_ffs_close(usb_handle* h) {
Josh Gao03eba902017-07-26 11:06:55 -0700266 LOG(INFO) << "closing functionfs transport";
267
Yabin Cui6760b2a2016-04-05 14:51:52 -0700268 h->kicked = false;
Josh Gao9fce5642018-08-21 15:44:49 -0700269 h->bulk_out.reset();
270 h->bulk_in.reset();
Jerry Zhanga2cb8de2017-07-18 14:07:57 -0700271
Yabin Cui6760b2a2016-04-05 14:51:52 -0700272 // Notify usb_adb_open_thread to open a new connection.
Josh Gaoe7daf572016-09-21 12:37:10 -0700273 h->lock.lock();
Yabin Cui6760b2a2016-04-05 14:51:52 -0700274 h->open_new_connection = true;
Josh Gaoe7daf572016-09-21 12:37:10 -0700275 h->lock.unlock();
276 h->notify.notify_one();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800277}
278
Josh Gaof0cb5462018-10-08 14:42:19 -0700279usb_handle* create_usb_handle(unsigned num_bufs, unsigned io_size) {
Josh Gaoe7daf572016-09-21 12:37:10 -0700280 usb_handle* h = new usb_handle();
Elliott Hughesd0269c92015-04-21 19:39:52 -0700281
Jerry Zhanga2cb8de2017-07-18 14:07:57 -0700282 if (android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false)) {
283 // Devices on older kernels (< 3.18) will not have aio support for ffs
284 // unless backported. Fall back on the non-aio functions instead.
285 h->write = usb_ffs_write;
286 h->read = usb_ffs_read;
287 } else {
288 h->write = usb_ffs_aio_write;
289 h->read = usb_ffs_aio_read;
Jerry Zhang01ee27f2018-05-21 14:22:43 -0700290 aio_block_init(&h->read_aiob, num_bufs);
291 aio_block_init(&h->write_aiob, num_bufs);
Jerry Zhanga2cb8de2017-07-18 14:07:57 -0700292 }
Jerry Zhang01ee27f2018-05-21 14:22:43 -0700293 h->io_size = io_size;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100294 h->kick = usb_ffs_kick;
Yabin Cui6760b2a2016-04-05 14:51:52 -0700295 h->close = usb_ffs_close;
Jerry Zhangd4fe8b62018-05-07 15:14:47 -0700296 return h;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100297}
298
Josh Gao3a34bc52018-10-11 16:33:05 -0700299void usb_init_legacy() {
300 D("[ usb_init - using legacy FunctionFS ]");
Josh Gao9fce5642018-08-21 15:44:49 -0700301 dummy_fd.reset(adb_open("/dev/null", O_WRONLY | O_CLOEXEC));
302 CHECK_NE(-1, dummy_fd.get());
Jerry Zhangd4fe8b62018-05-07 15:14:47 -0700303
Josh Gao3a34bc52018-10-11 16:33:05 -0700304 std::thread(usb_legacy_ffs_open_thread, create_usb_handle(USB_FFS_NUM_BUFS, USB_FFS_BULK_SIZE))
Josh Gaof0cb5462018-10-08 14:42:19 -0700305 .detach();
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100306}
307
Josh Gao0dde9c82017-01-11 14:39:19 -0800308int usb_write(usb_handle* h, const void* data, int len) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100309 return h->write(h, data, len);
310}
311
Josh Gao0dde9c82017-01-11 14:39:19 -0800312int usb_read(usb_handle* h, void* data, int len) {
chihhao.chen6f8d0f72019-05-20 16:55:55 +0800313 return h->read(h, data, len, false /* allow_partial */);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100314}
Josh Gaoe7daf572016-09-21 12:37:10 -0700315
Josh Gao0dde9c82017-01-11 14:39:19 -0800316int usb_close(usb_handle* h) {
Yabin Cui6760b2a2016-04-05 14:51:52 -0700317 h->close(h);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800318 return 0;
319}
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100320
Josh Gao3a2172b2019-03-28 15:47:44 -0700321void usb_reset(usb_handle* h) {
322 usb_close(h);
323}
324
Josh Gao0dde9c82017-01-11 14:39:19 -0800325void usb_kick(usb_handle* h) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100326 h->kick(h);
327}