blob: 7fbccc36e6b24dbbb7ae66cbe23b8c0e1ecd8f7e [file] [log] [blame]
The Android Open Source Projectdd7bc332009-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 Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG USB
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <dirent.h>
22#include <errno.h>
Dan Albert76649012015-02-24 15:51:19 -080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/ioctl.h>
Jerry Zhangecee4342017-07-18 14:07:57 -070027#include <sys/mman.h>
Dan Albert76649012015-02-24 15:51:19 -080028#include <sys/types.h>
29#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
Josh Gao613cbb42018-10-08 14:20:29 -070031#include <linux/usb/ch9.h>
32#include <linux/usb/functionfs.h>
33
Josh Gaoae72b5a2015-12-17 13:45:18 -080034#include <algorithm>
Yabin Cui9b53e4c2016-04-05 14:51:52 -070035#include <atomic>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080036#include <chrono>
Josh Gao0cd3ae12016-09-21 12:37:10 -070037#include <condition_variable>
38#include <mutex>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080039#include <thread>
Yabin Cui9b53e4c2016-04-05 14:51:52 -070040
41#include <android-base/logging.h>
Elliott Hughesffdec182016-09-23 15:40:03 -070042#include <android-base/properties.h>
Josh Gaoae72b5a2015-12-17 13:45:18 -080043
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044#include "adb.h"
Jerry Zhangb156c602018-05-07 15:14:47 -070045#include "adbd/usb.h"
Dan Albert76649012015-02-24 15:51:19 -080046#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080048using namespace std::chrono_literals;
49
Josh Gao44c688c2017-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 Pietrasiewiczfd96db12012-01-13 15:13:46 +010053
Jerry Zhang55205a52017-01-04 12:34:38 -080054#define USB_FFS_BULK_SIZE 16384
Josh Gaoae72b5a2015-12-17 13:45:18 -080055
Jerry Zhangecee4342017-07-18 14:07:57 -070056// Number of buffers needed to fit MAX_PAYLOAD, with an extra for ZLPs.
Jerry Zhangcda7c3b2018-05-21 14:22:43 -070057#define USB_FFS_NUM_BUFS ((4 * MAX_PAYLOAD / USB_FFS_BULK_SIZE) + 1)
Jerry Zhangecee4342017-07-18 14:07:57 -070058
Josh Gao860cc5a2018-08-21 15:44:49 -070059static unique_fd& dummy_fd = *new unique_fd();
Yabin Cui9b53e4c2016-04-05 14:51:52 -070060
Jerry Zhangcda7c3b2018-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 Zhangecee4342017-07-18 14:07:57 -070065 aiob->num_submitted = 0;
Jerry Zhangcda7c3b2018-05-21 14:22:43 -070066 for (unsigned i = 0; i < num_bufs; i++) {
Jerry Zhangecee4342017-07-18 14:07:57 -070067 aiob->iocbs[i] = &aiob->iocb[i];
68 }
Jerry Zhangd8c1ae92018-05-15 16:30:10 -070069 memset(&aiob->ctx, 0, sizeof(aiob->ctx));
Jerry Zhangcda7c3b2018-05-21 14:22:43 -070070 if (io_setup(num_bufs, &aiob->ctx)) {
Jerry Zhangd8c1ae92018-05-15 16:30:10 -070071 D("[ aio: got error on io_setup (%d) ]", errno);
72 }
Jerry Zhangecee4342017-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 Zhangb156c602018-05-07 15:14:47 -070085static bool init_functionfs(struct usb_handle* h) {
Josh Gao184f5712017-07-26 11:06:55 -070086 LOG(INFO) << "initializing functionfs";
Josh Gao613cbb42018-10-08 14:20:29 -070087 if (!open_functionfs(&h->control, &h->bulk_out, &h->bulk_in)) {
88 return false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010089 }
90
Josh Gao613cbb42018-10-08 14:20:29 -070091 h->read_aiob.fd = h->bulk_out.get();
92 h->write_aiob.fd = h->bulk_in.get();
Jerry Zhangcda7c3b2018-05-21 14:22:43 -070093 h->reads_zero_packets = true;
Yabin Cui9b53e4c2016-04-05 14:51:52 -070094 return true;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010095}
96
Jerry Zhangb156c602018-05-07 15:14:47 -070097static void usb_ffs_open_thread(usb_handle *usb) {
Siva Velusamy49ee7cf2015-08-28 16:37:29 -070098 adb_thread_setname("usb ffs open");
99
Elliott Hughesa7090b92015-04-17 17:03:59 -0700100 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100101 // wait until the USB device needs opening
Josh Gao0cd3ae12016-09-21 12:37:10 -0700102 std::unique_lock<std::mutex> lock(usb->lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700103 while (!usb->open_new_connection) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700104 usb->notify.wait(lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700105 }
106 usb->open_new_connection = false;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700107 lock.unlock();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100108
Elliott Hughesa7090b92015-04-17 17:03:59 -0700109 while (true) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700110 if (init_functionfs(usb)) {
Josh Gao184f5712017-07-26 11:06:55 -0700111 LOG(INFO) << "functionfs successfully initialized";
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100112 break;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700113 }
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800114 std::this_thread::sleep_for(1s);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100115 }
116
Josh Gao184f5712017-07-26 11:06:55 -0700117 LOG(INFO) << "registering usb transport";
Yi Kongaed415c2018-07-13 18:15:16 -0700118 register_usb_transport(usb, nullptr, nullptr, 1);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100119 }
120
121 // never gets here
Josh Gaod9db09c2016-02-12 14:31:15 -0800122 abort();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100123}
124
Josh Gao6b531c42015-12-02 17:30:58 -0800125static int usb_ffs_write(usb_handle* h, const void* data, int len) {
Josh Gao860cc5a2018-08-21 15:44:49 -0700126 D("about to write (fd=%d, len=%d)", h->bulk_in.get(), len);
Josh Gao6b531c42015-12-02 17:30:58 -0800127
Josh Gao6b531c42015-12-02 17:30:58 -0800128 const char* buf = static_cast<const char*>(data);
Jerry Zhang16b78db2018-05-15 16:20:41 -0700129 int orig_len = len;
Josh Gao6b531c42015-12-02 17:30:58 -0800130 while (len > 0) {
Jerry Zhang99499f12018-03-14 14:57:31 -0700131 int write_len = std::min(USB_FFS_BULK_SIZE, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800132 int n = adb_write(h->bulk_in, buf, write_len);
133 if (n < 0) {
Josh Gao860cc5a2018-08-21 15:44:49 -0700134 D("ERROR: fd = %d, n = %d: %s", h->bulk_in.get(), n, strerror(errno));
Josh Gao6b531c42015-12-02 17:30:58 -0800135 return -1;
136 }
137 buf += n;
138 len -= n;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100139 }
Josh Gao6b531c42015-12-02 17:30:58 -0800140
Josh Gao860cc5a2018-08-21 15:44:49 -0700141 D("[ done fd=%d ]", h->bulk_in.get());
Jerry Zhang16b78db2018-05-15 16:20:41 -0700142 return orig_len;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100143}
144
Josh Gao6b531c42015-12-02 17:30:58 -0800145static int usb_ffs_read(usb_handle* h, void* data, int len) {
Josh Gao860cc5a2018-08-21 15:44:49 -0700146 D("about to read (fd=%d, len=%d)", h->bulk_out.get(), len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100147
Josh Gao6b531c42015-12-02 17:30:58 -0800148 char* buf = static_cast<char*>(data);
Jerry Zhang16b78db2018-05-15 16:20:41 -0700149 int orig_len = len;
Josh Gao6b531c42015-12-02 17:30:58 -0800150 while (len > 0) {
Jerry Zhang99499f12018-03-14 14:57:31 -0700151 int read_len = std::min(USB_FFS_BULK_SIZE, len);
Josh Gao0b195402015-12-16 11:00:08 -0800152 int n = adb_read(h->bulk_out, buf, read_len);
Josh Gao6b531c42015-12-02 17:30:58 -0800153 if (n < 0) {
Josh Gao860cc5a2018-08-21 15:44:49 -0700154 D("ERROR: fd = %d, n = %d: %s", h->bulk_out.get(), n, strerror(errno));
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700155 return -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100156 }
Josh Gao6b531c42015-12-02 17:30:58 -0800157 buf += n;
158 len -= n;
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700159 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100160
Josh Gao860cc5a2018-08-21 15:44:49 -0700161 D("[ done fd=%d ]", h->bulk_out.get());
Jerry Zhang16b78db2018-05-15 16:20:41 -0700162 return orig_len;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100163}
164
Jerry Zhangecee4342017-07-18 14:07:57 -0700165static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
166 aio_block* aiob = read ? &h->read_aiob : &h->write_aiob;
167 bool zero_packet = false;
168
Jerry Zhangcda7c3b2018-05-21 14:22:43 -0700169 int num_bufs = len / h->io_size + (len % h->io_size == 0 ? 0 : 1);
Jerry Zhangecee4342017-07-18 14:07:57 -0700170 const char* cur_data = reinterpret_cast<const char*>(data);
171 int packet_size = getMaxPacketSize(aiob->fd);
172
173 if (posix_madvise(const_cast<void*>(data), len, POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED) <
174 0) {
175 D("[ Failed to madvise: %d ]", errno);
176 }
177
178 for (int i = 0; i < num_bufs; i++) {
Jerry Zhangcda7c3b2018-05-21 14:22:43 -0700179 int buf_len = std::min(len, static_cast<int>(h->io_size));
Jerry Zhangecee4342017-07-18 14:07:57 -0700180 io_prep(&aiob->iocb[i], aiob->fd, cur_data, buf_len, 0, read);
181
182 len -= buf_len;
183 cur_data += buf_len;
184
185 if (len == 0 && buf_len % packet_size == 0 && read) {
186 // adb does not expect the device to send a zero packet after data transfer,
187 // but the host *does* send a zero packet for the device to read.
Jerry Zhangcda7c3b2018-05-21 14:22:43 -0700188 zero_packet = h->reads_zero_packets;
Jerry Zhangecee4342017-07-18 14:07:57 -0700189 }
190 }
191 if (zero_packet) {
192 io_prep(&aiob->iocb[num_bufs], aiob->fd, reinterpret_cast<const void*>(cur_data),
193 packet_size, 0, read);
194 num_bufs += 1;
195 }
196
Jerry Zhang5ba68022018-03-19 17:54:39 -0700197 while (true) {
198 if (TEMP_FAILURE_RETRY(io_submit(aiob->ctx, num_bufs, aiob->iocbs.data())) < num_bufs) {
199 PLOG(ERROR) << "aio: got error submitting " << (read ? "read" : "write");
Jerry Zhangecee4342017-07-18 14:07:57 -0700200 return -1;
201 }
Jerry Zhang5ba68022018-03-19 17:54:39 -0700202 if (TEMP_FAILURE_RETRY(io_getevents(aiob->ctx, num_bufs, num_bufs, aiob->events.data(),
203 nullptr)) < num_bufs) {
204 PLOG(ERROR) << "aio: got error waiting " << (read ? "read" : "write");
205 return -1;
206 }
207 if (num_bufs == 1 && aiob->events[0].res == -EINTR) {
208 continue;
209 }
Jerry Zhang16b78db2018-05-15 16:20:41 -0700210 int ret = 0;
Jerry Zhang5ba68022018-03-19 17:54:39 -0700211 for (int i = 0; i < num_bufs; i++) {
212 if (aiob->events[i].res < 0) {
213 errno = -aiob->events[i].res;
214 PLOG(ERROR) << "aio: got error event on " << (read ? "read" : "write")
215 << " total bufs " << num_bufs;
216 return -1;
217 }
Jerry Zhang16b78db2018-05-15 16:20:41 -0700218 ret += aiob->events[i].res;
Jerry Zhang5ba68022018-03-19 17:54:39 -0700219 }
Jerry Zhang16b78db2018-05-15 16:20:41 -0700220 return ret;
Jerry Zhangecee4342017-07-18 14:07:57 -0700221 }
Jerry Zhangecee4342017-07-18 14:07:57 -0700222}
223
224static int usb_ffs_aio_read(usb_handle* h, void* data, int len) {
225 return usb_ffs_do_aio(h, data, len, true);
226}
227
228static int usb_ffs_aio_write(usb_handle* h, const void* data, int len) {
229 return usb_ffs_do_aio(h, data, len, false);
230}
231
Josh Gao183b73e2017-01-11 14:39:19 -0800232static void usb_ffs_kick(usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100233 int err;
234
Josh Gao860cc5a2018-08-21 15:44:49 -0700235 err = ioctl(h->bulk_in.get(), FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700236 if (err < 0) {
Josh Gao860cc5a2018-08-21 15:44:49 -0700237 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in.get(), errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700238 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100239
Josh Gao860cc5a2018-08-21 15:44:49 -0700240 err = ioctl(h->bulk_out.get(), FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700241 if (err < 0) {
Josh Gao860cc5a2018-08-21 15:44:49 -0700242 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out.get(), errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700243 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100244
Jack Pham4cbf1d82013-12-23 17:46:10 -0800245 // don't close ep0 here, since we may not need to reinitialize it with
246 // the same descriptors again. if however ep1/ep2 fail to re-open in
247 // init_functionfs, only then would we close and open ep0 again.
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700248 // Ditto the comment in usb_adb_kick.
249 h->kicked = true;
Josh Gao860cc5a2018-08-21 15:44:49 -0700250 TEMP_FAILURE_RETRY(dup2(dummy_fd.get(), h->bulk_out.get()));
251 TEMP_FAILURE_RETRY(dup2(dummy_fd.get(), h->bulk_in.get()));
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700252}
253
Josh Gao183b73e2017-01-11 14:39:19 -0800254static void usb_ffs_close(usb_handle* h) {
Josh Gao184f5712017-07-26 11:06:55 -0700255 LOG(INFO) << "closing functionfs transport";
256
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700257 h->kicked = false;
Josh Gao860cc5a2018-08-21 15:44:49 -0700258 h->bulk_out.reset();
259 h->bulk_in.reset();
Jerry Zhangecee4342017-07-18 14:07:57 -0700260
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700261 // Notify usb_adb_open_thread to open a new connection.
Josh Gao0cd3ae12016-09-21 12:37:10 -0700262 h->lock.lock();
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700263 h->open_new_connection = true;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700264 h->lock.unlock();
265 h->notify.notify_one();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266}
267
Jerry Zhangcda7c3b2018-05-21 14:22:43 -0700268usb_handle *create_usb_handle(unsigned num_bufs, unsigned io_size) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700269 usb_handle* h = new usb_handle();
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700270
Jerry Zhangecee4342017-07-18 14:07:57 -0700271 if (android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false)) {
272 // Devices on older kernels (< 3.18) will not have aio support for ffs
273 // unless backported. Fall back on the non-aio functions instead.
274 h->write = usb_ffs_write;
275 h->read = usb_ffs_read;
276 } else {
277 h->write = usb_ffs_aio_write;
278 h->read = usb_ffs_aio_read;
Jerry Zhangcda7c3b2018-05-21 14:22:43 -0700279 aio_block_init(&h->read_aiob, num_bufs);
280 aio_block_init(&h->write_aiob, num_bufs);
Jerry Zhangecee4342017-07-18 14:07:57 -0700281 }
Jerry Zhangcda7c3b2018-05-21 14:22:43 -0700282 h->io_size = io_size;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100283 h->kick = usb_ffs_kick;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700284 h->close = usb_ffs_close;
Jerry Zhangb156c602018-05-07 15:14:47 -0700285 return h;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100286}
287
Josh Gao183b73e2017-01-11 14:39:19 -0800288void usb_init() {
Jerry Zhangb156c602018-05-07 15:14:47 -0700289 D("[ usb_init - using FunctionFS ]");
Josh Gao860cc5a2018-08-21 15:44:49 -0700290 dummy_fd.reset(adb_open("/dev/null", O_WRONLY | O_CLOEXEC));
291 CHECK_NE(-1, dummy_fd.get());
Jerry Zhangb156c602018-05-07 15:14:47 -0700292
Jerry Zhangcda7c3b2018-05-21 14:22:43 -0700293 std::thread(usb_ffs_open_thread, create_usb_handle(USB_FFS_NUM_BUFS, USB_FFS_BULK_SIZE)).detach();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100294}
295
Josh Gao183b73e2017-01-11 14:39:19 -0800296int usb_write(usb_handle* h, const void* data, int len) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100297 return h->write(h, data, len);
298}
299
Josh Gao183b73e2017-01-11 14:39:19 -0800300int usb_read(usb_handle* h, void* data, int len) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100301 return h->read(h, data, len);
302}
Josh Gao0cd3ae12016-09-21 12:37:10 -0700303
Josh Gao183b73e2017-01-11 14:39:19 -0800304int usb_close(usb_handle* h) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700305 h->close(h);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306 return 0;
307}
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100308
Josh Gao183b73e2017-01-11 14:39:19 -0800309void usb_kick(usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100310 h->kick(h);
311}