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 <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-07-24 00:20:09 +02:00
|
|
|
#include <linux/usb/ch9.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <linux/usbdevice_fs.h>
|
|
|
|
#include <linux/version.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
2010-02-19 23:45:57 +01:00
|
|
|
#include <sys/time.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2015-07-24 00:20:09 +02:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <list>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
2015-04-27 23:20:17 +02:00
|
|
|
|
|
|
|
#include <base/file.h>
|
|
|
|
#include <base/stringprintf.h>
|
|
|
|
#include <base/strings.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#include "adb.h"
|
2015-02-25 00:51:19 +01:00
|
|
|
#include "transport.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* usb scan debugging is waaaay too verbose */
|
|
|
|
#define DBGX(x...)
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
struct usb_handle {
|
|
|
|
~usb_handle() {
|
|
|
|
if (fd != -1) unix_close(fd);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
std::string path;
|
|
|
|
int fd = -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned char ep_in;
|
|
|
|
unsigned char ep_out;
|
|
|
|
|
|
|
|
unsigned zero_mask;
|
2015-07-24 00:20:09 +02:00
|
|
|
unsigned writeable = 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
usbdevfs_urb urb_in;
|
|
|
|
usbdevfs_urb urb_out;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
bool urb_in_busy = false;
|
|
|
|
bool urb_out_busy = false;
|
|
|
|
bool dead = false;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
std::condition_variable cv;
|
|
|
|
std::mutex mutex;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
// for garbage collecting disconnected devices
|
2015-07-24 00:20:09 +02:00
|
|
|
bool mark;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
// ID of thread currently in REAPURB
|
2015-07-24 00:20:09 +02:00
|
|
|
pthread_t reaper_thread = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
};
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
static std::mutex g_usb_handles_mutex;
|
|
|
|
static std::list<usb_handle*> g_usb_handles;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
static int is_known_device(const char* dev_name) {
|
|
|
|
std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
|
|
|
|
for (usb_handle* usb : g_usb_handles) {
|
|
|
|
if (usb->path == dev_name) {
|
2009-03-04 04:32:55 +01:00
|
|
|
// set mark flag to indicate this device is still alive
|
2015-07-24 00:20:09 +02:00
|
|
|
usb->mark = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
static void kick_disconnected_devices() {
|
|
|
|
std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
|
2009-03-04 04:32:55 +01:00
|
|
|
// kick any devices in the device list that were not found in the device scan
|
2015-07-24 00:20:09 +02:00
|
|
|
for (usb_handle* usb : g_usb_handles) {
|
|
|
|
if (!usb->mark) {
|
2009-03-04 04:32:55 +01:00
|
|
|
usb_kick(usb);
|
|
|
|
} else {
|
2015-07-24 00:20:09 +02:00
|
|
|
usb->mark = false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
static inline bool contains_non_digit(const char* name) {
|
|
|
|
while (*name) {
|
|
|
|
if (!isdigit(*name++)) return true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-07-24 00:20:09 +02:00
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
static void find_usb_device(const std::string& base,
|
2009-08-08 18:37:44 +02:00
|
|
|
void (*register_device_callback)
|
2015-07-24 00:20:09 +02:00
|
|
|
(const char*, const char*, unsigned char, unsigned char, int, int, unsigned))
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-07-24 00:20:09 +02:00
|
|
|
std::unique_ptr<DIR, int(*)(DIR*)> bus_dir(opendir(base.c_str()), closedir);
|
|
|
|
if (!bus_dir) return;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
dirent* de;
|
|
|
|
while ((de = readdir(bus_dir.get())) != 0) {
|
|
|
|
if (contains_non_digit(de->d_name)) continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
std::string bus_name = base + "/" + de->d_name;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
std::unique_ptr<DIR, int(*)(DIR*)> dev_dir(opendir(bus_name.c_str()), closedir);
|
|
|
|
if (!dev_dir) continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
while ((de = readdir(dev_dir.get()))) {
|
2011-01-08 05:18:14 +01:00
|
|
|
unsigned char devdesc[4096];
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned char* bufptr = devdesc;
|
2010-01-17 06:52:27 +01:00
|
|
|
unsigned char* bufend;
|
2009-03-04 04:32:55 +01:00
|
|
|
struct usb_device_descriptor* device;
|
|
|
|
struct usb_config_descriptor* config;
|
|
|
|
struct usb_interface_descriptor* interface;
|
|
|
|
struct usb_endpoint_descriptor *ep1, *ep2;
|
|
|
|
unsigned zero_mask = 0;
|
|
|
|
unsigned vid, pid;
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
if (contains_non_digit(de->d_name)) continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
std::string dev_name = bus_name + "/" + de->d_name;
|
|
|
|
if (is_known_device(dev_name.c_str())) {
|
2009-03-04 04:32:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
int fd = unix_open(dev_name.c_str(), O_RDONLY | O_CLOEXEC);
|
|
|
|
if (fd == -1) {
|
2009-03-04 04:32:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
size_t desclength = unix_read(fd, devdesc, sizeof(devdesc));
|
2010-01-17 06:52:27 +01:00
|
|
|
bufend = bufptr + desclength;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
// should have device and configuration descriptors, and atleast two endpoints
|
|
|
|
if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("desclength %zu is too small", desclength);
|
2015-05-23 05:09:06 +02:00
|
|
|
unix_close(fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
device = (struct usb_device_descriptor*)bufptr;
|
|
|
|
bufptr += USB_DT_DEVICE_SIZE;
|
|
|
|
|
|
|
|
if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {
|
2015-05-23 05:09:06 +02:00
|
|
|
unix_close(fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-09-22 20:09:44 +02:00
|
|
|
vid = device->idVendor;
|
|
|
|
pid = device->idProduct;
|
2015-07-24 00:20:09 +02:00
|
|
|
DBGX("[ %s is V:%04x P:%04x ]\n", dev_name.c_str(), vid, pid);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
// should have config descriptor next
|
|
|
|
config = (struct usb_config_descriptor *)bufptr;
|
|
|
|
bufptr += USB_DT_CONFIG_SIZE;
|
|
|
|
if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("usb_config_descriptor not found");
|
2015-05-23 05:09:06 +02:00
|
|
|
unix_close(fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-01-17 06:52:27 +01:00
|
|
|
// loop through all the descriptors and look for the ADB interface
|
|
|
|
while (bufptr < bufend) {
|
|
|
|
unsigned char length = bufptr[0];
|
|
|
|
unsigned char type = bufptr[1];
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-01-17 06:52:27 +01:00
|
|
|
if (type == USB_DT_INTERFACE) {
|
|
|
|
interface = (struct usb_interface_descriptor *)bufptr;
|
|
|
|
bufptr += length;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-01-17 06:52:27 +01:00
|
|
|
if (length != USB_DT_INTERFACE_SIZE) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("interface descriptor has wrong size");
|
2010-01-17 06:52:27 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2010-01-17 06:52:27 +01:00
|
|
|
DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d,"
|
|
|
|
"bInterfaceProtocol: %d, bNumEndpoints: %d\n",
|
|
|
|
interface->bInterfaceClass, interface->bInterfaceSubClass,
|
|
|
|
interface->bInterfaceProtocol, interface->bNumEndpoints);
|
|
|
|
|
|
|
|
if (interface->bNumEndpoints == 2 &&
|
|
|
|
is_adb_interface(vid, pid, interface->bInterfaceClass,
|
|
|
|
interface->bInterfaceSubClass, interface->bInterfaceProtocol)) {
|
|
|
|
|
2012-04-20 20:21:14 +02:00
|
|
|
struct stat st;
|
|
|
|
char pathbuf[128];
|
|
|
|
char link[256];
|
2015-07-24 00:20:09 +02:00
|
|
|
char *devpath = nullptr;
|
2012-04-20 20:21:14 +02:00
|
|
|
|
2010-01-17 06:52:27 +01:00
|
|
|
DBGX("looking for bulk endpoints\n");
|
|
|
|
// looks like ADB...
|
|
|
|
ep1 = (struct usb_endpoint_descriptor *)bufptr;
|
|
|
|
bufptr += USB_DT_ENDPOINT_SIZE;
|
2014-05-16 21:51:41 +02:00
|
|
|
// For USB 3.0 SuperSpeed devices, skip potential
|
|
|
|
// USB 3.0 SuperSpeed Endpoint Companion descriptor
|
|
|
|
if (bufptr+2 <= devdesc + desclength &&
|
|
|
|
bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
|
|
|
|
bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
|
|
|
|
bufptr += USB_DT_SS_EP_COMP_SIZE;
|
|
|
|
}
|
2010-01-17 06:52:27 +01:00
|
|
|
ep2 = (struct usb_endpoint_descriptor *)bufptr;
|
|
|
|
bufptr += USB_DT_ENDPOINT_SIZE;
|
2014-05-16 21:51:41 +02:00
|
|
|
if (bufptr+2 <= devdesc + desclength &&
|
|
|
|
bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
|
|
|
|
bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
|
|
|
|
bufptr += USB_DT_SS_EP_COMP_SIZE;
|
|
|
|
}
|
2010-01-17 06:52:27 +01:00
|
|
|
|
|
|
|
if (bufptr > devdesc + desclength ||
|
|
|
|
ep1->bLength != USB_DT_ENDPOINT_SIZE ||
|
|
|
|
ep1->bDescriptorType != USB_DT_ENDPOINT ||
|
|
|
|
ep2->bLength != USB_DT_ENDPOINT_SIZE ||
|
|
|
|
ep2->bDescriptorType != USB_DT_ENDPOINT) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("endpoints not found");
|
2010-01-17 06:52:27 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// both endpoints should be bulk
|
|
|
|
if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK ||
|
|
|
|
ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("bulk endpoints not found");
|
2010-01-17 06:52:27 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* aproto 01 needs 0 termination */
|
|
|
|
if(interface->bInterfaceProtocol == 0x01) {
|
|
|
|
zero_mask = ep1->wMaxPacketSize - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have a match. now we just need to figure out which is in and which is out.
|
2015-07-24 00:20:09 +02:00
|
|
|
unsigned char local_ep_in, local_ep_out;
|
2010-01-17 06:52:27 +01:00
|
|
|
if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
|
|
|
|
local_ep_in = ep1->bEndpointAddress;
|
|
|
|
local_ep_out = ep2->bEndpointAddress;
|
|
|
|
} else {
|
|
|
|
local_ep_in = ep2->bEndpointAddress;
|
|
|
|
local_ep_out = ep1->bEndpointAddress;
|
|
|
|
}
|
|
|
|
|
2012-04-20 20:21:14 +02:00
|
|
|
// Determine the device path
|
|
|
|
if (!fstat(fd, &st) && S_ISCHR(st.st_mode)) {
|
|
|
|
snprintf(pathbuf, sizeof(pathbuf), "/sys/dev/char/%d:%d",
|
|
|
|
major(st.st_rdev), minor(st.st_rdev));
|
2015-07-28 06:21:39 +02:00
|
|
|
ssize_t link_len = readlink(pathbuf, link, sizeof(link) - 1);
|
2012-04-20 20:21:14 +02:00
|
|
|
if (link_len > 0) {
|
|
|
|
link[link_len] = '\0';
|
2015-07-28 06:21:39 +02:00
|
|
|
const char* slash = strrchr(link, '/');
|
2012-04-20 20:21:14 +02:00
|
|
|
if (slash) {
|
|
|
|
snprintf(pathbuf, sizeof(pathbuf),
|
|
|
|
"usb:%s", slash + 1);
|
|
|
|
devpath = pathbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
register_device_callback(dev_name.c_str(), devpath,
|
2012-04-20 20:21:14 +02:00
|
|
|
local_ep_in, local_ep_out,
|
2010-01-17 06:52:27 +01:00
|
|
|
interface->bInterfaceNumber, device->iSerialNumber, zero_mask);
|
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
} else {
|
2010-01-17 06:52:27 +01:00
|
|
|
bufptr += length;
|
|
|
|
}
|
|
|
|
} // end of while
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-23 05:09:06 +02:00
|
|
|
unix_close(fd);
|
2015-07-24 00:20:09 +02:00
|
|
|
}
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
static int usb_bulk_write(usb_handle* h, const void* data, int len) {
|
|
|
|
std::unique_lock<std::mutex> lock(h->mutex);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("++ usb_bulk_write ++");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
usbdevfs_urb* urb = &h->urb_out;
|
2009-03-04 04:32:55 +01:00
|
|
|
memset(urb, 0, sizeof(*urb));
|
|
|
|
urb->type = USBDEVFS_URB_TYPE_BULK;
|
|
|
|
urb->endpoint = h->ep_out;
|
|
|
|
urb->status = -1;
|
2015-07-24 00:20:09 +02:00
|
|
|
urb->buffer = const_cast<void*>(data);
|
2009-03-04 04:32:55 +01:00
|
|
|
urb->buffer_length = len;
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
if (h->dead) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
if (TEMP_FAILURE_RETRY(ioctl(h->fd, USBDEVFS_SUBMITURB, urb)) == -1) {
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
h->urb_out_busy = true;
|
|
|
|
while (true) {
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
if (h->cv.wait_until(lock, now + 5s) == std::cv_status::timeout || h->dead) {
|
|
|
|
// TODO: call USBDEVFS_DISCARDURB?
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-07-24 00:20:09 +02:00
|
|
|
if (!h->urb_out_busy) {
|
|
|
|
if (urb->status != 0) {
|
|
|
|
errno = -urb->status;
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-07-24 00:20:09 +02:00
|
|
|
return urb->actual_length;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
static int usb_bulk_read(usb_handle* h, void* data, int len) {
|
|
|
|
std::unique_lock<std::mutex> lock(h->mutex);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("++ usb_bulk_read ++");
|
2015-07-24 00:20:09 +02:00
|
|
|
|
|
|
|
usbdevfs_urb* urb = &h->urb_in;
|
2009-03-04 04:32:55 +01:00
|
|
|
memset(urb, 0, sizeof(*urb));
|
|
|
|
urb->type = USBDEVFS_URB_TYPE_BULK;
|
|
|
|
urb->endpoint = h->ep_in;
|
|
|
|
urb->status = -1;
|
|
|
|
urb->buffer = data;
|
|
|
|
urb->buffer_length = len;
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
if (h->dead) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
if (TEMP_FAILURE_RETRY(ioctl(h->fd, USBDEVFS_SUBMITURB, urb)) == -1) {
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
h->urb_in_busy = true;
|
|
|
|
while (true) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ reap urb - wait ]");
|
2009-03-04 04:32:55 +01:00
|
|
|
h->reaper_thread = pthread_self();
|
2015-07-24 00:20:09 +02:00
|
|
|
int fd = h->fd;
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
// This ioctl must not have TEMP_FAILURE_RETRY because we send SIGALRM to break out.
|
|
|
|
usbdevfs_urb* out = nullptr;
|
|
|
|
int res = ioctl(fd, USBDEVFS_REAPURB, &out);
|
2011-03-16 23:57:42 +01:00
|
|
|
int saved_errno = errno;
|
2015-07-24 00:20:09 +02:00
|
|
|
|
|
|
|
lock.lock();
|
2009-03-04 04:32:55 +01:00
|
|
|
h->reaper_thread = 0;
|
2015-07-24 00:20:09 +02:00
|
|
|
if (h->dead) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-07-24 00:20:09 +02:00
|
|
|
if (res < 0) {
|
|
|
|
if (saved_errno == EINTR) {
|
2009-03-04 04:32:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ reap urb - error ]");
|
2015-07-24 00:20:09 +02:00
|
|
|
errno = saved_errno;
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ urb @%p status = %d, actual = %d ]", out, out->status, out->actual_length);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
if (out == &h->urb_in) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ reap urb - IN complete ]");
|
2015-07-24 00:20:09 +02:00
|
|
|
h->urb_in_busy = false;
|
|
|
|
if (urb->status != 0) {
|
|
|
|
errno = -urb->status;
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-07-24 00:20:09 +02:00
|
|
|
return urb->actual_length;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-07-24 00:20:09 +02:00
|
|
|
if (out == &h->urb_out) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ reap urb - OUT compelete ]");
|
2015-07-24 00:20:09 +02:00
|
|
|
h->urb_out_busy = false;
|
|
|
|
h->cv.notify_all();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int usb_write(usb_handle *h, const void *_data, int len)
|
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("++ usb_write ++");
|
2015-07-13 20:12:28 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned char *data = (unsigned char*) _data;
|
2015-07-13 20:12:28 +02:00
|
|
|
int n = usb_bulk_write(h, data, len);
|
2015-07-24 00:20:09 +02:00
|
|
|
if (n != len) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("ERROR: n = %d, errno = %d (%s)", n, errno, strerror(errno));
|
2015-07-13 20:12:28 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
if (h->zero_mask && !(len & h->zero_mask)) {
|
|
|
|
// If we need 0-markers and our transfer is an even multiple of the packet size,
|
|
|
|
// then send a zero marker.
|
|
|
|
return usb_bulk_write(h, _data, 0);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("-- usb_write --");
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_read(usb_handle *h, void *_data, int len)
|
|
|
|
{
|
|
|
|
unsigned char *data = (unsigned char*) _data;
|
|
|
|
int n;
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("++ usb_read ++");
|
2009-03-04 04:32:55 +01:00
|
|
|
while(len > 0) {
|
2015-07-13 20:12:28 +02:00
|
|
|
int xfer = len;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ usb read %d fd = %d], path=%s", xfer, h->fd, h->path.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
n = usb_bulk_read(h, data, xfer);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ usb read %d ] = %d, path=%s", xfer, n, h->path.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
if(n != xfer) {
|
2015-07-24 00:20:09 +02:00
|
|
|
if((errno == ETIMEDOUT) && (h->fd != -1)) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ timeout ]");
|
2009-03-04 04:32:55 +01:00
|
|
|
if(n > 0){
|
|
|
|
data += n;
|
|
|
|
len -= n;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-03 02:44:28 +02:00
|
|
|
D("ERROR: n = %d, errno = %d (%s)",
|
2009-03-04 04:32:55 +01:00
|
|
|
n, errno, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len -= xfer;
|
|
|
|
data += xfer;
|
|
|
|
}
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("-- usb_read --");
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
void usb_kick(usb_handle* h) {
|
|
|
|
std::lock_guard<std::mutex> lock(h->mutex);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ kicking %p (fd = %d) ]", h, h->fd);
|
2015-07-24 00:20:09 +02:00
|
|
|
if (!h->dead) {
|
|
|
|
h->dead = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2009-08-08 18:37:44 +02:00
|
|
|
if (h->writeable) {
|
|
|
|
/* HACK ALERT!
|
|
|
|
** Sometimes we get stuck in ioctl(USBDEVFS_REAPURB).
|
|
|
|
** This is a workaround for that problem.
|
|
|
|
*/
|
|
|
|
if (h->reaper_thread) {
|
|
|
|
pthread_kill(h->reaper_thread, SIGALRM);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2009-08-08 18:37:44 +02:00
|
|
|
/* cancel any pending transactions
|
|
|
|
** these will quietly fail if the txns are not active,
|
|
|
|
** but this ensures that a reader blocked on REAPURB
|
|
|
|
** will get unblocked
|
|
|
|
*/
|
2015-07-24 00:20:09 +02:00
|
|
|
ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_in);
|
|
|
|
ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_out);
|
2009-08-08 18:37:44 +02:00
|
|
|
h->urb_in.status = -ENODEV;
|
|
|
|
h->urb_out.status = -ENODEV;
|
2015-07-24 00:20:09 +02:00
|
|
|
h->urb_in_busy = false;
|
|
|
|
h->urb_out_busy = false;
|
|
|
|
h->cv.notify_all();
|
2009-08-08 18:37:44 +02:00
|
|
|
} else {
|
|
|
|
unregister_usb_transport(h);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
int usb_close(usb_handle* h) {
|
|
|
|
std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
|
|
|
|
g_usb_handles.remove(h);
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("-- usb close %p (fd = %d) --", h, h->fd);
|
2015-07-24 00:20:09 +02:00
|
|
|
|
|
|
|
delete h;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-27 23:20:17 +02:00
|
|
|
static void register_device(const char* dev_name, const char* dev_path,
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned char ep_in, unsigned char ep_out,
|
2015-05-07 01:48:52 +02:00
|
|
|
int interface, int serial_index,
|
|
|
|
unsigned zero_mask) {
|
|
|
|
// Since Linux will not reassign the device ID (and dev_name) as long as the
|
|
|
|
// device is open, we can add to the list here once we open it and remove
|
|
|
|
// from the list when we're finally closed and everything will work out
|
|
|
|
// fine.
|
|
|
|
//
|
2015-07-24 00:20:09 +02:00
|
|
|
// If we have a usb_handle on the list of handles with a matching name, we
|
2015-05-07 01:48:52 +02:00
|
|
|
// have no further work to do.
|
2015-07-24 00:20:09 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
|
|
|
|
for (usb_handle* usb: g_usb_handles) {
|
|
|
|
if (usb->path == dev_name) {
|
|
|
|
return;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ usb located new device %s (%d/%d/%d) ]", dev_name, ep_in, ep_out, interface);
|
2015-07-24 00:20:09 +02:00
|
|
|
std::unique_ptr<usb_handle> usb(new usb_handle);
|
|
|
|
usb->path = dev_name;
|
2009-03-04 04:32:55 +01:00
|
|
|
usb->ep_in = ep_in;
|
|
|
|
usb->ep_out = ep_out;
|
|
|
|
usb->zero_mask = zero_mask;
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
// Initialize mark so we don't get garbage collected after the device scan.
|
|
|
|
usb->mark = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
usb->fd = unix_open(usb->path.c_str(), O_RDWR | O_CLOEXEC);
|
|
|
|
if (usb->fd == -1) {
|
2015-04-25 23:44:23 +02:00
|
|
|
// Opening RW failed, so see if we have RO access.
|
2015-07-24 00:20:09 +02:00
|
|
|
usb->fd = unix_open(usb->path.c_str(), O_RDONLY | O_CLOEXEC);
|
|
|
|
if (usb->fd == -1) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ usb open %s failed: %s]", usb->path.c_str(), strerror(errno));
|
2015-04-25 23:44:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-08-08 18:37:44 +02:00
|
|
|
usb->writeable = 0;
|
2015-04-25 23:44:23 +02:00
|
|
|
}
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ usb opened %s%s, fd=%d]",
|
2015-07-24 00:20:09 +02:00
|
|
|
usb->path.c_str(), (usb->writeable ? "" : " (read-only)"), usb->fd);
|
2015-04-25 23:44:23 +02:00
|
|
|
|
|
|
|
if (usb->writeable) {
|
2015-07-24 00:20:09 +02:00
|
|
|
if (ioctl(usb->fd, USBDEVFS_CLAIMINTERFACE, &interface) != 0) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ usb ioctl(%d, USBDEVFS_CLAIMINTERFACE) failed: %s]", usb->fd, strerror(errno));
|
2015-04-25 23:44:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-08-08 18:37:44 +02:00
|
|
|
}
|
|
|
|
|
2015-04-27 23:20:17 +02:00
|
|
|
// Read the device's serial number.
|
2015-05-07 01:48:52 +02:00
|
|
|
std::string serial_path = android::base::StringPrintf(
|
|
|
|
"/sys/bus/usb/devices/%s/serial", dev_path + 4);
|
2015-04-27 23:20:17 +02:00
|
|
|
std::string serial;
|
|
|
|
if (!android::base::ReadFileToString(serial_path, &serial)) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("[ usb read %s failed: %s ]", serial_path.c_str(), strerror(errno));
|
2015-05-07 01:48:52 +02:00
|
|
|
// We don't actually want to treat an unknown serial as an error because
|
|
|
|
// devices aren't able to communicate a serial number in early bringup.
|
|
|
|
// http://b/20883914
|
|
|
|
serial = "";
|
2009-08-08 18:37:44 +02:00
|
|
|
}
|
2015-04-27 23:20:17 +02:00
|
|
|
serial = android::base::Trim(serial);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-07 01:48:52 +02:00
|
|
|
// Add to the end of the active handles.
|
2015-07-24 00:20:09 +02:00
|
|
|
usb_handle* done_usb = usb.release();
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
|
|
|
|
g_usb_handles.push_back(done_usb);
|
|
|
|
}
|
|
|
|
register_usb_transport(done_usb, serial.c_str(), dev_path, done_usb->writeable);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-07 01:48:52 +02:00
|
|
|
static void* device_poll_thread(void* unused) {
|
2015-08-29 01:37:29 +02:00
|
|
|
adb_thread_setname("device poll");
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Created device thread");
|
2015-05-07 01:48:52 +02:00
|
|
|
while (true) {
|
|
|
|
// TODO: Use inotify.
|
2009-03-04 04:32:55 +01:00
|
|
|
find_usb_device("/dev/bus/usb", register_device);
|
|
|
|
kick_disconnected_devices();
|
|
|
|
sleep(1);
|
|
|
|
}
|
2015-05-07 01:48:52 +02:00
|
|
|
return nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 00:20:09 +02:00
|
|
|
void usb_init() {
|
|
|
|
struct sigaction actions;
|
2009-03-04 04:32:55 +01:00
|
|
|
memset(&actions, 0, sizeof(actions));
|
|
|
|
sigemptyset(&actions.sa_mask);
|
|
|
|
actions.sa_flags = 0;
|
2015-07-24 00:20:09 +02:00
|
|
|
actions.sa_handler = [](int) {};
|
|
|
|
sigaction(SIGALRM, &actions, nullptr);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-05 22:41:21 +02:00
|
|
|
if (!adb_thread_create(device_poll_thread, nullptr)) {
|
2015-08-31 20:50:24 +02:00
|
|
|
fatal_errno("cannot create device_poll thread");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|