blob: 263303fde91f06d03617141e397167d57613a955 [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 <linux/usb/ch9.h>
24#include <linux/usb/functionfs.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/ioctl.h>
Jerry Zhangecee4342017-07-18 14:07:57 -070029#include <sys/mman.h>
Dan Albert76649012015-02-24 15:51:19 -080030#include <sys/types.h>
31#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
Josh Gaoae72b5a2015-12-17 13:45:18 -080033#include <algorithm>
Yabin Cui9b53e4c2016-04-05 14:51:52 -070034#include <atomic>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080035#include <chrono>
Josh Gao0cd3ae12016-09-21 12:37:10 -070036#include <condition_variable>
37#include <mutex>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080038#include <thread>
Yabin Cui9b53e4c2016-04-05 14:51:52 -070039
40#include <android-base/logging.h>
Elliott Hughesffdec182016-09-23 15:40:03 -070041#include <android-base/properties.h>
Josh Gaoae72b5a2015-12-17 13:45:18 -080042
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#include "adb.h"
Josh Gao44c688c2017-01-11 17:37:35 -080044#include "daemon/usb.h"
Dan Albert76649012015-02-24 15:51:19 -080045#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080047using namespace std::chrono_literals;
48
Josh Gao44c688c2017-01-11 17:37:35 -080049#define MAX_PACKET_SIZE_FS 64
50#define MAX_PACKET_SIZE_HS 512
51#define MAX_PACKET_SIZE_SS 1024
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010052
Jerry Zhang55205a52017-01-04 12:34:38 -080053#define USB_FFS_BULK_SIZE 16384
Josh Gaoae72b5a2015-12-17 13:45:18 -080054
Jerry Zhangecee4342017-07-18 14:07:57 -070055// Number of buffers needed to fit MAX_PAYLOAD, with an extra for ZLPs.
56#define USB_FFS_NUM_BUFS ((MAX_PAYLOAD / USB_FFS_BULK_SIZE) + 1)
57
Josh Gao44c688c2017-01-11 17:37:35 -080058#define cpu_to_le16(x) htole16(x)
59#define cpu_to_le32(x) htole32(x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Yabin Cui9b53e4c2016-04-05 14:51:52 -070061static int dummy_fd = -1;
62
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070063struct func_desc {
64 struct usb_interface_descriptor intf;
65 struct usb_endpoint_descriptor_no_audio source;
66 struct usb_endpoint_descriptor_no_audio sink;
67} __attribute__((packed));
68
Jack Phama190c802015-06-02 10:36:43 -070069struct ss_func_desc {
70 struct usb_interface_descriptor intf;
71 struct usb_endpoint_descriptor_no_audio source;
72 struct usb_ss_ep_comp_descriptor source_comp;
73 struct usb_endpoint_descriptor_no_audio sink;
74 struct usb_ss_ep_comp_descriptor sink_comp;
75} __attribute__((packed));
76
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070077struct desc_v1 {
78 struct usb_functionfs_descs_head_v1 {
79 __le32 magic;
80 __le32 length;
81 __le32 fs_count;
82 __le32 hs_count;
83 } __attribute__((packed)) header;
84 struct func_desc fs_descs, hs_descs;
85} __attribute__((packed));
86
87struct desc_v2 {
Christopher Ferrisc49f51c2015-01-23 17:09:56 -080088 struct usb_functionfs_descs_head_v2 header;
89 // The rest of the structure depends on the flags in the header.
90 __le32 fs_count;
91 __le32 hs_count;
Jack Phama190c802015-06-02 10:36:43 -070092 __le32 ss_count;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -070093 __le32 os_count;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070094 struct func_desc fs_descs, hs_descs;
Jack Phama190c802015-06-02 10:36:43 -070095 struct ss_func_desc ss_descs;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -070096 struct usb_os_desc_header os_header;
97 struct usb_ext_compat_desc os_desc;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070098} __attribute__((packed));
99
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700100static struct func_desc fs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700101 .intf = {
102 .bLength = sizeof(fs_descriptors.intf),
103 .bDescriptorType = USB_DT_INTERFACE,
104 .bInterfaceNumber = 0,
105 .bNumEndpoints = 2,
106 .bInterfaceClass = ADB_CLASS,
107 .bInterfaceSubClass = ADB_SUBCLASS,
108 .bInterfaceProtocol = ADB_PROTOCOL,
109 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100110 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700111 .source = {
112 .bLength = sizeof(fs_descriptors.source),
113 .bDescriptorType = USB_DT_ENDPOINT,
114 .bEndpointAddress = 1 | USB_DIR_OUT,
115 .bmAttributes = USB_ENDPOINT_XFER_BULK,
116 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100117 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700118 .sink = {
119 .bLength = sizeof(fs_descriptors.sink),
120 .bDescriptorType = USB_DT_ENDPOINT,
121 .bEndpointAddress = 2 | USB_DIR_IN,
122 .bmAttributes = USB_ENDPOINT_XFER_BULK,
123 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
124 },
125};
126
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700127static struct func_desc hs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700128 .intf = {
129 .bLength = sizeof(hs_descriptors.intf),
130 .bDescriptorType = USB_DT_INTERFACE,
131 .bInterfaceNumber = 0,
132 .bNumEndpoints = 2,
133 .bInterfaceClass = ADB_CLASS,
134 .bInterfaceSubClass = ADB_SUBCLASS,
135 .bInterfaceProtocol = ADB_PROTOCOL,
136 .iInterface = 1, /* first string from the provided table */
137 },
138 .source = {
139 .bLength = sizeof(hs_descriptors.source),
140 .bDescriptorType = USB_DT_ENDPOINT,
141 .bEndpointAddress = 1 | USB_DIR_OUT,
142 .bmAttributes = USB_ENDPOINT_XFER_BULK,
143 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
144 },
145 .sink = {
146 .bLength = sizeof(hs_descriptors.sink),
147 .bDescriptorType = USB_DT_ENDPOINT,
148 .bEndpointAddress = 2 | USB_DIR_IN,
149 .bmAttributes = USB_ENDPOINT_XFER_BULK,
150 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +0800151 },
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100152};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153
Jack Phama190c802015-06-02 10:36:43 -0700154static struct ss_func_desc ss_descriptors = {
155 .intf = {
156 .bLength = sizeof(ss_descriptors.intf),
157 .bDescriptorType = USB_DT_INTERFACE,
158 .bInterfaceNumber = 0,
159 .bNumEndpoints = 2,
160 .bInterfaceClass = ADB_CLASS,
161 .bInterfaceSubClass = ADB_SUBCLASS,
162 .bInterfaceProtocol = ADB_PROTOCOL,
163 .iInterface = 1, /* first string from the provided table */
164 },
165 .source = {
166 .bLength = sizeof(ss_descriptors.source),
167 .bDescriptorType = USB_DT_ENDPOINT,
168 .bEndpointAddress = 1 | USB_DIR_OUT,
169 .bmAttributes = USB_ENDPOINT_XFER_BULK,
170 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
171 },
172 .source_comp = {
173 .bLength = sizeof(ss_descriptors.source_comp),
174 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Jerry Zhangb5a34a22017-02-10 17:43:00 -0800175 .bMaxBurst = 4,
Jack Phama190c802015-06-02 10:36:43 -0700176 },
177 .sink = {
178 .bLength = sizeof(ss_descriptors.sink),
179 .bDescriptorType = USB_DT_ENDPOINT,
180 .bEndpointAddress = 2 | USB_DIR_IN,
181 .bmAttributes = USB_ENDPOINT_XFER_BULK,
182 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
183 },
184 .sink_comp = {
185 .bLength = sizeof(ss_descriptors.sink_comp),
186 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Jerry Zhangb5a34a22017-02-10 17:43:00 -0800187 .bMaxBurst = 4,
Jack Phama190c802015-06-02 10:36:43 -0700188 },
189};
190
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700191struct usb_ext_compat_desc os_desc_compat = {
192 .bFirstInterfaceNumber = 0,
193 .Reserved1 = cpu_to_le32(1),
194 .CompatibleID = {0},
195 .SubCompatibleID = {0},
196 .Reserved2 = {0},
197};
198
199static struct usb_os_desc_header os_desc_header = {
200 .interface = cpu_to_le32(1),
201 .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)),
202 .bcdVersion = cpu_to_le32(1),
203 .wIndex = cpu_to_le32(4),
204 .bCount = cpu_to_le32(1),
205 .Reserved = cpu_to_le32(0),
206};
207
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100208#define STR_INTERFACE_ "ADB Interface"
209
210static const struct {
211 struct usb_functionfs_strings_head header;
212 struct {
213 __le16 code;
214 const char str1[sizeof(STR_INTERFACE_)];
215 } __attribute__((packed)) lang0;
216} __attribute__((packed)) strings = {
217 .header = {
218 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
219 .length = cpu_to_le32(sizeof(strings)),
220 .str_count = cpu_to_le32(1),
221 .lang_count = cpu_to_le32(1),
222 },
223 .lang0 = {
224 cpu_to_le16(0x0409), /* en-us */
225 STR_INTERFACE_,
226 },
227};
228
Jerry Zhangecee4342017-07-18 14:07:57 -0700229static void aio_block_init(aio_block* aiob) {
230 aiob->iocb.resize(USB_FFS_NUM_BUFS);
231 aiob->iocbs.resize(USB_FFS_NUM_BUFS);
232 aiob->events.resize(USB_FFS_NUM_BUFS);
233 aiob->num_submitted = 0;
234 for (unsigned i = 0; i < USB_FFS_NUM_BUFS; i++) {
235 aiob->iocbs[i] = &aiob->iocb[i];
236 }
237}
238
239static int getMaxPacketSize(int ffs_fd) {
240 usb_endpoint_descriptor desc;
241 if (ioctl(ffs_fd, FUNCTIONFS_ENDPOINT_DESC, reinterpret_cast<unsigned long>(&desc))) {
242 D("[ could not get endpoint descriptor! (%d) ]", errno);
243 return MAX_PACKET_SIZE_HS;
244 } else {
245 return desc.wMaxPacketSize;
246 }
247}
248
Josh Gao44c688c2017-01-11 17:37:35 -0800249bool init_functionfs(struct usb_handle* h) {
Josh Gao184f5712017-07-26 11:06:55 -0700250 LOG(INFO) << "initializing functionfs";
251
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100252 ssize_t ret;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700253 struct desc_v1 v1_descriptor;
254 struct desc_v2 v2_descriptor;
255
256 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
257 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
Jack Phama190c802015-06-02 10:36:43 -0700258 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700259 FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC;
Christopher Ferrisc49f51c2015-01-23 17:09:56 -0800260 v2_descriptor.fs_count = 3;
261 v2_descriptor.hs_count = 3;
Jack Phama190c802015-06-02 10:36:43 -0700262 v2_descriptor.ss_count = 5;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700263 v2_descriptor.os_count = 1;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700264 v2_descriptor.fs_descs = fs_descriptors;
265 v2_descriptor.hs_descs = hs_descriptors;
Jack Phama190c802015-06-02 10:36:43 -0700266 v2_descriptor.ss_descs = ss_descriptors;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700267 v2_descriptor.os_header = os_desc_header;
268 v2_descriptor.os_desc = os_desc_compat;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100269
Siqi Lin8c407622016-05-26 13:04:52 -0700270 if (h->control < 0) { // might have already done this before
Josh Gao184f5712017-07-26 11:06:55 -0700271 LOG(INFO) << "opening control endpoint " << USB_FFS_ADB_EP0;
Luis Hector Chavez3a3df402018-03-09 10:14:01 -0800272 h->control = adb_open(USB_FFS_ADB_EP0, O_WRONLY);
Siqi Lin8c407622016-05-26 13:04:52 -0700273 if (h->control < 0) {
Josh Gao184f5712017-07-26 11:06:55 -0700274 PLOG(ERROR) << "cannot open control endpoint " << USB_FFS_ADB_EP0;
Jack Pham4cbf1d82013-12-23 17:46:10 -0800275 goto err;
276 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100277
Siqi Lin8c407622016-05-26 13:04:52 -0700278 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
279 if (ret < 0) {
280 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
281 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
282 v1_descriptor.header.fs_count = 3;
283 v1_descriptor.header.hs_count = 3;
284 v1_descriptor.fs_descs = fs_descriptors;
285 v1_descriptor.hs_descs = hs_descriptors;
286 D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno);
287 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
288 if (ret < 0) {
289 D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno);
290 goto err;
291 }
292 }
293
294 ret = adb_write(h->control, &strings, sizeof(strings));
295 if (ret < 0) {
296 D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno);
297 goto err;
298 }
Badhri Jagan Sridharan43fd1a42017-03-07 17:30:03 -0800299 //Signal only when writing the descriptors to ffs
300 android::base::SetProperty("sys.usb.ffs.ready", "1");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100301 }
302
Luis Hector Chavez3a3df402018-03-09 10:14:01 -0800303 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDONLY);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100304 if (h->bulk_out < 0) {
Josh Gao184f5712017-07-26 11:06:55 -0700305 PLOG(ERROR) << "cannot open bulk-out endpoint " << USB_FFS_ADB_OUT;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100306 goto err;
307 }
308
Luis Hector Chavez3a3df402018-03-09 10:14:01 -0800309 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_WRONLY);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100310 if (h->bulk_in < 0) {
Josh Gao184f5712017-07-26 11:06:55 -0700311 PLOG(ERROR) << "cannot open bulk-in endpoint " << USB_FFS_ADB_IN;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100312 goto err;
313 }
314
Jerry Zhangc3d4e722018-02-12 16:15:50 -0800315 memset(&h->read_aiob.ctx, 0, sizeof(h->read_aiob.ctx));
316 memset(&h->write_aiob.ctx, 0, sizeof(h->write_aiob.ctx));
Jerry Zhangecee4342017-07-18 14:07:57 -0700317 if (io_setup(USB_FFS_NUM_BUFS, &h->read_aiob.ctx) ||
318 io_setup(USB_FFS_NUM_BUFS, &h->write_aiob.ctx)) {
319 D("[ aio: got error on io_setup (%d) ]", errno);
320 }
321
322 h->read_aiob.fd = h->bulk_out;
323 h->write_aiob.fd = h->bulk_in;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700324 return true;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100325
326err:
327 if (h->bulk_in > 0) {
328 adb_close(h->bulk_in);
329 h->bulk_in = -1;
330 }
331 if (h->bulk_out > 0) {
332 adb_close(h->bulk_out);
333 h->bulk_out = -1;
334 }
335 if (h->control > 0) {
336 adb_close(h->control);
337 h->control = -1;
338 }
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700339 return false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100340}
341
Josh Gaod9db09c2016-02-12 14:31:15 -0800342static void usb_ffs_open_thread(void* x) {
Josh Gao183b73e2017-01-11 14:39:19 -0800343 struct usb_handle* usb = (struct usb_handle*)x;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100344
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700345 adb_thread_setname("usb ffs open");
346
Elliott Hughesa7090b92015-04-17 17:03:59 -0700347 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100348 // wait until the USB device needs opening
Josh Gao0cd3ae12016-09-21 12:37:10 -0700349 std::unique_lock<std::mutex> lock(usb->lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700350 while (!usb->open_new_connection) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700351 usb->notify.wait(lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700352 }
353 usb->open_new_connection = false;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700354 lock.unlock();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100355
Elliott Hughesa7090b92015-04-17 17:03:59 -0700356 while (true) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700357 if (init_functionfs(usb)) {
Josh Gao184f5712017-07-26 11:06:55 -0700358 LOG(INFO) << "functionfs successfully initialized";
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100359 break;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700360 }
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800361 std::this_thread::sleep_for(1s);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100362 }
363
Josh Gao184f5712017-07-26 11:06:55 -0700364 LOG(INFO) << "registering usb transport";
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100365 register_usb_transport(usb, 0, 0, 1);
366 }
367
368 // never gets here
Josh Gaod9db09c2016-02-12 14:31:15 -0800369 abort();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100370}
371
Josh Gao6b531c42015-12-02 17:30:58 -0800372static int usb_ffs_write(usb_handle* h, const void* data, int len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700373 D("about to write (fd=%d, len=%d)", h->bulk_in, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800374
Josh Gao6b531c42015-12-02 17:30:58 -0800375 const char* buf = static_cast<const char*>(data);
376 while (len > 0) {
Jerry Zhang99499f12018-03-14 14:57:31 -0700377 int write_len = std::min(USB_FFS_BULK_SIZE, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800378 int n = adb_write(h->bulk_in, buf, write_len);
379 if (n < 0) {
380 D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno));
381 return -1;
382 }
383 buf += n;
384 len -= n;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100385 }
Josh Gao6b531c42015-12-02 17:30:58 -0800386
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700387 D("[ done fd=%d ]", h->bulk_in);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100388 return 0;
389}
390
Josh Gao6b531c42015-12-02 17:30:58 -0800391static int usb_ffs_read(usb_handle* h, void* data, int len) {
392 D("about to read (fd=%d, len=%d)", h->bulk_out, len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100393
Josh Gao6b531c42015-12-02 17:30:58 -0800394 char* buf = static_cast<char*>(data);
395 while (len > 0) {
Jerry Zhang99499f12018-03-14 14:57:31 -0700396 int read_len = std::min(USB_FFS_BULK_SIZE, len);
Josh Gao0b195402015-12-16 11:00:08 -0800397 int n = adb_read(h->bulk_out, buf, read_len);
Josh Gao6b531c42015-12-02 17:30:58 -0800398 if (n < 0) {
399 D("ERROR: fd = %d, n = %d: %s", h->bulk_out, n, strerror(errno));
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700400 return -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100401 }
Josh Gao6b531c42015-12-02 17:30:58 -0800402 buf += n;
403 len -= n;
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700404 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100405
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700406 D("[ done fd=%d ]", h->bulk_out);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100407 return 0;
408}
409
Jerry Zhangecee4342017-07-18 14:07:57 -0700410static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
411 aio_block* aiob = read ? &h->read_aiob : &h->write_aiob;
412 bool zero_packet = false;
413
414 int num_bufs = len / USB_FFS_BULK_SIZE + (len % USB_FFS_BULK_SIZE == 0 ? 0 : 1);
415 const char* cur_data = reinterpret_cast<const char*>(data);
416 int packet_size = getMaxPacketSize(aiob->fd);
417
418 if (posix_madvise(const_cast<void*>(data), len, POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED) <
419 0) {
420 D("[ Failed to madvise: %d ]", errno);
421 }
422
423 for (int i = 0; i < num_bufs; i++) {
424 int buf_len = std::min(len, USB_FFS_BULK_SIZE);
425 io_prep(&aiob->iocb[i], aiob->fd, cur_data, buf_len, 0, read);
426
427 len -= buf_len;
428 cur_data += buf_len;
429
430 if (len == 0 && buf_len % packet_size == 0 && read) {
431 // adb does not expect the device to send a zero packet after data transfer,
432 // but the host *does* send a zero packet for the device to read.
433 zero_packet = true;
434 }
435 }
436 if (zero_packet) {
437 io_prep(&aiob->iocb[num_bufs], aiob->fd, reinterpret_cast<const void*>(cur_data),
438 packet_size, 0, read);
439 num_bufs += 1;
440 }
441
Jerry Zhang5ba68022018-03-19 17:54:39 -0700442 while (true) {
443 if (TEMP_FAILURE_RETRY(io_submit(aiob->ctx, num_bufs, aiob->iocbs.data())) < num_bufs) {
444 PLOG(ERROR) << "aio: got error submitting " << (read ? "read" : "write");
Jerry Zhangecee4342017-07-18 14:07:57 -0700445 return -1;
446 }
Jerry Zhang5ba68022018-03-19 17:54:39 -0700447 if (TEMP_FAILURE_RETRY(io_getevents(aiob->ctx, num_bufs, num_bufs, aiob->events.data(),
448 nullptr)) < num_bufs) {
449 PLOG(ERROR) << "aio: got error waiting " << (read ? "read" : "write");
450 return -1;
451 }
452 if (num_bufs == 1 && aiob->events[0].res == -EINTR) {
453 continue;
454 }
455 for (int i = 0; i < num_bufs; i++) {
456 if (aiob->events[i].res < 0) {
457 errno = -aiob->events[i].res;
458 PLOG(ERROR) << "aio: got error event on " << (read ? "read" : "write")
459 << " total bufs " << num_bufs;
460 return -1;
461 }
462 }
463 return 0;
Jerry Zhangecee4342017-07-18 14:07:57 -0700464 }
Jerry Zhangecee4342017-07-18 14:07:57 -0700465}
466
467static int usb_ffs_aio_read(usb_handle* h, void* data, int len) {
468 return usb_ffs_do_aio(h, data, len, true);
469}
470
471static int usb_ffs_aio_write(usb_handle* h, const void* data, int len) {
472 return usb_ffs_do_aio(h, data, len, false);
473}
474
Josh Gao183b73e2017-01-11 14:39:19 -0800475static void usb_ffs_kick(usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100476 int err;
477
478 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700479 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700480 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700481 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100482
483 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700484 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700485 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700486 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100487
Jack Pham4cbf1d82013-12-23 17:46:10 -0800488 // don't close ep0 here, since we may not need to reinitialize it with
489 // the same descriptors again. if however ep1/ep2 fail to re-open in
490 // init_functionfs, only then would we close and open ep0 again.
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700491 // Ditto the comment in usb_adb_kick.
492 h->kicked = true;
493 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_out));
494 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_in));
495}
496
Josh Gao183b73e2017-01-11 14:39:19 -0800497static void usb_ffs_close(usb_handle* h) {
Josh Gao184f5712017-07-26 11:06:55 -0700498 LOG(INFO) << "closing functionfs transport";
499
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700500 h->kicked = false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100501 adb_close(h->bulk_out);
502 adb_close(h->bulk_in);
Jerry Zhangecee4342017-07-18 14:07:57 -0700503 io_destroy(h->read_aiob.ctx);
504 io_destroy(h->write_aiob.ctx);
505
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700506 // Notify usb_adb_open_thread to open a new connection.
Josh Gao0cd3ae12016-09-21 12:37:10 -0700507 h->lock.lock();
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700508 h->open_new_connection = true;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700509 h->lock.unlock();
510 h->notify.notify_one();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511}
512
Josh Gao183b73e2017-01-11 14:39:19 -0800513static void usb_ffs_init() {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700514 D("[ usb_init - using FunctionFS ]");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100515
Josh Gao0cd3ae12016-09-21 12:37:10 -0700516 usb_handle* h = new usb_handle();
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700517
Jerry Zhangecee4342017-07-18 14:07:57 -0700518 if (android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false)) {
519 // Devices on older kernels (< 3.18) will not have aio support for ffs
520 // unless backported. Fall back on the non-aio functions instead.
521 h->write = usb_ffs_write;
522 h->read = usb_ffs_read;
523 } else {
524 h->write = usb_ffs_aio_write;
525 h->read = usb_ffs_aio_read;
526 aio_block_init(&h->read_aiob);
527 aio_block_init(&h->write_aiob);
528 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100529 h->kick = usb_ffs_kick;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700530 h->close = usb_ffs_close;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100531
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700532 D("[ usb_init - starting thread ]");
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700533 std::thread(usb_ffs_open_thread, h).detach();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100534}
535
Josh Gao183b73e2017-01-11 14:39:19 -0800536void usb_init() {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700537 dummy_fd = adb_open("/dev/null", O_WRONLY);
538 CHECK_NE(dummy_fd, -1);
Josh Gao183b73e2017-01-11 14:39:19 -0800539 usb_ffs_init();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100540}
541
Josh Gao183b73e2017-01-11 14:39:19 -0800542int usb_write(usb_handle* h, const void* data, int len) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100543 return h->write(h, data, len);
544}
545
Josh Gao183b73e2017-01-11 14:39:19 -0800546int usb_read(usb_handle* h, void* data, int len) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100547 return h->read(h, data, len);
548}
Josh Gao0cd3ae12016-09-21 12:37:10 -0700549
Josh Gao183b73e2017-01-11 14:39:19 -0800550int usb_close(usb_handle* h) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700551 h->close(h);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800552 return 0;
553}
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100554
Josh Gao183b73e2017-01-11 14:39:19 -0800555void usb_kick(usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100556 h->kick(h);
557}