2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
2012-02-28 16:21:08 +01:00
|
|
|
* the documentation and/or other materials provided with the
|
2009-03-04 04:32:55 +01:00
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
2012-02-28 16:21:08 +01:00
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
2009-03-04 04:32:55 +01:00
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2015-07-24 23:09:44 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pthread.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
2012-04-06 21:39:30 +02:00
|
|
|
#include <sys/stat.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <sys/types.h>
|
2015-07-24 23:09:44 +02:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#include <linux/usbdevice_fs.h>
|
|
|
|
#include <linux/version.h>
|
|
|
|
#include <linux/usb/ch9.h>
|
|
|
|
|
2020-05-28 06:39:37 +02:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
2016-11-15 02:08:47 +01:00
|
|
|
#include <chrono>
|
2015-10-30 19:22:01 +01:00
|
|
|
#include <memory>
|
2016-11-15 02:08:47 +01:00
|
|
|
#include <thread>
|
2015-10-30 19:22:01 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "usb.h"
|
2018-06-26 22:38:35 +02:00
|
|
|
#include "util.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-11-15 02:08:47 +01:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2018-07-26 17:56:09 +02:00
|
|
|
#define MAX_RETRIES 2
|
2009-08-18 16:41:09 +02:00
|
|
|
|
2013-10-02 15:35:38 +02:00
|
|
|
/* Timeout in seconds for usb_wait_for_disconnect.
|
|
|
|
* It doesn't usually take long for a device to disconnect (almost always
|
|
|
|
* under 2 seconds) but we'll time out after 3 seconds just in case.
|
|
|
|
*/
|
|
|
|
#define WAIT_FOR_DISCONNECT_TIMEOUT 3
|
|
|
|
|
2009-08-18 16:41:09 +02:00
|
|
|
#ifdef TRACE_USB
|
2009-03-04 04:32:55 +01:00
|
|
|
#define DBG1(x...) fprintf(stderr, x)
|
|
|
|
#define DBG(x...) fprintf(stderr, x)
|
|
|
|
#else
|
|
|
|
#define DBG(x...)
|
|
|
|
#define DBG1(x...)
|
|
|
|
#endif
|
|
|
|
|
2015-07-24 23:09:44 +02: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. 256KiB contiguous allocations are generally not reliable
|
|
|
|
// on a device kernel that has been running for a while fragmenting its
|
|
|
|
// memory, but that shouldn't be a problem for fastboot on the host.
|
|
|
|
// In 3.6, the contiguous buffer limit was removed by allocating multiple
|
|
|
|
// 16KiB chunks and having the USB driver stitch them back together while
|
|
|
|
// transmitting using a scatter-gather list, so 256KiB bulk transfers should
|
|
|
|
// be reliable.
|
|
|
|
// 256KiB seems to work, but 1MiB bulk transfers lock up my z620 with a 3.13
|
|
|
|
// kernel.
|
fastboot: Increase maximum usbfs bulk size for writes to 256KiB
With a device capable of saturating the bus at SuperSpeed+,
the next bottleneck is the fixed (size-independent) overhead of
the usbfs ioctl() system calls, which includes entering/exiting
the kernel, allocating/deallocating a contiguous buffer for DMA,
configuring/deconfiguring the IOMMU and issuing the DMA to the HC. In
order to saturate the bus from the host software perspective, we must
reach the schedule() call in reap_as() before the next interrupt from
the HC indicating the completion of the URB.
In my experimental setup, with an SS+ capable host and device
and 16 KiB URBs, we reach the schedule() call in 25us, but the
URB is serviced in an estimated 16us, so we lose roughly a third
of the bandwidth. Increasing the URB size to 64KiB there are
65us between interrupts and 55us until schedule(). This means
we usually reach schedule() in time but not always, so we lose a
bit of bandwidth. Increasing it again to 128KiB and we have 128us
between interrupts and 65us until schedule(), so we're now comfortably
saturating the bus. In order to account for differences between hosts,
this CL uses a doubled maximum of 256KiB.
With larger allocation sizes we now risk contiguous allocation
failures, so I implemented a fallback where we try smaller sizes if
a larger one fails.
With this CL download speeds on my hosts are now around 980 MB/s over
SS+ and 440 MB/s over SS.
Bug: 325128548
Change-Id: Ie5ad480c73f2f71a50ce7f75ffb4aaa93ded2f0b
2024-02-14 00:40:37 +01:00
|
|
|
// 128KiB was experimentally found to be enough to saturate the bus at
|
|
|
|
// SuperSpeed+, so we first try double that for writes. If the operation fails
|
|
|
|
// due to a lack of contiguous regions (or an ancient kernel), try smaller sizes
|
|
|
|
// until we find one that works (see LinuxUsbTransport::Write). Reads are less
|
|
|
|
// performance critical so for now just use a known good size.
|
|
|
|
#define MAX_USBFS_BULK_WRITE_SIZE (256 * 1024)
|
|
|
|
#define MAX_USBFS_BULK_READ_SIZE (16 * 1024)
|
|
|
|
|
|
|
|
// This size should pretty much always work (it's compatible with pre-3.3
|
|
|
|
// kernels and it's what we used to use historically), so if it doesn't work
|
|
|
|
// something has gone badly wrong.
|
|
|
|
#define MIN_USBFS_BULK_WRITE_SIZE (16 * 1024)
|
2011-03-08 07:10:16 +01:00
|
|
|
|
2012-02-28 16:21:08 +01:00
|
|
|
struct usb_handle
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
char fname[64];
|
|
|
|
int desc;
|
|
|
|
unsigned char ep_in;
|
|
|
|
unsigned char ep_out;
|
|
|
|
};
|
|
|
|
|
2018-07-26 17:56:09 +02:00
|
|
|
class LinuxUsbTransport : public UsbTransport {
|
2015-10-30 19:22:01 +01:00
|
|
|
public:
|
2018-07-26 17:56:09 +02:00
|
|
|
explicit LinuxUsbTransport(std::unique_ptr<usb_handle> handle, uint32_t ms_timeout = 0)
|
|
|
|
: handle_(std::move(handle)), ms_timeout_(ms_timeout) {}
|
2018-09-04 23:32:54 +02:00
|
|
|
~LinuxUsbTransport() override;
|
2015-10-30 19:22:01 +01:00
|
|
|
|
|
|
|
ssize_t Read(void* data, size_t len) override;
|
|
|
|
ssize_t Write(const void* data, size_t len) override;
|
|
|
|
int Close() override;
|
2018-07-26 17:56:09 +02:00
|
|
|
int Reset() override;
|
2015-10-30 19:22:01 +01:00
|
|
|
int WaitForDisconnect() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<usb_handle> handle_;
|
2018-07-26 17:56:09 +02:00
|
|
|
const uint32_t ms_timeout_;
|
fastboot: Increase maximum usbfs bulk size for writes to 256KiB
With a device capable of saturating the bus at SuperSpeed+,
the next bottleneck is the fixed (size-independent) overhead of
the usbfs ioctl() system calls, which includes entering/exiting
the kernel, allocating/deallocating a contiguous buffer for DMA,
configuring/deconfiguring the IOMMU and issuing the DMA to the HC. In
order to saturate the bus from the host software perspective, we must
reach the schedule() call in reap_as() before the next interrupt from
the HC indicating the completion of the URB.
In my experimental setup, with an SS+ capable host and device
and 16 KiB URBs, we reach the schedule() call in 25us, but the
URB is serviced in an estimated 16us, so we lose roughly a third
of the bandwidth. Increasing the URB size to 64KiB there are
65us between interrupts and 55us until schedule(). This means
we usually reach schedule() in time but not always, so we lose a
bit of bandwidth. Increasing it again to 128KiB and we have 128us
between interrupts and 65us until schedule(), so we're now comfortably
saturating the bus. In order to account for differences between hosts,
this CL uses a doubled maximum of 256KiB.
With larger allocation sizes we now risk contiguous allocation
failures, so I implemented a fallback where we try smaller sizes if
a larger one fails.
With this CL download speeds on my hosts are now around 980 MB/s over
SS+ and 440 MB/s over SS.
Bug: 325128548
Change-Id: Ie5ad480c73f2f71a50ce7f75ffb4aaa93ded2f0b
2024-02-14 00:40:37 +01:00
|
|
|
size_t max_usbfs_bulk_write_size_ = MAX_USBFS_BULK_WRITE_SIZE;
|
2015-10-30 19:22:01 +01:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(LinuxUsbTransport);
|
|
|
|
};
|
|
|
|
|
2013-09-12 00:23:31 +02:00
|
|
|
/* True if name isn't a valid name for a USB device in /sys/bus/usb/devices.
|
|
|
|
* Device names are made up of numbers, dots, and dashes, e.g., '7-1.5'.
|
|
|
|
* We reject interfaces (e.g., '7-1.5:1.0') and host controllers (e.g. 'usb1').
|
|
|
|
* The name must also start with a digit, to disallow '.' and '..'
|
|
|
|
*/
|
2009-03-04 04:32:55 +01:00
|
|
|
static inline int badname(const char *name)
|
|
|
|
{
|
2013-09-12 00:23:31 +02:00
|
|
|
if (!isdigit(*name))
|
|
|
|
return 1;
|
|
|
|
while(*++name) {
|
|
|
|
if(!isdigit(*name) && *name != '.' && *name != '-')
|
|
|
|
return 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check(void *_desc, int len, unsigned type, int size)
|
|
|
|
{
|
2014-07-11 17:59:01 +02:00
|
|
|
struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)_desc;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
if(len < size) return -1;
|
2014-07-11 17:59:01 +02:00
|
|
|
if(hdr->bLength < size) return -1;
|
|
|
|
if(hdr->bLength > len) return -1;
|
|
|
|
if(hdr->bDescriptorType != type) return -1;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-02 15:35:38 +02:00
|
|
|
static int filter_usb_device(char* sysfs_name,
|
2013-09-12 00:23:31 +02:00
|
|
|
char *ptr, int len, int writable,
|
2009-10-07 03:07:49 +02:00
|
|
|
ifc_match_func callback,
|
2009-03-04 04:32:55 +01:00
|
|
|
int *ept_in_id, int *ept_out_id, int *ifc_id)
|
|
|
|
{
|
|
|
|
struct usb_device_descriptor *dev;
|
|
|
|
struct usb_config_descriptor *cfg;
|
|
|
|
struct usb_interface_descriptor *ifc;
|
|
|
|
struct usb_endpoint_descriptor *ept;
|
|
|
|
struct usb_ifc_info info;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
int in, out;
|
|
|
|
unsigned i;
|
|
|
|
unsigned e;
|
2016-02-18 23:52:46 +01:00
|
|
|
|
2014-07-11 17:59:01 +02:00
|
|
|
if (check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE))
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
2014-07-11 17:59:01 +02:00
|
|
|
dev = (struct usb_device_descriptor *)ptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
len -= dev->bLength;
|
|
|
|
ptr += dev->bLength;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2014-07-11 17:59:01 +02:00
|
|
|
if (check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE))
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
2014-07-11 17:59:01 +02:00
|
|
|
cfg = (struct usb_config_descriptor *)ptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
len -= cfg->bLength;
|
|
|
|
ptr += cfg->bLength;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
info.dev_vendor = dev->idVendor;
|
|
|
|
info.dev_product = dev->idProduct;
|
|
|
|
info.dev_class = dev->bDeviceClass;
|
|
|
|
info.dev_subclass = dev->bDeviceSubClass;
|
|
|
|
info.dev_protocol = dev->bDeviceProtocol;
|
2009-10-07 03:07:49 +02:00
|
|
|
info.writable = writable;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2013-09-12 00:23:31 +02:00
|
|
|
snprintf(info.device_path, sizeof(info.device_path), "usb:%s", sysfs_name);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-09-12 00:23:31 +02:00
|
|
|
/* Read device serial number (if there is one).
|
|
|
|
* We read the serial number from sysfs, since it's faster and more
|
|
|
|
* reliable than issuing a control pipe read, and also won't
|
|
|
|
* cause problems for devices which don't like getting descriptor
|
|
|
|
* requests while they're in the middle of flashing.
|
2012-04-06 21:39:30 +02:00
|
|
|
*/
|
2013-09-12 00:23:31 +02:00
|
|
|
info.serial_number[0] = '\0';
|
|
|
|
if (dev->iSerialNumber) {
|
|
|
|
char path[80];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path),
|
|
|
|
"/sys/bus/usb/devices/%s/serial", sysfs_name);
|
|
|
|
path[sizeof(path) - 1] = '\0';
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd >= 0) {
|
|
|
|
int chars_read = read(fd, info.serial_number,
|
|
|
|
sizeof(info.serial_number) - 1);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (chars_read <= 0)
|
|
|
|
info.serial_number[0] = '\0';
|
|
|
|
else if (info.serial_number[chars_read - 1] == '\n') {
|
|
|
|
// strip trailing newline
|
|
|
|
info.serial_number[chars_read - 1] = '\0';
|
|
|
|
}
|
2012-04-06 21:39:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
for(i = 0; i < cfg->bNumInterfaces; i++) {
|
2014-07-11 17:59:01 +02:00
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr;
|
|
|
|
if (check(hdr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE) == 0)
|
|
|
|
break;
|
|
|
|
len -= hdr->bLength;
|
|
|
|
ptr += hdr->bLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len <= 0)
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
2014-07-11 17:59:01 +02:00
|
|
|
|
|
|
|
ifc = (struct usb_interface_descriptor *)ptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
len -= ifc->bLength;
|
|
|
|
ptr += ifc->bLength;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
in = -1;
|
|
|
|
out = -1;
|
|
|
|
info.ifc_class = ifc->bInterfaceClass;
|
|
|
|
info.ifc_subclass = ifc->bInterfaceSubClass;
|
|
|
|
info.ifc_protocol = ifc->bInterfaceProtocol;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
for(e = 0; e < ifc->bNumEndpoints; e++) {
|
2014-07-11 17:59:01 +02:00
|
|
|
while (len > 0) {
|
|
|
|
struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr;
|
|
|
|
if (check(hdr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE) == 0)
|
|
|
|
break;
|
|
|
|
len -= hdr->bLength;
|
|
|
|
ptr += hdr->bLength;
|
|
|
|
}
|
|
|
|
if (len < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ept = (struct usb_endpoint_descriptor *)ptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
len -= ept->bLength;
|
|
|
|
ptr += ept->bLength;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2014-07-11 17:59:01 +02:00
|
|
|
if((ept->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK)
|
2009-03-04 04:32:55 +01:00
|
|
|
continue;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2014-07-11 17:59:01 +02:00
|
|
|
if(ept->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
|
2009-03-04 04:32:55 +01:00
|
|
|
in = ept->bEndpointAddress;
|
|
|
|
} else {
|
|
|
|
out = ept->bEndpointAddress;
|
|
|
|
}
|
2014-12-05 21:11:20 +01:00
|
|
|
|
|
|
|
// For USB 3.0 devices skip the SS Endpoint Companion descriptor
|
|
|
|
if (check((struct usb_descriptor_hdr *)ptr, len,
|
|
|
|
USB_DT_SS_ENDPOINT_COMP, USB_DT_SS_EP_COMP_SIZE) == 0) {
|
|
|
|
len -= USB_DT_SS_EP_COMP_SIZE;
|
|
|
|
ptr += USB_DT_SS_EP_COMP_SIZE;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
info.has_bulk_in = (in != -1);
|
|
|
|
info.has_bulk_out = (out != -1);
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2020-05-28 06:39:37 +02:00
|
|
|
std::string interface;
|
|
|
|
auto path = android::base::StringPrintf("/sys/bus/usb/devices/%s/%s:1.%d/interface",
|
|
|
|
sysfs_name, sysfs_name, ifc->bInterfaceNumber);
|
|
|
|
if (android::base::ReadFileToString(path, &interface)) {
|
2024-02-08 01:24:26 +01:00
|
|
|
if (!interface.empty() && interface.back() == '\n') {
|
|
|
|
interface.pop_back();
|
|
|
|
}
|
2020-05-28 06:39:37 +02:00
|
|
|
snprintf(info.interface, sizeof(info.interface), "%s", interface.c_str());
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
if(callback(&info) == 0) {
|
|
|
|
*ept_in_id = in;
|
|
|
|
*ept_out_id = out;
|
|
|
|
*ifc_id = ifc->bInterfaceNumber;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-09-12 00:23:31 +02:00
|
|
|
static int read_sysfs_string(const char *sysfs_name, const char *sysfs_node,
|
|
|
|
char* buf, int bufsize)
|
|
|
|
{
|
|
|
|
char path[80];
|
|
|
|
int fd, n;
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path),
|
|
|
|
"/sys/bus/usb/devices/%s/%s", sysfs_name, sysfs_node);
|
|
|
|
path[sizeof(path) - 1] = '\0';
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
n = read(fd, buf, bufsize - 1);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (n < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
buf[n] = '\0';
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_sysfs_number(const char *sysfs_name, const char *sysfs_node)
|
|
|
|
{
|
|
|
|
char buf[16];
|
|
|
|
int value;
|
|
|
|
|
|
|
|
if (read_sysfs_string(sysfs_name, sysfs_node, buf, sizeof(buf)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (sscanf(buf, "%d", &value) != 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given the name of a USB device in sysfs, get the name for the same
|
|
|
|
* device in devfs. Returns 0 for success, -1 for failure.
|
|
|
|
*/
|
|
|
|
static int convert_to_devfs_name(const char* sysfs_name,
|
|
|
|
char* devname, int devname_size)
|
|
|
|
{
|
|
|
|
int busnum, devnum;
|
|
|
|
|
|
|
|
busnum = read_sysfs_number(sysfs_name, "busnum");
|
|
|
|
if (busnum < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
devnum = read_sysfs_number(sysfs_name, "devnum");
|
|
|
|
if (devnum < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
snprintf(devname, devname_size, "/dev/bus/usb/%03d/%03d", busnum, devnum);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
static std::unique_ptr<usb_handle> find_usb_device(const char* base, ifc_match_func callback)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-10-30 19:22:01 +01:00
|
|
|
std::unique_ptr<usb_handle> usb;
|
2013-09-12 00:23:31 +02:00
|
|
|
char devname[64];
|
2009-03-04 04:32:55 +01:00
|
|
|
char desc[1024];
|
|
|
|
int n, in, out, ifc;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
struct dirent *de;
|
|
|
|
int fd;
|
2009-10-08 02:24:39 +02:00
|
|
|
int writable;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2016-02-18 23:52:46 +01:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> busdir(opendir(base), closedir);
|
2015-10-30 19:22:01 +01:00
|
|
|
if (busdir == 0) return 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-02-18 23:52:46 +01:00
|
|
|
while ((de = readdir(busdir.get())) && (usb == nullptr)) {
|
2015-10-30 19:22:01 +01:00
|
|
|
if (badname(de->d_name)) continue;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (!convert_to_devfs_name(de->d_name, devname, sizeof(devname))) {
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
// DBG("[ scanning %s ]\n", devname);
|
2009-10-08 02:24:39 +02:00
|
|
|
writable = 1;
|
2015-10-30 19:22:01 +01:00
|
|
|
if ((fd = open(devname, O_RDWR)) < 0) {
|
2009-10-07 03:07:49 +02:00
|
|
|
// Check if we have read-only access, so we can give a helpful
|
|
|
|
// diagnostic like "adb devices" does.
|
|
|
|
writable = 0;
|
2015-10-30 19:22:01 +01:00
|
|
|
if ((fd = open(devname, O_RDONLY)) < 0) {
|
2009-10-07 03:07:49 +02:00
|
|
|
continue;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
n = read(fd, desc, sizeof(desc));
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (filter_usb_device(de->d_name, desc, n, writable, callback, &in, &out, &ifc) == 0) {
|
|
|
|
usb.reset(new usb_handle());
|
2009-03-04 04:32:55 +01:00
|
|
|
strcpy(usb->fname, devname);
|
|
|
|
usb->ep_in = in;
|
|
|
|
usb->ep_out = out;
|
|
|
|
usb->desc = fd;
|
|
|
|
|
|
|
|
n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc);
|
2015-10-30 19:22:01 +01:00
|
|
|
if (n != 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
close(fd);
|
2015-10-30 19:22:01 +01:00
|
|
|
usb.reset();
|
2009-03-04 04:32:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return usb;
|
|
|
|
}
|
|
|
|
|
2018-09-04 23:32:54 +02:00
|
|
|
LinuxUsbTransport::~LinuxUsbTransport() {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
ssize_t LinuxUsbTransport::Write(const void* _data, size_t len)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
unsigned char *data = (unsigned char*) _data;
|
|
|
|
unsigned count = 0;
|
2024-02-05 23:59:44 +01:00
|
|
|
struct usbdevfs_urb urb[2] = {};
|
|
|
|
bool pending[2] = {};
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (handle_->ep_out == 0 || handle_->desc == -1) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2024-02-05 23:59:44 +01:00
|
|
|
auto submit_urb = [&](size_t i) {
|
fastboot: Increase maximum usbfs bulk size for writes to 256KiB
With a device capable of saturating the bus at SuperSpeed+,
the next bottleneck is the fixed (size-independent) overhead of
the usbfs ioctl() system calls, which includes entering/exiting
the kernel, allocating/deallocating a contiguous buffer for DMA,
configuring/deconfiguring the IOMMU and issuing the DMA to the HC. In
order to saturate the bus from the host software perspective, we must
reach the schedule() call in reap_as() before the next interrupt from
the HC indicating the completion of the URB.
In my experimental setup, with an SS+ capable host and device
and 16 KiB URBs, we reach the schedule() call in 25us, but the
URB is serviced in an estimated 16us, so we lose roughly a third
of the bandwidth. Increasing the URB size to 64KiB there are
65us between interrupts and 55us until schedule(). This means
we usually reach schedule() in time but not always, so we lose a
bit of bandwidth. Increasing it again to 128KiB and we have 128us
between interrupts and 65us until schedule(), so we're now comfortably
saturating the bus. In order to account for differences between hosts,
this CL uses a doubled maximum of 256KiB.
With larger allocation sizes we now risk contiguous allocation
failures, so I implemented a fallback where we try smaller sizes if
a larger one fails.
With this CL download speeds on my hosts are now around 980 MB/s over
SS+ and 440 MB/s over SS.
Bug: 325128548
Change-Id: Ie5ad480c73f2f71a50ce7f75ffb4aaa93ded2f0b
2024-02-14 00:40:37 +01:00
|
|
|
while (true) {
|
|
|
|
int xfer = (len > max_usbfs_bulk_write_size_) ? max_usbfs_bulk_write_size_ : len;
|
|
|
|
|
|
|
|
urb[i].type = USBDEVFS_URB_TYPE_BULK;
|
|
|
|
urb[i].endpoint = handle_->ep_out;
|
|
|
|
urb[i].buffer_length = xfer;
|
|
|
|
urb[i].buffer = data;
|
|
|
|
urb[i].usercontext = (void *)i;
|
|
|
|
|
|
|
|
int n = ioctl(handle_->desc, USBDEVFS_SUBMITURB, &urb[i]);
|
|
|
|
if (n != 0) {
|
|
|
|
if (errno == ENOMEM && max_usbfs_bulk_write_size_ > MIN_USBFS_BULK_WRITE_SIZE) {
|
|
|
|
max_usbfs_bulk_write_size_ /= 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DBG("ioctl(USBDEVFS_SUBMITURB) failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
fastboot: Increase maximum usbfs bulk size for writes to 256KiB
With a device capable of saturating the bus at SuperSpeed+,
the next bottleneck is the fixed (size-independent) overhead of
the usbfs ioctl() system calls, which includes entering/exiting
the kernel, allocating/deallocating a contiguous buffer for DMA,
configuring/deconfiguring the IOMMU and issuing the DMA to the HC. In
order to saturate the bus from the host software perspective, we must
reach the schedule() call in reap_as() before the next interrupt from
the HC indicating the completion of the URB.
In my experimental setup, with an SS+ capable host and device
and 16 KiB URBs, we reach the schedule() call in 25us, but the
URB is serviced in an estimated 16us, so we lose roughly a third
of the bandwidth. Increasing the URB size to 64KiB there are
65us between interrupts and 55us until schedule(). This means
we usually reach schedule() in time but not always, so we lose a
bit of bandwidth. Increasing it again to 128KiB and we have 128us
between interrupts and 65us until schedule(), so we're now comfortably
saturating the bus. In order to account for differences between hosts,
this CL uses a doubled maximum of 256KiB.
With larger allocation sizes we now risk contiguous allocation
failures, so I implemented a fallback where we try smaller sizes if
a larger one fails.
With this CL download speeds on my hosts are now around 980 MB/s over
SS+ and 440 MB/s over SS.
Bug: 325128548
Change-Id: Ie5ad480c73f2f71a50ce7f75ffb4aaa93ded2f0b
2024-02-14 00:40:37 +01:00
|
|
|
pending[i] = true;
|
|
|
|
count += xfer;
|
|
|
|
len -= xfer;
|
|
|
|
data += xfer;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
fastboot: Increase maximum usbfs bulk size for writes to 256KiB
With a device capable of saturating the bus at SuperSpeed+,
the next bottleneck is the fixed (size-independent) overhead of
the usbfs ioctl() system calls, which includes entering/exiting
the kernel, allocating/deallocating a contiguous buffer for DMA,
configuring/deconfiguring the IOMMU and issuing the DMA to the HC. In
order to saturate the bus from the host software perspective, we must
reach the schedule() call in reap_as() before the next interrupt from
the HC indicating the completion of the URB.
In my experimental setup, with an SS+ capable host and device
and 16 KiB URBs, we reach the schedule() call in 25us, but the
URB is serviced in an estimated 16us, so we lose roughly a third
of the bandwidth. Increasing the URB size to 64KiB there are
65us between interrupts and 55us until schedule(). This means
we usually reach schedule() in time but not always, so we lose a
bit of bandwidth. Increasing it again to 128KiB and we have 128us
between interrupts and 65us until schedule(), so we're now comfortably
saturating the bus. In order to account for differences between hosts,
this CL uses a doubled maximum of 256KiB.
With larger allocation sizes we now risk contiguous allocation
failures, so I implemented a fallback where we try smaller sizes if
a larger one fails.
With this CL download speeds on my hosts are now around 980 MB/s over
SS+ and 440 MB/s over SS.
Bug: 325128548
Change-Id: Ie5ad480c73f2f71a50ce7f75ffb4aaa93ded2f0b
2024-02-14 00:40:37 +01:00
|
|
|
return true;
|
|
|
|
}
|
2024-02-05 23:59:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
auto reap_urb = [&](size_t i) {
|
|
|
|
while (pending[i]) {
|
|
|
|
struct usbdevfs_urb *urbp;
|
|
|
|
int res = ioctl(handle_->desc, USBDEVFS_REAPURB, &urbp);
|
|
|
|
if (res != 0) {
|
|
|
|
DBG("ioctl(USBDEVFS_REAPURB) failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size_t done = (size_t)urbp->usercontext;
|
|
|
|
if (done >= 2 || !pending[done]) {
|
|
|
|
DBG("unexpected urb\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (urbp->status != 0 || urbp->actual_length != urbp->buffer_length) {
|
|
|
|
DBG("urb returned error\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pending[done] = false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!submit_urb(0)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (len > 0) {
|
|
|
|
if (!submit_urb(1)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!reap_urb(0)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (len <= 0) {
|
|
|
|
if (!reap_urb(1)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
if (!submit_urb(0)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!reap_urb(1)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!reap_urb(0)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
ssize_t LinuxUsbTransport::Read(void* _data, size_t len)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
unsigned char *data = (unsigned char*) _data;
|
|
|
|
unsigned count = 0;
|
|
|
|
struct usbdevfs_bulktransfer bulk;
|
2009-08-18 16:41:09 +02:00
|
|
|
int n, retry;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (handle_->ep_in == 0 || handle_->desc == -1) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2016-11-15 02:08:47 +01:00
|
|
|
while (len > 0) {
|
fastboot: Increase maximum usbfs bulk size for writes to 256KiB
With a device capable of saturating the bus at SuperSpeed+,
the next bottleneck is the fixed (size-independent) overhead of
the usbfs ioctl() system calls, which includes entering/exiting
the kernel, allocating/deallocating a contiguous buffer for DMA,
configuring/deconfiguring the IOMMU and issuing the DMA to the HC. In
order to saturate the bus from the host software perspective, we must
reach the schedule() call in reap_as() before the next interrupt from
the HC indicating the completion of the URB.
In my experimental setup, with an SS+ capable host and device
and 16 KiB URBs, we reach the schedule() call in 25us, but the
URB is serviced in an estimated 16us, so we lose roughly a third
of the bandwidth. Increasing the URB size to 64KiB there are
65us between interrupts and 55us until schedule(). This means
we usually reach schedule() in time but not always, so we lose a
bit of bandwidth. Increasing it again to 128KiB and we have 128us
between interrupts and 65us until schedule(), so we're now comfortably
saturating the bus. In order to account for differences between hosts,
this CL uses a doubled maximum of 256KiB.
With larger allocation sizes we now risk contiguous allocation
failures, so I implemented a fallback where we try smaller sizes if
a larger one fails.
With this CL download speeds on my hosts are now around 980 MB/s over
SS+ and 440 MB/s over SS.
Bug: 325128548
Change-Id: Ie5ad480c73f2f71a50ce7f75ffb4aaa93ded2f0b
2024-02-14 00:40:37 +01:00
|
|
|
int xfer = (len > MAX_USBFS_BULK_READ_SIZE) ? MAX_USBFS_BULK_READ_SIZE : len;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
bulk.ep = handle_->ep_in;
|
2009-03-04 04:32:55 +01:00
|
|
|
bulk.len = xfer;
|
|
|
|
bulk.data = data;
|
2018-07-26 17:56:09 +02:00
|
|
|
bulk.timeout = ms_timeout_;
|
2009-08-18 16:41:09 +02:00
|
|
|
retry = 0;
|
|
|
|
|
2016-11-15 02:08:47 +01:00
|
|
|
do {
|
|
|
|
DBG("[ usb read %d fd = %d], fname=%s\n", xfer, handle_->desc, handle_->fname);
|
|
|
|
n = ioctl(handle_->desc, USBDEVFS_BULK, &bulk);
|
|
|
|
DBG("[ usb read %d ] = %d, fname=%s, Retry %d \n", xfer, n, handle_->fname, retry);
|
2009-08-18 16:41:09 +02:00
|
|
|
|
2016-11-15 02:08:47 +01:00
|
|
|
if (n < 0) {
|
|
|
|
DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno));
|
|
|
|
if (++retry > MAX_RETRIES) return -1;
|
2018-07-26 17:56:09 +02:00
|
|
|
std::this_thread::sleep_for(100ms);
|
2016-11-15 02:08:47 +01:00
|
|
|
}
|
|
|
|
} while (n < 0);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
count += n;
|
|
|
|
len -= n;
|
|
|
|
data += n;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
if(n < xfer) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
int LinuxUsbTransport::Close()
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
int fd;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
fd = handle_->desc;
|
|
|
|
handle_->desc = -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
if(fd >= 0) {
|
|
|
|
close(fd);
|
|
|
|
DBG("[ usb closed %d ]\n", fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-26 17:56:09 +02:00
|
|
|
int LinuxUsbTransport::Reset() {
|
|
|
|
int ret = 0;
|
|
|
|
// We reset the USB connection
|
|
|
|
if ((ret = ioctl(handle_->desc, USBDEVFS_RESET, 0))) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-03 18:30:46 +02:00
|
|
|
std::unique_ptr<UsbTransport> usb_open(ifc_match_func callback, uint32_t timeout_ms) {
|
|
|
|
std::unique_ptr<UsbTransport> result;
|
2015-10-30 19:22:01 +01:00
|
|
|
std::unique_ptr<usb_handle> handle = find_usb_device("/sys/bus/usb/devices", callback);
|
2023-09-03 18:30:46 +02:00
|
|
|
|
|
|
|
if (handle) {
|
|
|
|
result = std::make_unique<LinuxUsbTransport>(std::move(handle), timeout_ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2013-10-02 15:35:38 +02:00
|
|
|
|
|
|
|
/* Wait for the system to notice the device is gone, so that a subsequent
|
|
|
|
* fastboot command won't try to access the device before it's rebooted.
|
|
|
|
* Returns 0 for success, -1 for timeout.
|
|
|
|
*/
|
2015-10-30 19:22:01 +01:00
|
|
|
int LinuxUsbTransport::WaitForDisconnect()
|
2013-10-02 15:35:38 +02:00
|
|
|
{
|
|
|
|
double deadline = now() + WAIT_FOR_DISCONNECT_TIMEOUT;
|
|
|
|
while (now() < deadline) {
|
2016-11-15 02:08:47 +01:00
|
|
|
if (access(handle_->fname, F_OK)) return 0;
|
|
|
|
std::this_thread::sleep_for(50ms);
|
2013-10-02 15:35:38 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|