blob: 20fb6a3d102a8ab4184636ec01867d57e78cd4fc [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
Jerry Zhang55205a52017-01-04 12:34:38 -080061#define FUNCTIONFS_ENDPOINT_ALLOC _IOR('g', 231, __u32)
62
Jerry Zhang40a87782017-04-27 15:00:13 -070063static constexpr size_t ENDPOINT_ALLOC_RETRIES = 10;
Jerry Zhang55205a52017-01-04 12:34:38 -080064
Yabin Cui9b53e4c2016-04-05 14:51:52 -070065static int dummy_fd = -1;
66
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070067struct func_desc {
68 struct usb_interface_descriptor intf;
69 struct usb_endpoint_descriptor_no_audio source;
70 struct usb_endpoint_descriptor_no_audio sink;
71} __attribute__((packed));
72
Jack Phama190c802015-06-02 10:36:43 -070073struct ss_func_desc {
74 struct usb_interface_descriptor intf;
75 struct usb_endpoint_descriptor_no_audio source;
76 struct usb_ss_ep_comp_descriptor source_comp;
77 struct usb_endpoint_descriptor_no_audio sink;
78 struct usb_ss_ep_comp_descriptor sink_comp;
79} __attribute__((packed));
80
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070081struct desc_v1 {
82 struct usb_functionfs_descs_head_v1 {
83 __le32 magic;
84 __le32 length;
85 __le32 fs_count;
86 __le32 hs_count;
87 } __attribute__((packed)) header;
88 struct func_desc fs_descs, hs_descs;
89} __attribute__((packed));
90
91struct desc_v2 {
Christopher Ferrisc49f51c2015-01-23 17:09:56 -080092 struct usb_functionfs_descs_head_v2 header;
93 // The rest of the structure depends on the flags in the header.
94 __le32 fs_count;
95 __le32 hs_count;
Jack Phama190c802015-06-02 10:36:43 -070096 __le32 ss_count;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -070097 __le32 os_count;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070098 struct func_desc fs_descs, hs_descs;
Jack Phama190c802015-06-02 10:36:43 -070099 struct ss_func_desc ss_descs;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700100 struct usb_os_desc_header os_header;
101 struct usb_ext_compat_desc os_desc;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700102} __attribute__((packed));
103
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700104static struct func_desc fs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700105 .intf = {
106 .bLength = sizeof(fs_descriptors.intf),
107 .bDescriptorType = USB_DT_INTERFACE,
108 .bInterfaceNumber = 0,
109 .bNumEndpoints = 2,
110 .bInterfaceClass = ADB_CLASS,
111 .bInterfaceSubClass = ADB_SUBCLASS,
112 .bInterfaceProtocol = ADB_PROTOCOL,
113 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100114 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700115 .source = {
116 .bLength = sizeof(fs_descriptors.source),
117 .bDescriptorType = USB_DT_ENDPOINT,
118 .bEndpointAddress = 1 | USB_DIR_OUT,
119 .bmAttributes = USB_ENDPOINT_XFER_BULK,
120 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100121 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700122 .sink = {
123 .bLength = sizeof(fs_descriptors.sink),
124 .bDescriptorType = USB_DT_ENDPOINT,
125 .bEndpointAddress = 2 | USB_DIR_IN,
126 .bmAttributes = USB_ENDPOINT_XFER_BULK,
127 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
128 },
129};
130
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700131static struct func_desc hs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700132 .intf = {
133 .bLength = sizeof(hs_descriptors.intf),
134 .bDescriptorType = USB_DT_INTERFACE,
135 .bInterfaceNumber = 0,
136 .bNumEndpoints = 2,
137 .bInterfaceClass = ADB_CLASS,
138 .bInterfaceSubClass = ADB_SUBCLASS,
139 .bInterfaceProtocol = ADB_PROTOCOL,
140 .iInterface = 1, /* first string from the provided table */
141 },
142 .source = {
143 .bLength = sizeof(hs_descriptors.source),
144 .bDescriptorType = USB_DT_ENDPOINT,
145 .bEndpointAddress = 1 | USB_DIR_OUT,
146 .bmAttributes = USB_ENDPOINT_XFER_BULK,
147 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
148 },
149 .sink = {
150 .bLength = sizeof(hs_descriptors.sink),
151 .bDescriptorType = USB_DT_ENDPOINT,
152 .bEndpointAddress = 2 | USB_DIR_IN,
153 .bmAttributes = USB_ENDPOINT_XFER_BULK,
154 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +0800155 },
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100156};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
Jack Phama190c802015-06-02 10:36:43 -0700158static struct ss_func_desc ss_descriptors = {
159 .intf = {
160 .bLength = sizeof(ss_descriptors.intf),
161 .bDescriptorType = USB_DT_INTERFACE,
162 .bInterfaceNumber = 0,
163 .bNumEndpoints = 2,
164 .bInterfaceClass = ADB_CLASS,
165 .bInterfaceSubClass = ADB_SUBCLASS,
166 .bInterfaceProtocol = ADB_PROTOCOL,
167 .iInterface = 1, /* first string from the provided table */
168 },
169 .source = {
170 .bLength = sizeof(ss_descriptors.source),
171 .bDescriptorType = USB_DT_ENDPOINT,
172 .bEndpointAddress = 1 | USB_DIR_OUT,
173 .bmAttributes = USB_ENDPOINT_XFER_BULK,
174 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
175 },
176 .source_comp = {
177 .bLength = sizeof(ss_descriptors.source_comp),
178 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Jerry Zhangb5a34a22017-02-10 17:43:00 -0800179 .bMaxBurst = 4,
Jack Phama190c802015-06-02 10:36:43 -0700180 },
181 .sink = {
182 .bLength = sizeof(ss_descriptors.sink),
183 .bDescriptorType = USB_DT_ENDPOINT,
184 .bEndpointAddress = 2 | USB_DIR_IN,
185 .bmAttributes = USB_ENDPOINT_XFER_BULK,
186 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
187 },
188 .sink_comp = {
189 .bLength = sizeof(ss_descriptors.sink_comp),
190 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Jerry Zhangb5a34a22017-02-10 17:43:00 -0800191 .bMaxBurst = 4,
Jack Phama190c802015-06-02 10:36:43 -0700192 },
193};
194
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700195struct usb_ext_compat_desc os_desc_compat = {
196 .bFirstInterfaceNumber = 0,
197 .Reserved1 = cpu_to_le32(1),
198 .CompatibleID = {0},
199 .SubCompatibleID = {0},
200 .Reserved2 = {0},
201};
202
203static struct usb_os_desc_header os_desc_header = {
204 .interface = cpu_to_le32(1),
205 .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)),
206 .bcdVersion = cpu_to_le32(1),
207 .wIndex = cpu_to_le32(4),
208 .bCount = cpu_to_le32(1),
209 .Reserved = cpu_to_le32(0),
210};
211
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100212#define STR_INTERFACE_ "ADB Interface"
213
214static const struct {
215 struct usb_functionfs_strings_head header;
216 struct {
217 __le16 code;
218 const char str1[sizeof(STR_INTERFACE_)];
219 } __attribute__((packed)) lang0;
220} __attribute__((packed)) strings = {
221 .header = {
222 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
223 .length = cpu_to_le32(sizeof(strings)),
224 .str_count = cpu_to_le32(1),
225 .lang_count = cpu_to_le32(1),
226 },
227 .lang0 = {
228 cpu_to_le16(0x0409), /* en-us */
229 STR_INTERFACE_,
230 },
231};
232
Jerry Zhangecee4342017-07-18 14:07:57 -0700233static void aio_block_init(aio_block* aiob) {
234 aiob->iocb.resize(USB_FFS_NUM_BUFS);
235 aiob->iocbs.resize(USB_FFS_NUM_BUFS);
236 aiob->events.resize(USB_FFS_NUM_BUFS);
237 aiob->num_submitted = 0;
238 for (unsigned i = 0; i < USB_FFS_NUM_BUFS; i++) {
239 aiob->iocbs[i] = &aiob->iocb[i];
240 }
241}
242
243static int getMaxPacketSize(int ffs_fd) {
244 usb_endpoint_descriptor desc;
245 if (ioctl(ffs_fd, FUNCTIONFS_ENDPOINT_DESC, reinterpret_cast<unsigned long>(&desc))) {
246 D("[ could not get endpoint descriptor! (%d) ]", errno);
247 return MAX_PACKET_SIZE_HS;
248 } else {
249 return desc.wMaxPacketSize;
250 }
251}
252
Josh Gao44c688c2017-01-11 17:37:35 -0800253bool init_functionfs(struct usb_handle* h) {
Josh Gao184f5712017-07-26 11:06:55 -0700254 LOG(INFO) << "initializing functionfs";
255
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100256 ssize_t ret;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700257 struct desc_v1 v1_descriptor;
258 struct desc_v2 v2_descriptor;
Jerry Zhang55205a52017-01-04 12:34:38 -0800259 size_t retries = 0;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700260
261 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
262 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
Jack Phama190c802015-06-02 10:36:43 -0700263 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700264 FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC;
Christopher Ferrisc49f51c2015-01-23 17:09:56 -0800265 v2_descriptor.fs_count = 3;
266 v2_descriptor.hs_count = 3;
Jack Phama190c802015-06-02 10:36:43 -0700267 v2_descriptor.ss_count = 5;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700268 v2_descriptor.os_count = 1;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700269 v2_descriptor.fs_descs = fs_descriptors;
270 v2_descriptor.hs_descs = hs_descriptors;
Jack Phama190c802015-06-02 10:36:43 -0700271 v2_descriptor.ss_descs = ss_descriptors;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700272 v2_descriptor.os_header = os_desc_header;
273 v2_descriptor.os_desc = os_desc_compat;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100274
Siqi Lin8c407622016-05-26 13:04:52 -0700275 if (h->control < 0) { // might have already done this before
Josh Gao184f5712017-07-26 11:06:55 -0700276 LOG(INFO) << "opening control endpoint " << USB_FFS_ADB_EP0;
Siqi Lin8c407622016-05-26 13:04:52 -0700277 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
278 if (h->control < 0) {
Josh Gao184f5712017-07-26 11:06:55 -0700279 PLOG(ERROR) << "cannot open control endpoint " << USB_FFS_ADB_EP0;
Jack Pham4cbf1d82013-12-23 17:46:10 -0800280 goto err;
281 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100282
Siqi Lin8c407622016-05-26 13:04:52 -0700283 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
284 if (ret < 0) {
285 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
286 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
287 v1_descriptor.header.fs_count = 3;
288 v1_descriptor.header.hs_count = 3;
289 v1_descriptor.fs_descs = fs_descriptors;
290 v1_descriptor.hs_descs = hs_descriptors;
291 D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno);
292 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
293 if (ret < 0) {
294 D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno);
295 goto err;
296 }
297 }
298
299 ret = adb_write(h->control, &strings, sizeof(strings));
300 if (ret < 0) {
301 D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno);
302 goto err;
303 }
Badhri Jagan Sridharan43fd1a42017-03-07 17:30:03 -0800304 //Signal only when writing the descriptors to ffs
305 android::base::SetProperty("sys.usb.ffs.ready", "1");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100306 }
307
308 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
309 if (h->bulk_out < 0) {
Josh Gao184f5712017-07-26 11:06:55 -0700310 PLOG(ERROR) << "cannot open bulk-out endpoint " << USB_FFS_ADB_OUT;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100311 goto err;
312 }
313
314 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
315 if (h->bulk_in < 0) {
Josh Gao184f5712017-07-26 11:06:55 -0700316 PLOG(ERROR) << "cannot open bulk-in endpoint " << USB_FFS_ADB_IN;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100317 goto err;
318 }
319
Jerry Zhangc3d4e722018-02-12 16:15:50 -0800320 memset(&h->read_aiob.ctx, 0, sizeof(h->read_aiob.ctx));
321 memset(&h->write_aiob.ctx, 0, sizeof(h->write_aiob.ctx));
Jerry Zhangecee4342017-07-18 14:07:57 -0700322 if (io_setup(USB_FFS_NUM_BUFS, &h->read_aiob.ctx) ||
323 io_setup(USB_FFS_NUM_BUFS, &h->write_aiob.ctx)) {
324 D("[ aio: got error on io_setup (%d) ]", errno);
325 }
326
327 h->read_aiob.fd = h->bulk_out;
328 h->write_aiob.fd = h->bulk_in;
329
Jerry Zhang55205a52017-01-04 12:34:38 -0800330 h->max_rw = MAX_PAYLOAD;
331 while (h->max_rw >= USB_FFS_BULK_SIZE && retries < ENDPOINT_ALLOC_RETRIES) {
332 int ret_in = ioctl(h->bulk_in, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(h->max_rw));
333 int errno_in = errno;
334 int ret_out = ioctl(h->bulk_out, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(h->max_rw));
335 int errno_out = errno;
336
337 if (ret_in || ret_out) {
338 if (errno_in == ENODEV || errno_out == ENODEV) {
339 std::this_thread::sleep_for(100ms);
340 retries += 1;
341 continue;
342 }
343 h->max_rw /= 2;
344 } else {
345 return true;
346 }
347 }
348
349 D("[ adb: cannot call endpoint alloc: errno=%d ]", errno);
350 // Kernel pre-allocation could have failed for recoverable reasons.
351 // Continue running with a safe max rw size.
Jerry Zhangf3fb7de2017-02-21 14:37:07 -0800352 h->max_rw = USB_FFS_BULK_SIZE;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700353 return true;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100354
355err:
356 if (h->bulk_in > 0) {
357 adb_close(h->bulk_in);
358 h->bulk_in = -1;
359 }
360 if (h->bulk_out > 0) {
361 adb_close(h->bulk_out);
362 h->bulk_out = -1;
363 }
364 if (h->control > 0) {
365 adb_close(h->control);
366 h->control = -1;
367 }
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700368 return false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100369}
370
Josh Gaod9db09c2016-02-12 14:31:15 -0800371static void usb_ffs_open_thread(void* x) {
Josh Gao183b73e2017-01-11 14:39:19 -0800372 struct usb_handle* usb = (struct usb_handle*)x;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100373
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700374 adb_thread_setname("usb ffs open");
375
Elliott Hughesa7090b92015-04-17 17:03:59 -0700376 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100377 // wait until the USB device needs opening
Josh Gao0cd3ae12016-09-21 12:37:10 -0700378 std::unique_lock<std::mutex> lock(usb->lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700379 while (!usb->open_new_connection) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700380 usb->notify.wait(lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700381 }
382 usb->open_new_connection = false;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700383 lock.unlock();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100384
Elliott Hughesa7090b92015-04-17 17:03:59 -0700385 while (true) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700386 if (init_functionfs(usb)) {
Josh Gao184f5712017-07-26 11:06:55 -0700387 LOG(INFO) << "functionfs successfully initialized";
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100388 break;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700389 }
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800390 std::this_thread::sleep_for(1s);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100391 }
392
Josh Gao184f5712017-07-26 11:06:55 -0700393 LOG(INFO) << "registering usb transport";
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100394 register_usb_transport(usb, 0, 0, 1);
395 }
396
397 // never gets here
Josh Gaod9db09c2016-02-12 14:31:15 -0800398 abort();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100399}
400
Josh Gao6b531c42015-12-02 17:30:58 -0800401static int usb_ffs_write(usb_handle* h, const void* data, int len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700402 D("about to write (fd=%d, len=%d)", h->bulk_in, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800403
Josh Gao6b531c42015-12-02 17:30:58 -0800404 const char* buf = static_cast<const char*>(data);
405 while (len > 0) {
Jerry Zhang55205a52017-01-04 12:34:38 -0800406 int write_len = std::min(h->max_rw, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800407 int n = adb_write(h->bulk_in, buf, write_len);
408 if (n < 0) {
409 D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno));
410 return -1;
411 }
412 buf += n;
413 len -= n;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100414 }
Josh Gao6b531c42015-12-02 17:30:58 -0800415
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700416 D("[ done fd=%d ]", h->bulk_in);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100417 return 0;
418}
419
Josh Gao6b531c42015-12-02 17:30:58 -0800420static int usb_ffs_read(usb_handle* h, void* data, int len) {
421 D("about to read (fd=%d, len=%d)", h->bulk_out, len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100422
Josh Gao6b531c42015-12-02 17:30:58 -0800423 char* buf = static_cast<char*>(data);
424 while (len > 0) {
Jerry Zhang55205a52017-01-04 12:34:38 -0800425 int read_len = std::min(h->max_rw, len);
Josh Gao0b195402015-12-16 11:00:08 -0800426 int n = adb_read(h->bulk_out, buf, read_len);
Josh Gao6b531c42015-12-02 17:30:58 -0800427 if (n < 0) {
428 D("ERROR: fd = %d, n = %d: %s", h->bulk_out, n, strerror(errno));
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700429 return -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100430 }
Josh Gao6b531c42015-12-02 17:30:58 -0800431 buf += n;
432 len -= n;
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700433 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100434
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700435 D("[ done fd=%d ]", h->bulk_out);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100436 return 0;
437}
438
Jerry Zhangecee4342017-07-18 14:07:57 -0700439static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
440 aio_block* aiob = read ? &h->read_aiob : &h->write_aiob;
441 bool zero_packet = false;
442
443 int num_bufs = len / USB_FFS_BULK_SIZE + (len % USB_FFS_BULK_SIZE == 0 ? 0 : 1);
444 const char* cur_data = reinterpret_cast<const char*>(data);
445 int packet_size = getMaxPacketSize(aiob->fd);
446
447 if (posix_madvise(const_cast<void*>(data), len, POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED) <
448 0) {
449 D("[ Failed to madvise: %d ]", errno);
450 }
451
452 for (int i = 0; i < num_bufs; i++) {
453 int buf_len = std::min(len, USB_FFS_BULK_SIZE);
454 io_prep(&aiob->iocb[i], aiob->fd, cur_data, buf_len, 0, read);
455
456 len -= buf_len;
457 cur_data += buf_len;
458
459 if (len == 0 && buf_len % packet_size == 0 && read) {
460 // adb does not expect the device to send a zero packet after data transfer,
461 // but the host *does* send a zero packet for the device to read.
462 zero_packet = true;
463 }
464 }
465 if (zero_packet) {
466 io_prep(&aiob->iocb[num_bufs], aiob->fd, reinterpret_cast<const void*>(cur_data),
467 packet_size, 0, read);
468 num_bufs += 1;
469 }
470
471 if (io_submit(aiob->ctx, num_bufs, aiob->iocbs.data()) < num_bufs) {
472 D("[ aio: got error submitting %s (%d) ]", read ? "read" : "write", errno);
473 return -1;
474 }
475 if (TEMP_FAILURE_RETRY(
476 io_getevents(aiob->ctx, num_bufs, num_bufs, aiob->events.data(), nullptr)) < num_bufs) {
477 D("[ aio: got error waiting %s (%d) ]", read ? "read" : "write", errno);
478 return -1;
479 }
480 for (int i = 0; i < num_bufs; i++) {
481 if (aiob->events[i].res < 0) {
482 errno = aiob->events[i].res;
483 D("[ aio: got error event on %s (%d) ]", read ? "read" : "write", errno);
484 return -1;
485 }
486 }
487 return 0;
488}
489
490static int usb_ffs_aio_read(usb_handle* h, void* data, int len) {
491 return usb_ffs_do_aio(h, data, len, true);
492}
493
494static int usb_ffs_aio_write(usb_handle* h, const void* data, int len) {
495 return usb_ffs_do_aio(h, data, len, false);
496}
497
Josh Gao183b73e2017-01-11 14:39:19 -0800498static void usb_ffs_kick(usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100499 int err;
500
501 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700502 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700503 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700504 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100505
506 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700507 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700508 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700509 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100510
Jack Pham4cbf1d82013-12-23 17:46:10 -0800511 // don't close ep0 here, since we may not need to reinitialize it with
512 // the same descriptors again. if however ep1/ep2 fail to re-open in
513 // init_functionfs, only then would we close and open ep0 again.
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700514 // Ditto the comment in usb_adb_kick.
515 h->kicked = true;
516 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_out));
517 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_in));
518}
519
Josh Gao183b73e2017-01-11 14:39:19 -0800520static void usb_ffs_close(usb_handle* h) {
Josh Gao184f5712017-07-26 11:06:55 -0700521 LOG(INFO) << "closing functionfs transport";
522
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700523 h->kicked = false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100524 adb_close(h->bulk_out);
525 adb_close(h->bulk_in);
Jerry Zhangecee4342017-07-18 14:07:57 -0700526 io_destroy(h->read_aiob.ctx);
527 io_destroy(h->write_aiob.ctx);
528
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700529 // Notify usb_adb_open_thread to open a new connection.
Josh Gao0cd3ae12016-09-21 12:37:10 -0700530 h->lock.lock();
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700531 h->open_new_connection = true;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700532 h->lock.unlock();
533 h->notify.notify_one();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534}
535
Josh Gao183b73e2017-01-11 14:39:19 -0800536static void usb_ffs_init() {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700537 D("[ usb_init - using FunctionFS ]");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100538
Josh Gao0cd3ae12016-09-21 12:37:10 -0700539 usb_handle* h = new usb_handle();
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700540
Jerry Zhangecee4342017-07-18 14:07:57 -0700541 if (android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false)) {
542 // Devices on older kernels (< 3.18) will not have aio support for ffs
543 // unless backported. Fall back on the non-aio functions instead.
544 h->write = usb_ffs_write;
545 h->read = usb_ffs_read;
546 } else {
547 h->write = usb_ffs_aio_write;
548 h->read = usb_ffs_aio_read;
549 aio_block_init(&h->read_aiob);
550 aio_block_init(&h->write_aiob);
551 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100552 h->kick = usb_ffs_kick;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700553 h->close = usb_ffs_close;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100554
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700555 D("[ usb_init - starting thread ]");
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700556 std::thread(usb_ffs_open_thread, h).detach();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100557}
558
Josh Gao183b73e2017-01-11 14:39:19 -0800559void usb_init() {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700560 dummy_fd = adb_open("/dev/null", O_WRONLY);
561 CHECK_NE(dummy_fd, -1);
Josh Gao183b73e2017-01-11 14:39:19 -0800562 usb_ffs_init();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100563}
564
Josh Gao183b73e2017-01-11 14:39:19 -0800565int usb_write(usb_handle* h, const void* data, int len) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100566 return h->write(h, data, len);
567}
568
Josh Gao183b73e2017-01-11 14:39:19 -0800569int usb_read(usb_handle* h, void* data, int len) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100570 return h->read(h, data, len);
571}
Josh Gao0cd3ae12016-09-21 12:37:10 -0700572
Josh Gao183b73e2017-01-11 14:39:19 -0800573int usb_close(usb_handle* h) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700574 h->close(h);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575 return 0;
576}
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100577
Josh Gao183b73e2017-01-11 14:39:19 -0800578void usb_kick(usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100579 h->kick(h);
580}