blob: 4bee7b20c5c070555948165bef88e88f8b9c159b [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
Josh Gao08718242020-03-27 18:09:56 -070017#include "usb.h"
Dan Albert33134262015-03-19 15:21:08 -070018
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <dirent.h>
20#include <errno.h>
Dan Albert76649012015-02-24 15:51:19 -080021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/ioctl.h>
Jerry Zhangecee4342017-07-18 14:07:57 -070025#include <sys/mman.h>
Dan Albert76649012015-02-24 15:51:19 -080026#include <sys/types.h>
27#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Josh Gao613cbb42018-10-08 14:20:29 -070029#include <linux/usb/ch9.h>
30#include <linux/usb/functionfs.h>
31
Josh Gaoae72b5a2015-12-17 13:45:18 -080032#include <algorithm>
Yabin Cui9b53e4c2016-04-05 14:51:52 -070033#include <atomic>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080034#include <chrono>
Josh Gao0cd3ae12016-09-21 12:37:10 -070035#include <condition_variable>
36#include <mutex>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080037#include <thread>
Yabin Cui9b53e4c2016-04-05 14:51:52 -070038
39#include <android-base/logging.h>
Elliott Hughesffdec182016-09-23 15:40:03 -070040#include <android-base/properties.h>
Josh Gaoae72b5a2015-12-17 13:45:18 -080041
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080042using namespace std::chrono_literals;
43
Josh Gao08718242020-03-27 18:09:56 -070044#define D(...)
Josh Gao44c688c2017-01-11 17:37:35 -080045#define MAX_PACKET_SIZE_FS 64
46#define MAX_PACKET_SIZE_HS 512
47#define MAX_PACKET_SIZE_SS 1024
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010048
Jerry Zhang55205a52017-01-04 12:34:38 -080049#define USB_FFS_BULK_SIZE 16384
Josh Gaoae72b5a2015-12-17 13:45:18 -080050
Jerry Zhangecee4342017-07-18 14:07:57 -070051// Number of buffers needed to fit MAX_PAYLOAD, with an extra for ZLPs.
Jerry Zhangcda7c3b2018-05-21 14:22:43 -070052#define USB_FFS_NUM_BUFS ((4 * MAX_PAYLOAD / USB_FFS_BULK_SIZE) + 1)
Jerry Zhangecee4342017-07-18 14:07:57 -070053
Jerry Zhangcda7c3b2018-05-21 14:22:43 -070054static void aio_block_init(aio_block* aiob, unsigned num_bufs) {
55 aiob->iocb.resize(num_bufs);
56 aiob->iocbs.resize(num_bufs);
57 aiob->events.resize(num_bufs);
Jerry Zhangecee4342017-07-18 14:07:57 -070058 aiob->num_submitted = 0;
Jerry Zhangcda7c3b2018-05-21 14:22:43 -070059 for (unsigned i = 0; i < num_bufs; i++) {
Jerry Zhangecee4342017-07-18 14:07:57 -070060 aiob->iocbs[i] = &aiob->iocb[i];
61 }
Jerry Zhangd8c1ae92018-05-15 16:30:10 -070062 memset(&aiob->ctx, 0, sizeof(aiob->ctx));
Jerry Zhangcda7c3b2018-05-21 14:22:43 -070063 if (io_setup(num_bufs, &aiob->ctx)) {
Jerry Zhangd8c1ae92018-05-15 16:30:10 -070064 D("[ aio: got error on io_setup (%d) ]", errno);
65 }
Jerry Zhangecee4342017-07-18 14:07:57 -070066}
67
68static int getMaxPacketSize(int ffs_fd) {
69 usb_endpoint_descriptor desc;
70 if (ioctl(ffs_fd, FUNCTIONFS_ENDPOINT_DESC, reinterpret_cast<unsigned long>(&desc))) {
71 D("[ could not get endpoint descriptor! (%d) ]", errno);
72 return MAX_PACKET_SIZE_HS;
73 } else {
74 return desc.wMaxPacketSize;
75 }
76}
77
Josh Gao6b531c42015-12-02 17:30:58 -080078static int usb_ffs_write(usb_handle* h, const void* data, int len) {
Josh Gao860cc5a2018-08-21 15:44:49 -070079 D("about to write (fd=%d, len=%d)", h->bulk_in.get(), len);
Josh Gao6b531c42015-12-02 17:30:58 -080080
Josh Gao6b531c42015-12-02 17:30:58 -080081 const char* buf = static_cast<const char*>(data);
Jerry Zhang16b78db2018-05-15 16:20:41 -070082 int orig_len = len;
Josh Gao6b531c42015-12-02 17:30:58 -080083 while (len > 0) {
Jerry Zhang99499f12018-03-14 14:57:31 -070084 int write_len = std::min(USB_FFS_BULK_SIZE, len);
Josh Gao08718242020-03-27 18:09:56 -070085 int n = write(h->bulk_in, buf, write_len);
Josh Gao6b531c42015-12-02 17:30:58 -080086 if (n < 0) {
Josh Gao860cc5a2018-08-21 15:44:49 -070087 D("ERROR: fd = %d, n = %d: %s", h->bulk_in.get(), n, strerror(errno));
Josh Gao6b531c42015-12-02 17:30:58 -080088 return -1;
89 }
90 buf += n;
91 len -= n;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010092 }
Josh Gao6b531c42015-12-02 17:30:58 -080093
Josh Gao860cc5a2018-08-21 15:44:49 -070094 D("[ done fd=%d ]", h->bulk_in.get());
Jerry Zhang16b78db2018-05-15 16:20:41 -070095 return orig_len;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010096}
97
chihhao.chen8c544b62019-05-20 16:55:55 +080098static int usb_ffs_read(usb_handle* h, void* data, int len, bool allow_partial) {
Josh Gao860cc5a2018-08-21 15:44:49 -070099 D("about to read (fd=%d, len=%d)", h->bulk_out.get(), len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100100
Josh Gao6b531c42015-12-02 17:30:58 -0800101 char* buf = static_cast<char*>(data);
Jerry Zhang16b78db2018-05-15 16:20:41 -0700102 int orig_len = len;
chihhao.chen8c544b62019-05-20 16:55:55 +0800103 unsigned count = 0;
Josh Gao6b531c42015-12-02 17:30:58 -0800104 while (len > 0) {
Jerry Zhang99499f12018-03-14 14:57:31 -0700105 int read_len = std::min(USB_FFS_BULK_SIZE, len);
Josh Gao08718242020-03-27 18:09:56 -0700106 int n = read(h->bulk_out, buf, read_len);
Josh Gao6b531c42015-12-02 17:30:58 -0800107 if (n < 0) {
Josh Gao860cc5a2018-08-21 15:44:49 -0700108 D("ERROR: fd = %d, n = %d: %s", h->bulk_out.get(), n, strerror(errno));
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700109 return -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100110 }
Josh Gao6b531c42015-12-02 17:30:58 -0800111 buf += n;
112 len -= n;
chihhao.chen8c544b62019-05-20 16:55:55 +0800113 count += n;
114
115 // For fastbootd command such as "getvar all", len parameter is always set 64.
116 // But what we read is actually less than 64.
117 // For example, length 10 for "getvar all" command.
118 // If we get less data than expected, this means there should be no more data.
119 if (allow_partial && n < read_len) {
120 orig_len = count;
121 break;
122 }
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700123 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100124
Josh Gao860cc5a2018-08-21 15:44:49 -0700125 D("[ done fd=%d ]", h->bulk_out.get());
Jerry Zhang16b78db2018-05-15 16:20:41 -0700126 return orig_len;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100127}
128
Jerry Zhangecee4342017-07-18 14:07:57 -0700129static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
130 aio_block* aiob = read ? &h->read_aiob : &h->write_aiob;
131 bool zero_packet = false;
132
Jerry Zhangcda7c3b2018-05-21 14:22:43 -0700133 int num_bufs = len / h->io_size + (len % h->io_size == 0 ? 0 : 1);
Jerry Zhangecee4342017-07-18 14:07:57 -0700134 const char* cur_data = reinterpret_cast<const char*>(data);
135 int packet_size = getMaxPacketSize(aiob->fd);
136
137 if (posix_madvise(const_cast<void*>(data), len, POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED) <
138 0) {
139 D("[ Failed to madvise: %d ]", errno);
140 }
141
142 for (int i = 0; i < num_bufs; i++) {
Jerry Zhangcda7c3b2018-05-21 14:22:43 -0700143 int buf_len = std::min(len, static_cast<int>(h->io_size));
Jerry Zhangecee4342017-07-18 14:07:57 -0700144 io_prep(&aiob->iocb[i], aiob->fd, cur_data, buf_len, 0, read);
145
146 len -= buf_len;
147 cur_data += buf_len;
148
149 if (len == 0 && buf_len % packet_size == 0 && read) {
150 // adb does not expect the device to send a zero packet after data transfer,
151 // but the host *does* send a zero packet for the device to read.
Jerry Zhangcda7c3b2018-05-21 14:22:43 -0700152 zero_packet = h->reads_zero_packets;
Jerry Zhangecee4342017-07-18 14:07:57 -0700153 }
154 }
155 if (zero_packet) {
156 io_prep(&aiob->iocb[num_bufs], aiob->fd, reinterpret_cast<const void*>(cur_data),
157 packet_size, 0, read);
158 num_bufs += 1;
159 }
160
Jerry Zhang5ba68022018-03-19 17:54:39 -0700161 while (true) {
162 if (TEMP_FAILURE_RETRY(io_submit(aiob->ctx, num_bufs, aiob->iocbs.data())) < num_bufs) {
163 PLOG(ERROR) << "aio: got error submitting " << (read ? "read" : "write");
Jerry Zhangecee4342017-07-18 14:07:57 -0700164 return -1;
165 }
Jerry Zhang5ba68022018-03-19 17:54:39 -0700166 if (TEMP_FAILURE_RETRY(io_getevents(aiob->ctx, num_bufs, num_bufs, aiob->events.data(),
167 nullptr)) < num_bufs) {
168 PLOG(ERROR) << "aio: got error waiting " << (read ? "read" : "write");
169 return -1;
170 }
171 if (num_bufs == 1 && aiob->events[0].res == -EINTR) {
172 continue;
173 }
Jerry Zhang16b78db2018-05-15 16:20:41 -0700174 int ret = 0;
Jerry Zhang5ba68022018-03-19 17:54:39 -0700175 for (int i = 0; i < num_bufs; i++) {
176 if (aiob->events[i].res < 0) {
177 errno = -aiob->events[i].res;
178 PLOG(ERROR) << "aio: got error event on " << (read ? "read" : "write")
179 << " total bufs " << num_bufs;
180 return -1;
181 }
Jerry Zhang16b78db2018-05-15 16:20:41 -0700182 ret += aiob->events[i].res;
Jerry Zhang5ba68022018-03-19 17:54:39 -0700183 }
Jerry Zhang16b78db2018-05-15 16:20:41 -0700184 return ret;
Jerry Zhangecee4342017-07-18 14:07:57 -0700185 }
Jerry Zhangecee4342017-07-18 14:07:57 -0700186}
187
Josh Gao08718242020-03-27 18:09:56 -0700188static int usb_ffs_aio_read(usb_handle* h, void* data, int len, bool /* allow_partial */) {
Jerry Zhangecee4342017-07-18 14:07:57 -0700189 return usb_ffs_do_aio(h, data, len, true);
190}
191
192static int usb_ffs_aio_write(usb_handle* h, const void* data, int len) {
193 return usb_ffs_do_aio(h, data, len, false);
194}
195
Josh Gao183b73e2017-01-11 14:39:19 -0800196static void usb_ffs_close(usb_handle* h) {
Josh Gao184f5712017-07-26 11:06:55 -0700197 LOG(INFO) << "closing functionfs transport";
198
Josh Gao860cc5a2018-08-21 15:44:49 -0700199 h->bulk_out.reset();
200 h->bulk_in.reset();
Jerry Zhangecee4342017-07-18 14:07:57 -0700201
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700202 // Notify usb_adb_open_thread to open a new connection.
Josh Gao0cd3ae12016-09-21 12:37:10 -0700203 h->lock.lock();
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700204 h->open_new_connection = true;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700205 h->lock.unlock();
206 h->notify.notify_one();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207}
208
Josh Gao61e9e392018-10-08 14:42:19 -0700209usb_handle* create_usb_handle(unsigned num_bufs, unsigned io_size) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700210 usb_handle* h = new usb_handle();
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700211
Jerry Zhangecee4342017-07-18 14:07:57 -0700212 if (android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false)) {
213 // Devices on older kernels (< 3.18) will not have aio support for ffs
214 // unless backported. Fall back on the non-aio functions instead.
215 h->write = usb_ffs_write;
216 h->read = usb_ffs_read;
217 } else {
218 h->write = usb_ffs_aio_write;
219 h->read = usb_ffs_aio_read;
Jerry Zhangcda7c3b2018-05-21 14:22:43 -0700220 aio_block_init(&h->read_aiob, num_bufs);
221 aio_block_init(&h->write_aiob, num_bufs);
Jerry Zhangecee4342017-07-18 14:07:57 -0700222 }
Jerry Zhangcda7c3b2018-05-21 14:22:43 -0700223 h->io_size = io_size;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700224 h->close = usb_ffs_close;
Jerry Zhangb156c602018-05-07 15:14:47 -0700225 return h;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100226}