2009-03-04 03:32:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-22 22:52:57 +00:00
|
|
|
#define TRACE_TAG USB
|
2015-03-19 22:21:08 +00:00
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
2015-02-24 23:51:19 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2009-03-04 03:32:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
2017-07-18 21:07:57 +00:00
|
|
|
#include <sys/mman.h>
|
2009-03-04 03:32:55 +00:00
|
|
|
#include <sys/types.h>
|
2015-02-24 23:51:19 +00:00
|
|
|
#include <unistd.h>
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2018-10-08 21:20:29 +00:00
|
|
|
#include <linux/usb/ch9.h>
|
|
|
|
#include <linux/usb/functionfs.h>
|
|
|
|
|
2015-12-17 21:45:18 +00:00
|
|
|
#include <algorithm>
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
#include <atomic>
|
2016-11-15 20:37:32 +00:00
|
|
|
#include <chrono>
|
2016-09-21 19:37:10 +00:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2016-11-15 20:37:32 +00:00
|
|
|
#include <thread>
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
|
|
|
|
#include <android-base/logging.h>
|
2016-09-23 22:40:03 +00:00
|
|
|
#include <android-base/properties.h>
|
2015-12-17 21:45:18 +00:00
|
|
|
|
2009-03-04 03:32:55 +00:00
|
|
|
#include "adb.h"
|
2018-05-07 22:14:47 +00:00
|
|
|
#include "adbd/usb.h"
|
2015-02-24 23:51:19 +00:00
|
|
|
#include "transport.h"
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2016-11-15 20:37:32 +00:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2017-01-12 01:37:35 +00:00
|
|
|
#define MAX_PACKET_SIZE_FS 64
|
|
|
|
#define MAX_PACKET_SIZE_HS 512
|
|
|
|
#define MAX_PACKET_SIZE_SS 1024
|
2012-01-13 14:13:46 +00:00
|
|
|
|
2017-01-04 20:34:38 +00:00
|
|
|
#define USB_FFS_BULK_SIZE 16384
|
2015-12-17 21:45:18 +00:00
|
|
|
|
2017-07-18 21:07:57 +00:00
|
|
|
// Number of buffers needed to fit MAX_PAYLOAD, with an extra for ZLPs.
|
2018-05-21 21:22:43 +00:00
|
|
|
#define USB_FFS_NUM_BUFS ((4 * MAX_PAYLOAD / USB_FFS_BULK_SIZE) + 1)
|
2017-07-18 21:07:57 +00:00
|
|
|
|
2018-08-21 22:44:49 +00:00
|
|
|
static unique_fd& dummy_fd = *new unique_fd();
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
|
2018-05-21 21:22:43 +00:00
|
|
|
static void aio_block_init(aio_block* aiob, unsigned num_bufs) {
|
|
|
|
aiob->iocb.resize(num_bufs);
|
|
|
|
aiob->iocbs.resize(num_bufs);
|
|
|
|
aiob->events.resize(num_bufs);
|
2017-07-18 21:07:57 +00:00
|
|
|
aiob->num_submitted = 0;
|
2018-05-21 21:22:43 +00:00
|
|
|
for (unsigned i = 0; i < num_bufs; i++) {
|
2017-07-18 21:07:57 +00:00
|
|
|
aiob->iocbs[i] = &aiob->iocb[i];
|
|
|
|
}
|
2018-05-15 23:30:10 +00:00
|
|
|
memset(&aiob->ctx, 0, sizeof(aiob->ctx));
|
2018-05-21 21:22:43 +00:00
|
|
|
if (io_setup(num_bufs, &aiob->ctx)) {
|
2018-05-15 23:30:10 +00:00
|
|
|
D("[ aio: got error on io_setup (%d) ]", errno);
|
|
|
|
}
|
2017-07-18 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int getMaxPacketSize(int ffs_fd) {
|
|
|
|
usb_endpoint_descriptor desc;
|
|
|
|
if (ioctl(ffs_fd, FUNCTIONFS_ENDPOINT_DESC, reinterpret_cast<unsigned long>(&desc))) {
|
|
|
|
D("[ could not get endpoint descriptor! (%d) ]", errno);
|
|
|
|
return MAX_PACKET_SIZE_HS;
|
|
|
|
} else {
|
|
|
|
return desc.wMaxPacketSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 22:14:47 +00:00
|
|
|
static bool init_functionfs(struct usb_handle* h) {
|
2017-07-26 18:06:55 +00:00
|
|
|
LOG(INFO) << "initializing functionfs";
|
2018-10-08 21:20:29 +00:00
|
|
|
if (!open_functionfs(&h->control, &h->bulk_out, &h->bulk_in)) {
|
|
|
|
return false;
|
2012-01-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 21:20:29 +00:00
|
|
|
h->read_aiob.fd = h->bulk_out.get();
|
|
|
|
h->write_aiob.fd = h->bulk_in.get();
|
2018-05-21 21:22:43 +00:00
|
|
|
h->reads_zero_packets = true;
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
return true;
|
2012-01-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 21:42:19 +00:00
|
|
|
static void usb_ffs_open_thread(usb_handle* usb) {
|
2015-08-28 23:37:29 +00:00
|
|
|
adb_thread_setname("usb ffs open");
|
|
|
|
|
2015-04-18 00:03:59 +00:00
|
|
|
while (true) {
|
2012-01-13 14:13:46 +00:00
|
|
|
// wait until the USB device needs opening
|
2016-09-21 19:37:10 +00:00
|
|
|
std::unique_lock<std::mutex> lock(usb->lock);
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
while (!usb->open_new_connection) {
|
2016-09-21 19:37:10 +00:00
|
|
|
usb->notify.wait(lock);
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
}
|
|
|
|
usb->open_new_connection = false;
|
2016-09-21 19:37:10 +00:00
|
|
|
lock.unlock();
|
2012-01-13 14:13:46 +00:00
|
|
|
|
2015-04-18 00:03:59 +00:00
|
|
|
while (true) {
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
if (init_functionfs(usb)) {
|
2017-07-26 18:06:55 +00:00
|
|
|
LOG(INFO) << "functionfs successfully initialized";
|
2012-01-13 14:13:46 +00:00
|
|
|
break;
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
}
|
2016-11-15 20:37:32 +00:00
|
|
|
std::this_thread::sleep_for(1s);
|
2012-01-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 18:06:55 +00:00
|
|
|
LOG(INFO) << "registering usb transport";
|
2018-07-14 01:15:16 +00:00
|
|
|
register_usb_transport(usb, nullptr, nullptr, 1);
|
2012-01-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// never gets here
|
2016-02-12 22:31:15 +00:00
|
|
|
abort();
|
2012-01-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 01:30:58 +00:00
|
|
|
static int usb_ffs_write(usb_handle* h, const void* data, int len) {
|
2018-08-21 22:44:49 +00:00
|
|
|
D("about to write (fd=%d, len=%d)", h->bulk_in.get(), len);
|
2012-01-13 14:13:46 +00:00
|
|
|
|
2015-12-03 01:30:58 +00:00
|
|
|
const char* buf = static_cast<const char*>(data);
|
2018-05-15 23:20:41 +00:00
|
|
|
int orig_len = len;
|
2015-12-03 01:30:58 +00:00
|
|
|
while (len > 0) {
|
2018-03-14 21:57:31 +00:00
|
|
|
int write_len = std::min(USB_FFS_BULK_SIZE, len);
|
2015-12-03 01:30:58 +00:00
|
|
|
int n = adb_write(h->bulk_in, buf, write_len);
|
|
|
|
if (n < 0) {
|
2018-08-21 22:44:49 +00:00
|
|
|
D("ERROR: fd = %d, n = %d: %s", h->bulk_in.get(), n, strerror(errno));
|
2015-12-03 01:30:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buf += n;
|
|
|
|
len -= n;
|
2015-08-25 17:59:45 +00:00
|
|
|
}
|
2012-01-13 14:13:46 +00:00
|
|
|
|
2018-08-21 22:44:49 +00:00
|
|
|
D("[ done fd=%d ]", h->bulk_in.get());
|
2018-05-15 23:20:41 +00:00
|
|
|
return orig_len;
|
2012-01-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 01:30:58 +00:00
|
|
|
static int usb_ffs_read(usb_handle* h, void* data, int len) {
|
2018-08-21 22:44:49 +00:00
|
|
|
D("about to read (fd=%d, len=%d)", h->bulk_out.get(), len);
|
2012-01-13 14:13:46 +00:00
|
|
|
|
2015-12-03 01:30:58 +00:00
|
|
|
char* buf = static_cast<char*>(data);
|
2018-05-15 23:20:41 +00:00
|
|
|
int orig_len = len;
|
2015-12-03 01:30:58 +00:00
|
|
|
while (len > 0) {
|
2018-03-14 21:57:31 +00:00
|
|
|
int read_len = std::min(USB_FFS_BULK_SIZE, len);
|
2015-12-16 19:00:08 +00:00
|
|
|
int n = adb_read(h->bulk_out, buf, read_len);
|
2015-12-03 01:30:58 +00:00
|
|
|
if (n < 0) {
|
2018-08-21 22:44:49 +00:00
|
|
|
D("ERROR: fd = %d, n = %d: %s", h->bulk_out.get(), n, strerror(errno));
|
2015-08-25 17:59:45 +00:00
|
|
|
return -1;
|
2012-01-13 14:13:46 +00:00
|
|
|
}
|
2015-12-03 01:30:58 +00:00
|
|
|
buf += n;
|
|
|
|
len -= n;
|
2015-08-25 17:59:45 +00:00
|
|
|
}
|
2012-01-13 14:13:46 +00:00
|
|
|
|
2018-08-21 22:44:49 +00:00
|
|
|
D("[ done fd=%d ]", h->bulk_out.get());
|
2018-05-15 23:20:41 +00:00
|
|
|
return orig_len;
|
2012-01-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 21:07:57 +00:00
|
|
|
static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
|
|
|
|
aio_block* aiob = read ? &h->read_aiob : &h->write_aiob;
|
|
|
|
bool zero_packet = false;
|
|
|
|
|
2018-05-21 21:22:43 +00:00
|
|
|
int num_bufs = len / h->io_size + (len % h->io_size == 0 ? 0 : 1);
|
2017-07-18 21:07:57 +00:00
|
|
|
const char* cur_data = reinterpret_cast<const char*>(data);
|
|
|
|
int packet_size = getMaxPacketSize(aiob->fd);
|
|
|
|
|
|
|
|
if (posix_madvise(const_cast<void*>(data), len, POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED) <
|
|
|
|
0) {
|
|
|
|
D("[ Failed to madvise: %d ]", errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < num_bufs; i++) {
|
2018-05-21 21:22:43 +00:00
|
|
|
int buf_len = std::min(len, static_cast<int>(h->io_size));
|
2017-07-18 21:07:57 +00:00
|
|
|
io_prep(&aiob->iocb[i], aiob->fd, cur_data, buf_len, 0, read);
|
|
|
|
|
|
|
|
len -= buf_len;
|
|
|
|
cur_data += buf_len;
|
|
|
|
|
|
|
|
if (len == 0 && buf_len % packet_size == 0 && read) {
|
|
|
|
// adb does not expect the device to send a zero packet after data transfer,
|
|
|
|
// but the host *does* send a zero packet for the device to read.
|
2018-05-21 21:22:43 +00:00
|
|
|
zero_packet = h->reads_zero_packets;
|
2017-07-18 21:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (zero_packet) {
|
|
|
|
io_prep(&aiob->iocb[num_bufs], aiob->fd, reinterpret_cast<const void*>(cur_data),
|
|
|
|
packet_size, 0, read);
|
|
|
|
num_bufs += 1;
|
|
|
|
}
|
|
|
|
|
2018-03-20 00:54:39 +00:00
|
|
|
while (true) {
|
|
|
|
if (TEMP_FAILURE_RETRY(io_submit(aiob->ctx, num_bufs, aiob->iocbs.data())) < num_bufs) {
|
|
|
|
PLOG(ERROR) << "aio: got error submitting " << (read ? "read" : "write");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (TEMP_FAILURE_RETRY(io_getevents(aiob->ctx, num_bufs, num_bufs, aiob->events.data(),
|
|
|
|
nullptr)) < num_bufs) {
|
|
|
|
PLOG(ERROR) << "aio: got error waiting " << (read ? "read" : "write");
|
2017-07-18 21:07:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-03-20 00:54:39 +00:00
|
|
|
if (num_bufs == 1 && aiob->events[0].res == -EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-15 23:20:41 +00:00
|
|
|
int ret = 0;
|
2018-03-20 00:54:39 +00:00
|
|
|
for (int i = 0; i < num_bufs; i++) {
|
|
|
|
if (aiob->events[i].res < 0) {
|
|
|
|
errno = -aiob->events[i].res;
|
|
|
|
PLOG(ERROR) << "aio: got error event on " << (read ? "read" : "write")
|
|
|
|
<< " total bufs " << num_bufs;
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-15 23:20:41 +00:00
|
|
|
ret += aiob->events[i].res;
|
2018-03-20 00:54:39 +00:00
|
|
|
}
|
2018-05-15 23:20:41 +00:00
|
|
|
return ret;
|
2017-07-18 21:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_ffs_aio_read(usb_handle* h, void* data, int len) {
|
|
|
|
return usb_ffs_do_aio(h, data, len, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_ffs_aio_write(usb_handle* h, const void* data, int len) {
|
|
|
|
return usb_ffs_do_aio(h, data, len, false);
|
|
|
|
}
|
|
|
|
|
2017-01-11 22:39:19 +00:00
|
|
|
static void usb_ffs_kick(usb_handle* h) {
|
2012-01-13 14:13:46 +00:00
|
|
|
int err;
|
|
|
|
|
2018-08-21 22:44:49 +00:00
|
|
|
err = ioctl(h->bulk_in.get(), FUNCTIONFS_CLEAR_HALT);
|
2015-09-22 22:52:57 +00:00
|
|
|
if (err < 0) {
|
2018-08-21 22:44:49 +00:00
|
|
|
D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in.get(), errno);
|
2015-09-22 22:52:57 +00:00
|
|
|
}
|
2012-01-13 14:13:46 +00:00
|
|
|
|
2018-08-21 22:44:49 +00:00
|
|
|
err = ioctl(h->bulk_out.get(), FUNCTIONFS_CLEAR_HALT);
|
2015-09-22 22:52:57 +00:00
|
|
|
if (err < 0) {
|
2018-08-21 22:44:49 +00:00
|
|
|
D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out.get(), errno);
|
2015-09-22 22:52:57 +00:00
|
|
|
}
|
2012-01-13 14:13:46 +00:00
|
|
|
|
2013-12-24 01:46:10 +00:00
|
|
|
// don't close ep0 here, since we may not need to reinitialize it with
|
|
|
|
// the same descriptors again. if however ep1/ep2 fail to re-open in
|
|
|
|
// init_functionfs, only then would we close and open ep0 again.
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
// Ditto the comment in usb_adb_kick.
|
|
|
|
h->kicked = true;
|
2018-08-21 22:44:49 +00:00
|
|
|
TEMP_FAILURE_RETRY(dup2(dummy_fd.get(), h->bulk_out.get()));
|
|
|
|
TEMP_FAILURE_RETRY(dup2(dummy_fd.get(), h->bulk_in.get()));
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 22:39:19 +00:00
|
|
|
static void usb_ffs_close(usb_handle* h) {
|
2017-07-26 18:06:55 +00:00
|
|
|
LOG(INFO) << "closing functionfs transport";
|
|
|
|
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
h->kicked = false;
|
2018-08-21 22:44:49 +00:00
|
|
|
h->bulk_out.reset();
|
|
|
|
h->bulk_in.reset();
|
2017-07-18 21:07:57 +00:00
|
|
|
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
// Notify usb_adb_open_thread to open a new connection.
|
2016-09-21 19:37:10 +00:00
|
|
|
h->lock.lock();
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
h->open_new_connection = true;
|
2016-09-21 19:37:10 +00:00
|
|
|
h->lock.unlock();
|
|
|
|
h->notify.notify_one();
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 21:42:19 +00:00
|
|
|
usb_handle* create_usb_handle(unsigned num_bufs, unsigned io_size) {
|
2016-09-21 19:37:10 +00:00
|
|
|
usb_handle* h = new usb_handle();
|
2015-04-22 02:39:52 +00:00
|
|
|
|
2017-07-18 21:07:57 +00:00
|
|
|
if (android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false)) {
|
|
|
|
// Devices on older kernels (< 3.18) will not have aio support for ffs
|
|
|
|
// unless backported. Fall back on the non-aio functions instead.
|
|
|
|
h->write = usb_ffs_write;
|
|
|
|
h->read = usb_ffs_read;
|
|
|
|
} else {
|
|
|
|
h->write = usb_ffs_aio_write;
|
|
|
|
h->read = usb_ffs_aio_read;
|
2018-05-21 21:22:43 +00:00
|
|
|
aio_block_init(&h->read_aiob, num_bufs);
|
|
|
|
aio_block_init(&h->write_aiob, num_bufs);
|
2017-07-18 21:07:57 +00:00
|
|
|
}
|
2018-05-21 21:22:43 +00:00
|
|
|
h->io_size = io_size;
|
2012-01-13 14:13:46 +00:00
|
|
|
h->kick = usb_ffs_kick;
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
h->close = usb_ffs_close;
|
2018-05-07 22:14:47 +00:00
|
|
|
return h;
|
2012-01-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 22:39:19 +00:00
|
|
|
void usb_init() {
|
2018-05-07 22:14:47 +00:00
|
|
|
D("[ usb_init - using FunctionFS ]");
|
2018-08-21 22:44:49 +00:00
|
|
|
dummy_fd.reset(adb_open("/dev/null", O_WRONLY | O_CLOEXEC));
|
|
|
|
CHECK_NE(-1, dummy_fd.get());
|
2018-05-07 22:14:47 +00:00
|
|
|
|
2018-10-08 21:42:19 +00:00
|
|
|
std::thread(usb_ffs_open_thread, create_usb_handle(USB_FFS_NUM_BUFS, USB_FFS_BULK_SIZE))
|
|
|
|
.detach();
|
2012-01-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 22:39:19 +00:00
|
|
|
int usb_write(usb_handle* h, const void* data, int len) {
|
2012-01-13 14:13:46 +00:00
|
|
|
return h->write(h, data, len);
|
|
|
|
}
|
|
|
|
|
2017-01-11 22:39:19 +00:00
|
|
|
int usb_read(usb_handle* h, void* data, int len) {
|
2012-01-13 14:13:46 +00:00
|
|
|
return h->read(h, data, len);
|
|
|
|
}
|
2016-09-21 19:37:10 +00:00
|
|
|
|
2017-01-11 22:39:19 +00:00
|
|
|
int usb_close(usb_handle* h) {
|
adb: fix adb usb operations on device.
Problem: For devices using /dev/usb-ffs/adb, Run
`while true; do adb reconnect device; sleep 1; done`. And the
device soon becomes offline. The adbd log shows that calling
adb_read(h->bulk_out) in usb_ffs_read() gets EOVERFLOW error.
Reason: When kicking a transport using usb-ffs, /dev/usb-ffs/adb/ep0
is not closed, and the device will not notify a usb connection reset
to host. So the host will continue to send unfinished packets even
if a new transport is started on device. The unfinished packets may
not have the same size as what is expected on device, so adbd on
device gets EOVERFLOW error. At the worst case, adbd has to create new
transports for each unfinished packet.
Fixes:
The direct fix is to make the usb connection reset when kicking transports,
as in https://android-review.googlesource.com/#/c/211267/1. And I think
we can make following improvements beside that.
1. Close a file that is used in other threads isn't safe. Because the file
descriptor may be reused to open other files, and other threads may operate
on the wrong file. So use dup2(dummy_fd) to replace close() in kick function,
and really close the file descriptor after the read/write threads exit.
2. Open new usb connection after usb_close() instead of after
usb_kick(). After usb_kick(), the transport may still exist and
reader/writer for the transport may be still running. But after
usb_close(), the previous transport is guaranteed to be destroyed.
Bug: 25935458
Change-Id: I1eff99662d1bf1cba66af7e7142f4c0c4d82c01b
(cherry picked from commit 005bf1e05be5de518184c376ebf88268bb191d09)
2016-04-05 21:51:52 +00:00
|
|
|
h->close(h);
|
2009-03-04 03:32:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-01-13 14:13:46 +00:00
|
|
|
|
2017-01-11 22:39:19 +00:00
|
|
|
void usb_kick(usb_handle* h) {
|
2012-01-13 14:13:46 +00:00
|
|
|
h->kick(h);
|
|
|
|
}
|