2009-03-04 04:32:55 +01: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-23 00:52:57 +02:00
|
|
|
#define TRACE_TAG USB
|
2015-03-19 23:21:08 +01:00
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <linux/usb/ch9.h>
|
|
|
|
#include <linux/usb/functionfs.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-12-17 22:45:18 +01: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 23:51:52 +02:00
|
|
|
#include <atomic>
|
2016-11-15 21:37:32 +01:00
|
|
|
#include <chrono>
|
2016-09-21 21:37:10 +02:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2016-11-15 21:37:32 +01: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 23:51:52 +02:00
|
|
|
|
|
|
|
#include <android-base/logging.h>
|
2016-09-24 00:40:03 +02:00
|
|
|
#include <android-base/properties.h>
|
2015-12-17 22:45:18 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "adb.h"
|
2017-01-12 02:37:35 +01:00
|
|
|
#include "daemon/usb.h"
|
2015-02-25 00:51:19 +01:00
|
|
|
#include "transport.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-11-15 21:37:32 +01:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2017-01-12 02:37:35 +01:00
|
|
|
#define MAX_PACKET_SIZE_FS 64
|
|
|
|
#define MAX_PACKET_SIZE_HS 512
|
|
|
|
#define MAX_PACKET_SIZE_SS 1024
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2017-01-04 21:34:38 +01:00
|
|
|
// Kernels before 3.3 have a 16KiB transfer limit That limit was replaced
|
|
|
|
// with a 16MiB global limit in 3.3, but each URB submitted required a
|
|
|
|
// contiguous kernel allocation, so you would get ENOMEM if you tried to
|
|
|
|
// send something larger than the biggest available contiguous kernel
|
|
|
|
// memory region. Large contiguous allocations could be unreliable
|
|
|
|
// on a device kernel that has been running for a while fragmenting its
|
|
|
|
// memory so we start with a larger allocation, and shrink the amount if
|
|
|
|
// necessary.
|
|
|
|
#define USB_FFS_BULK_SIZE 16384
|
2015-12-17 22:45:18 +01:00
|
|
|
|
2017-01-12 02:37:35 +01:00
|
|
|
#define cpu_to_le16(x) htole16(x)
|
|
|
|
#define cpu_to_le32(x) htole32(x)
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-01-04 21:34:38 +01:00
|
|
|
#define FUNCTIONFS_ENDPOINT_ALLOC _IOR('g', 231, __u32)
|
|
|
|
|
2017-04-28 00:00:13 +02:00
|
|
|
static constexpr size_t ENDPOINT_ALLOC_RETRIES = 10;
|
2017-01-04 21:34:38 +01: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 23:51:52 +02:00
|
|
|
static int dummy_fd = -1;
|
|
|
|
|
2014-10-28 02:26:17 +01:00
|
|
|
struct func_desc {
|
|
|
|
struct usb_interface_descriptor intf;
|
|
|
|
struct usb_endpoint_descriptor_no_audio source;
|
|
|
|
struct usb_endpoint_descriptor_no_audio sink;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
2015-06-02 19:36:43 +02:00
|
|
|
struct ss_func_desc {
|
|
|
|
struct usb_interface_descriptor intf;
|
|
|
|
struct usb_endpoint_descriptor_no_audio source;
|
|
|
|
struct usb_ss_ep_comp_descriptor source_comp;
|
|
|
|
struct usb_endpoint_descriptor_no_audio sink;
|
|
|
|
struct usb_ss_ep_comp_descriptor sink_comp;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
2014-10-28 02:26:17 +01:00
|
|
|
struct desc_v1 {
|
|
|
|
struct usb_functionfs_descs_head_v1 {
|
|
|
|
__le32 magic;
|
|
|
|
__le32 length;
|
|
|
|
__le32 fs_count;
|
|
|
|
__le32 hs_count;
|
|
|
|
} __attribute__((packed)) header;
|
|
|
|
struct func_desc fs_descs, hs_descs;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
struct desc_v2 {
|
2015-01-24 02:09:56 +01:00
|
|
|
struct usb_functionfs_descs_head_v2 header;
|
|
|
|
// The rest of the structure depends on the flags in the header.
|
|
|
|
__le32 fs_count;
|
|
|
|
__le32 hs_count;
|
2015-06-02 19:36:43 +02:00
|
|
|
__le32 ss_count;
|
2015-10-05 22:04:03 +02:00
|
|
|
__le32 os_count;
|
2014-10-28 02:26:17 +01:00
|
|
|
struct func_desc fs_descs, hs_descs;
|
2015-06-02 19:36:43 +02:00
|
|
|
struct ss_func_desc ss_descs;
|
2015-10-05 22:04:03 +02:00
|
|
|
struct usb_os_desc_header os_header;
|
|
|
|
struct usb_ext_compat_desc os_desc;
|
2014-10-28 02:26:17 +01:00
|
|
|
} __attribute__((packed));
|
|
|
|
|
2015-05-06 03:26:10 +02:00
|
|
|
static struct func_desc fs_descriptors = {
|
2014-10-28 02:26:17 +01:00
|
|
|
.intf = {
|
|
|
|
.bLength = sizeof(fs_descriptors.intf),
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = ADB_CLASS,
|
|
|
|
.bInterfaceSubClass = ADB_SUBCLASS,
|
|
|
|
.bInterfaceProtocol = ADB_PROTOCOL,
|
|
|
|
.iInterface = 1, /* first string from the provided table */
|
|
|
|
},
|
|
|
|
.source = {
|
|
|
|
.bLength = sizeof(fs_descriptors.source),
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = 1 | USB_DIR_OUT,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = MAX_PACKET_SIZE_FS,
|
|
|
|
},
|
|
|
|
.sink = {
|
|
|
|
.bLength = sizeof(fs_descriptors.sink),
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = 2 | USB_DIR_IN,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = MAX_PACKET_SIZE_FS,
|
2012-01-13 15:13:46 +01:00
|
|
|
},
|
2014-10-28 02:26:17 +01:00
|
|
|
};
|
|
|
|
|
2015-05-06 03:26:10 +02:00
|
|
|
static struct func_desc hs_descriptors = {
|
2014-10-28 02:26:17 +01:00
|
|
|
.intf = {
|
|
|
|
.bLength = sizeof(hs_descriptors.intf),
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = ADB_CLASS,
|
|
|
|
.bInterfaceSubClass = ADB_SUBCLASS,
|
|
|
|
.bInterfaceProtocol = ADB_PROTOCOL,
|
|
|
|
.iInterface = 1, /* first string from the provided table */
|
|
|
|
},
|
|
|
|
.source = {
|
|
|
|
.bLength = sizeof(hs_descriptors.source),
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = 1 | USB_DIR_OUT,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = MAX_PACKET_SIZE_HS,
|
2012-01-13 15:13:46 +01:00
|
|
|
},
|
2014-10-28 02:26:17 +01:00
|
|
|
.sink = {
|
|
|
|
.bLength = sizeof(hs_descriptors.sink),
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = 2 | USB_DIR_IN,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = MAX_PACKET_SIZE_HS,
|
2014-09-02 07:04:44 +02:00
|
|
|
},
|
2012-01-13 15:13:46 +01:00
|
|
|
};
|
|
|
|
|
2015-06-02 19:36:43 +02:00
|
|
|
static struct ss_func_desc ss_descriptors = {
|
|
|
|
.intf = {
|
|
|
|
.bLength = sizeof(ss_descriptors.intf),
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = ADB_CLASS,
|
|
|
|
.bInterfaceSubClass = ADB_SUBCLASS,
|
|
|
|
.bInterfaceProtocol = ADB_PROTOCOL,
|
|
|
|
.iInterface = 1, /* first string from the provided table */
|
|
|
|
},
|
|
|
|
.source = {
|
|
|
|
.bLength = sizeof(ss_descriptors.source),
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = 1 | USB_DIR_OUT,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = MAX_PACKET_SIZE_SS,
|
|
|
|
},
|
|
|
|
.source_comp = {
|
|
|
|
.bLength = sizeof(ss_descriptors.source_comp),
|
|
|
|
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
|
2017-02-11 02:43:00 +01:00
|
|
|
.bMaxBurst = 4,
|
2015-06-02 19:36:43 +02:00
|
|
|
},
|
|
|
|
.sink = {
|
|
|
|
.bLength = sizeof(ss_descriptors.sink),
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = 2 | USB_DIR_IN,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = MAX_PACKET_SIZE_SS,
|
|
|
|
},
|
|
|
|
.sink_comp = {
|
|
|
|
.bLength = sizeof(ss_descriptors.sink_comp),
|
|
|
|
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
|
2017-02-11 02:43:00 +01:00
|
|
|
.bMaxBurst = 4,
|
2015-06-02 19:36:43 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-10-05 22:04:03 +02:00
|
|
|
struct usb_ext_compat_desc os_desc_compat = {
|
|
|
|
.bFirstInterfaceNumber = 0,
|
|
|
|
.Reserved1 = cpu_to_le32(1),
|
|
|
|
.CompatibleID = {0},
|
|
|
|
.SubCompatibleID = {0},
|
|
|
|
.Reserved2 = {0},
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_os_desc_header os_desc_header = {
|
|
|
|
.interface = cpu_to_le32(1),
|
|
|
|
.dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)),
|
|
|
|
.bcdVersion = cpu_to_le32(1),
|
|
|
|
.wIndex = cpu_to_le32(4),
|
|
|
|
.bCount = cpu_to_le32(1),
|
|
|
|
.Reserved = cpu_to_le32(0),
|
|
|
|
};
|
|
|
|
|
2012-01-13 15:13:46 +01:00
|
|
|
#define STR_INTERFACE_ "ADB Interface"
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
struct usb_functionfs_strings_head header;
|
|
|
|
struct {
|
|
|
|
__le16 code;
|
|
|
|
const char str1[sizeof(STR_INTERFACE_)];
|
|
|
|
} __attribute__((packed)) lang0;
|
|
|
|
} __attribute__((packed)) strings = {
|
|
|
|
.header = {
|
|
|
|
.magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
|
|
|
|
.length = cpu_to_le32(sizeof(strings)),
|
|
|
|
.str_count = cpu_to_le32(1),
|
|
|
|
.lang_count = cpu_to_le32(1),
|
|
|
|
},
|
|
|
|
.lang0 = {
|
|
|
|
cpu_to_le16(0x0409), /* en-us */
|
|
|
|
STR_INTERFACE_,
|
|
|
|
},
|
2009-03-04 04:32:55 +01:00
|
|
|
};
|
|
|
|
|
2017-01-12 02:37:35 +01:00
|
|
|
bool init_functionfs(struct usb_handle* h) {
|
2012-01-13 15:13:46 +01:00
|
|
|
ssize_t ret;
|
2014-10-28 02:26:17 +01:00
|
|
|
struct desc_v1 v1_descriptor;
|
|
|
|
struct desc_v2 v2_descriptor;
|
2017-01-04 21:34:38 +01:00
|
|
|
size_t retries = 0;
|
2014-10-28 02:26:17 +01:00
|
|
|
|
|
|
|
v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
|
|
|
|
v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
|
2015-06-02 19:36:43 +02:00
|
|
|
v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
|
2015-10-05 22:04:03 +02:00
|
|
|
FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC;
|
2015-01-24 02:09:56 +01:00
|
|
|
v2_descriptor.fs_count = 3;
|
|
|
|
v2_descriptor.hs_count = 3;
|
2015-06-02 19:36:43 +02:00
|
|
|
v2_descriptor.ss_count = 5;
|
2015-10-05 22:04:03 +02:00
|
|
|
v2_descriptor.os_count = 1;
|
2014-10-28 02:26:17 +01:00
|
|
|
v2_descriptor.fs_descs = fs_descriptors;
|
|
|
|
v2_descriptor.hs_descs = hs_descriptors;
|
2015-06-02 19:36:43 +02:00
|
|
|
v2_descriptor.ss_descs = ss_descriptors;
|
2015-10-05 22:04:03 +02:00
|
|
|
v2_descriptor.os_header = os_desc_header;
|
|
|
|
v2_descriptor.os_desc = os_desc_compat;
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2016-05-26 22:04:52 +02:00
|
|
|
if (h->control < 0) { // might have already done this before
|
|
|
|
D("OPENING %s", USB_FFS_ADB_EP0);
|
|
|
|
h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
|
|
|
|
if (h->control < 0) {
|
|
|
|
D("[ %s: cannot open control endpoint: errno=%d]", USB_FFS_ADB_EP0, errno);
|
|
|
|
goto err;
|
|
|
|
}
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2016-05-26 22:04:52 +02:00
|
|
|
ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
|
2013-12-24 02:46:10 +01:00
|
|
|
if (ret < 0) {
|
2016-05-26 22:04:52 +02:00
|
|
|
v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
|
|
|
|
v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
|
|
|
|
v1_descriptor.header.fs_count = 3;
|
|
|
|
v1_descriptor.header.hs_count = 3;
|
|
|
|
v1_descriptor.fs_descs = fs_descriptors;
|
|
|
|
v1_descriptor.hs_descs = hs_descriptors;
|
|
|
|
D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno);
|
|
|
|
ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
|
|
|
|
if (ret < 0) {
|
|
|
|
D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno);
|
|
|
|
goto err;
|
|
|
|
}
|
2013-12-24 02:46:10 +01:00
|
|
|
}
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2016-05-26 22:04:52 +02:00
|
|
|
ret = adb_write(h->control, &strings, sizeof(strings));
|
|
|
|
if (ret < 0) {
|
|
|
|
D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno);
|
|
|
|
goto err;
|
|
|
|
}
|
2017-03-08 02:30:03 +01:00
|
|
|
//Signal only when writing the descriptors to ffs
|
|
|
|
android::base::SetProperty("sys.usb.ffs.ready", "1");
|
2016-04-27 23:21:52 +02:00
|
|
|
}
|
|
|
|
|
2012-01-13 15:13:46 +01:00
|
|
|
h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
|
|
|
|
if (h->bulk_out < 0) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_ADB_OUT, errno);
|
2012-01-13 15:13:46 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
|
|
|
|
if (h->bulk_in < 0) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_ADB_IN, errno);
|
2012-01-13 15:13:46 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:34:38 +01:00
|
|
|
h->max_rw = MAX_PAYLOAD;
|
|
|
|
while (h->max_rw >= USB_FFS_BULK_SIZE && retries < ENDPOINT_ALLOC_RETRIES) {
|
|
|
|
int ret_in = ioctl(h->bulk_in, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(h->max_rw));
|
|
|
|
int errno_in = errno;
|
|
|
|
int ret_out = ioctl(h->bulk_out, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(h->max_rw));
|
|
|
|
int errno_out = errno;
|
|
|
|
|
|
|
|
if (ret_in || ret_out) {
|
|
|
|
if (errno_in == ENODEV || errno_out == ENODEV) {
|
|
|
|
std::this_thread::sleep_for(100ms);
|
|
|
|
retries += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
h->max_rw /= 2;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
D("[ adb: cannot call endpoint alloc: errno=%d ]", errno);
|
|
|
|
// Kernel pre-allocation could have failed for recoverable reasons.
|
|
|
|
// Continue running with a safe max rw size.
|
2017-02-21 23:37:07 +01:00
|
|
|
h->max_rw = USB_FFS_BULK_SIZE;
|
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 23:51:52 +02:00
|
|
|
return true;
|
2012-01-13 15:13:46 +01:00
|
|
|
|
|
|
|
err:
|
|
|
|
if (h->bulk_in > 0) {
|
|
|
|
adb_close(h->bulk_in);
|
|
|
|
h->bulk_in = -1;
|
|
|
|
}
|
|
|
|
if (h->bulk_out > 0) {
|
|
|
|
adb_close(h->bulk_out);
|
|
|
|
h->bulk_out = -1;
|
|
|
|
}
|
|
|
|
if (h->control > 0) {
|
|
|
|
adb_close(h->control);
|
|
|
|
h->control = -1;
|
|
|
|
}
|
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 23:51:52 +02:00
|
|
|
return false;
|
2012-01-13 15:13:46 +01:00
|
|
|
}
|
|
|
|
|
2016-02-12 23:31:15 +01:00
|
|
|
static void usb_ffs_open_thread(void* x) {
|
2017-01-11 23:39:19 +01:00
|
|
|
struct usb_handle* usb = (struct usb_handle*)x;
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2015-08-29 01:37:29 +02:00
|
|
|
adb_thread_setname("usb ffs open");
|
|
|
|
|
2015-04-18 02:03:59 +02:00
|
|
|
while (true) {
|
2012-01-13 15:13:46 +01:00
|
|
|
// wait until the USB device needs opening
|
2016-09-21 21:37:10 +02: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 23:51:52 +02:00
|
|
|
while (!usb->open_new_connection) {
|
2016-09-21 21:37:10 +02: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 23:51:52 +02:00
|
|
|
}
|
|
|
|
usb->open_new_connection = false;
|
2016-09-21 21:37:10 +02:00
|
|
|
lock.unlock();
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2015-04-18 02:03:59 +02: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 23:51:52 +02:00
|
|
|
if (init_functionfs(usb)) {
|
2012-01-13 15:13:46 +01: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 23:51:52 +02:00
|
|
|
}
|
2016-11-15 21:37:32 +01:00
|
|
|
std::this_thread::sleep_for(1s);
|
2012-01-13 15:13:46 +01:00
|
|
|
}
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ usb_thread - registering device ]");
|
2012-01-13 15:13:46 +01:00
|
|
|
register_usb_transport(usb, 0, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// never gets here
|
2016-02-12 23:31:15 +01:00
|
|
|
abort();
|
2012-01-13 15:13:46 +01:00
|
|
|
}
|
|
|
|
|
2015-12-03 02:30:58 +01:00
|
|
|
static int usb_ffs_write(usb_handle* h, const void* data, int len) {
|
|
|
|
D("about to write (fd=%d, len=%d)", h->bulk_in, len);
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2015-12-03 02:30:58 +01:00
|
|
|
const char* buf = static_cast<const char*>(data);
|
|
|
|
while (len > 0) {
|
2017-01-04 21:34:38 +01:00
|
|
|
int write_len = std::min(h->max_rw, len);
|
2015-12-03 02:30:58 +01:00
|
|
|
int n = adb_write(h->bulk_in, buf, write_len);
|
|
|
|
if (n < 0) {
|
|
|
|
D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buf += n;
|
|
|
|
len -= n;
|
2015-08-25 19:59:45 +02:00
|
|
|
}
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ done fd=%d ]", h->bulk_in);
|
2012-01-13 15:13:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-03 02:30:58 +01:00
|
|
|
static int usb_ffs_read(usb_handle* h, void* data, int len) {
|
|
|
|
D("about to read (fd=%d, len=%d)", h->bulk_out, len);
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2015-12-03 02:30:58 +01:00
|
|
|
char* buf = static_cast<char*>(data);
|
|
|
|
while (len > 0) {
|
2017-01-04 21:34:38 +01:00
|
|
|
int read_len = std::min(h->max_rw, len);
|
2015-12-16 20:00:08 +01:00
|
|
|
int n = adb_read(h->bulk_out, buf, read_len);
|
2015-12-03 02:30:58 +01:00
|
|
|
if (n < 0) {
|
|
|
|
D("ERROR: fd = %d, n = %d: %s", h->bulk_out, n, strerror(errno));
|
2015-08-25 19:59:45 +02:00
|
|
|
return -1;
|
2012-01-13 15:13:46 +01:00
|
|
|
}
|
2015-12-03 02:30:58 +01:00
|
|
|
buf += n;
|
|
|
|
len -= n;
|
2015-08-25 19:59:45 +02:00
|
|
|
}
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ done fd=%d ]", h->bulk_out);
|
2012-01-13 15:13:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-11 23:39:19 +01:00
|
|
|
static void usb_ffs_kick(usb_handle* h) {
|
2012-01-13 15:13:46 +01:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
|
2015-09-23 00:52:57 +02:00
|
|
|
if (err < 0) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
|
2015-09-23 00:52:57 +02:00
|
|
|
}
|
2012-01-13 15:13:46 +01:00
|
|
|
|
|
|
|
err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
|
2015-09-23 00:52:57 +02:00
|
|
|
if (err < 0) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
|
2015-09-23 00:52:57 +02:00
|
|
|
}
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2013-12-24 02:46:10 +01: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 23:51:52 +02:00
|
|
|
// Ditto the comment in usb_adb_kick.
|
|
|
|
h->kicked = true;
|
|
|
|
TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_out));
|
|
|
|
TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_in));
|
|
|
|
}
|
|
|
|
|
2017-01-11 23:39:19 +01:00
|
|
|
static void usb_ffs_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 23:51:52 +02:00
|
|
|
h->kicked = false;
|
2012-01-13 15:13:46 +01:00
|
|
|
adb_close(h->bulk_out);
|
|
|
|
adb_close(h->bulk_in);
|
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 23:51:52 +02:00
|
|
|
// Notify usb_adb_open_thread to open a new connection.
|
2016-09-21 21:37:10 +02: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 23:51:52 +02:00
|
|
|
h->open_new_connection = true;
|
2016-09-21 21:37:10 +02:00
|
|
|
h->lock.unlock();
|
|
|
|
h->notify.notify_one();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 23:39:19 +01:00
|
|
|
static void usb_ffs_init() {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ usb_init - using FunctionFS ]");
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2016-09-21 21:37:10 +02:00
|
|
|
usb_handle* h = new usb_handle();
|
2015-04-22 04:39:52 +02:00
|
|
|
|
2012-01-13 15:13:46 +01:00
|
|
|
h->write = usb_ffs_write;
|
|
|
|
h->read = usb_ffs_read;
|
|
|
|
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 23:51:52 +02:00
|
|
|
h->close = usb_ffs_close;
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ usb_init - starting thread ]");
|
2017-04-13 02:00:49 +02:00
|
|
|
std::thread(usb_ffs_open_thread, h).detach();
|
2012-01-13 15:13:46 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 23:39:19 +01:00
|
|
|
void usb_init() {
|
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 23:51:52 +02:00
|
|
|
dummy_fd = adb_open("/dev/null", O_WRONLY);
|
|
|
|
CHECK_NE(dummy_fd, -1);
|
2017-01-11 23:39:19 +01:00
|
|
|
usb_ffs_init();
|
2012-01-13 15:13:46 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 23:39:19 +01:00
|
|
|
int usb_write(usb_handle* h, const void* data, int len) {
|
2012-01-13 15:13:46 +01:00
|
|
|
return h->write(h, data, len);
|
|
|
|
}
|
|
|
|
|
2017-01-11 23:39:19 +01:00
|
|
|
int usb_read(usb_handle* h, void* data, int len) {
|
2012-01-13 15:13:46 +01:00
|
|
|
return h->read(h, data, len);
|
|
|
|
}
|
2016-09-21 21:37:10 +02:00
|
|
|
|
2017-01-11 23:39:19 +01: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 23:51:52 +02:00
|
|
|
h->close(h);
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2012-01-13 15:13:46 +01:00
|
|
|
|
2017-01-11 23:39:19 +01:00
|
|
|
void usb_kick(usb_handle* h) {
|
2012-01-13 15:13:46 +01:00
|
|
|
h->kick(h);
|
|
|
|
}
|