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"
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
|
|
|
|
#include <IOKit/IOKitLib.h>
|
|
|
|
#include <IOKit/IOCFPlugIn.h>
|
|
|
|
#include <IOKit/usb/IOUSBLib.h>
|
|
|
|
#include <IOKit/IOMessage.h>
|
|
|
|
#include <mach/mach_port.h>
|
|
|
|
|
2015-04-17 04:20:40 +02:00
|
|
|
#include <inttypes.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
2015-08-19 02:53:16 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "adb.h"
|
2015-02-25 19:26:17 +01:00
|
|
|
#include "transport.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
struct usb_handle
|
|
|
|
{
|
2015-08-13 17:48:06 +02:00
|
|
|
UInt8 bulkIn;
|
|
|
|
UInt8 bulkOut;
|
|
|
|
IOUSBInterfaceInterface190** interface;
|
|
|
|
unsigned int zero_mask;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
// For garbage collecting disconnected devices.
|
|
|
|
bool mark;
|
|
|
|
std::string devpath;
|
|
|
|
std::atomic<bool> dead;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
usb_handle() : bulkIn(0), bulkOut(0), interface(nullptr),
|
|
|
|
zero_mask(0), mark(false), dead(false) {
|
|
|
|
}
|
|
|
|
};
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
static std::atomic<bool> usb_inited_flag;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
static auto& g_usb_handles_mutex = *new std::mutex();
|
|
|
|
static auto& g_usb_handles = *new std::vector<std::unique_ptr<usb_handle>>();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
static bool IsKnownDevice(const std::string& devpath) {
|
|
|
|
std::lock_guard<std::mutex> lock_guard(g_usb_handles_mutex);
|
|
|
|
for (auto& usb : g_usb_handles) {
|
|
|
|
if (usb->devpath == devpath) {
|
|
|
|
// Set mark flag to indicate this device is still alive.
|
|
|
|
usb->mark = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
static void usb_kick_locked(usb_handle* handle);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
static void KickDisconnectedDevices() {
|
|
|
|
std::lock_guard<std::mutex> lock_guard(g_usb_handles_mutex);
|
|
|
|
for (auto& usb : g_usb_handles) {
|
|
|
|
if (!usb->mark) {
|
|
|
|
usb_kick_locked(usb.get());
|
|
|
|
} else {
|
|
|
|
usb->mark = false;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-03-25 21:55:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AddDevice(std::unique_ptr<usb_handle> handle) {
|
|
|
|
handle->mark = true;
|
|
|
|
std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
|
|
|
|
g_usb_handles.push_back(std::move(handle));
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
static void AndroidInterfaceAdded(io_iterator_t iterator);
|
|
|
|
static std::unique_ptr<usb_handle> CheckInterface(IOUSBInterfaceInterface190 **iface,
|
|
|
|
UInt16 vendor, UInt16 product);
|
2014-11-21 13:21:12 +01:00
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
static bool FindUSBDevices() {
|
|
|
|
// Create the matching dictionary to find the Android device's adb interface.
|
|
|
|
CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBInterfaceClassName);
|
|
|
|
if (!matchingDict) {
|
|
|
|
LOG(ERROR) << "couldn't create USB matching dictionary";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Create an iterator for all I/O Registry objects that match the dictionary.
|
|
|
|
io_iterator_t iter = 0;
|
|
|
|
kern_return_t kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
|
|
|
|
if (kr != KERN_SUCCESS) {
|
|
|
|
LOG(ERROR) << "failed to get matching services";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Iterate over all matching objects.
|
|
|
|
AndroidInterfaceAdded(iter);
|
|
|
|
IOObjectRelease(iter);
|
|
|
|
return true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-03-25 21:55:07 +01:00
|
|
|
AndroidInterfaceAdded(io_iterator_t iterator)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
kern_return_t kr;
|
|
|
|
io_service_t usbDevice;
|
2009-05-09 03:25:58 +02:00
|
|
|
io_service_t usbInterface;
|
2009-03-04 04:32:55 +01:00
|
|
|
IOCFPlugInInterface **plugInInterface = NULL;
|
2009-05-09 03:25:58 +02:00
|
|
|
IOUSBInterfaceInterface220 **iface = NULL;
|
|
|
|
IOUSBDeviceInterface197 **dev = NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
HRESULT result;
|
|
|
|
SInt32 score;
|
2015-08-26 02:48:12 +02:00
|
|
|
uint32_t locationId;
|
2015-04-17 04:20:40 +02:00
|
|
|
UInt8 if_class, subclass, protocol;
|
2009-03-04 04:32:55 +01:00
|
|
|
UInt16 vendor;
|
|
|
|
UInt16 product;
|
|
|
|
UInt8 serialIndex;
|
|
|
|
char serial[256];
|
2016-03-25 21:55:07 +01:00
|
|
|
std::string devpath;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2009-05-09 03:25:58 +02:00
|
|
|
while ((usbInterface = IOIteratorNext(iterator))) {
|
|
|
|
//* Create an intermediate interface plugin
|
|
|
|
kr = IOCreatePlugInInterfaceForService(usbInterface,
|
|
|
|
kIOUSBInterfaceUserClientTypeID,
|
|
|
|
kIOCFPlugInInterfaceID,
|
|
|
|
&plugInInterface, &score);
|
|
|
|
IOObjectRelease(usbInterface);
|
|
|
|
if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "Unable to create an interface plug-in (" << std::hex << kr << ")";
|
2009-05-09 03:25:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//* This gets us the interface object
|
2015-04-17 04:20:40 +02:00
|
|
|
result = (*plugInInterface)->QueryInterface(
|
|
|
|
plugInInterface,
|
|
|
|
CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID*)&iface);
|
2009-05-09 03:25:58 +02:00
|
|
|
//* We only needed the plugin to get the interface, so discard it
|
|
|
|
(*plugInInterface)->Release(plugInInterface);
|
|
|
|
if (result || !iface) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "Couldn't query the interface (" << std::hex << result << ")";
|
2009-05-09 03:25:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-04-17 04:20:40 +02:00
|
|
|
kr = (*iface)->GetInterfaceClass(iface, &if_class);
|
2014-11-21 13:21:12 +01:00
|
|
|
kr = (*iface)->GetInterfaceSubClass(iface, &subclass);
|
|
|
|
kr = (*iface)->GetInterfaceProtocol(iface, &protocol);
|
2015-04-17 04:20:40 +02:00
|
|
|
if(if_class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) {
|
2014-11-21 13:21:12 +01:00
|
|
|
// Ignore non-ADB devices.
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(DEBUG) << "Ignoring interface with incorrect class/subclass/protocol - " << if_class
|
|
|
|
<< ", " << subclass << ", " << protocol;
|
2014-11-21 13:21:12 +01:00
|
|
|
(*iface)->Release(iface);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-05-09 03:25:58 +02:00
|
|
|
//* this gets us an ioservice, with which we will find the actual
|
|
|
|
//* device; after getting a plugin, and querying the interface, of
|
|
|
|
//* course.
|
|
|
|
//* Gotta love OS X
|
|
|
|
kr = (*iface)->GetDevice(iface, &usbDevice);
|
|
|
|
if (kIOReturnSuccess != kr || !usbDevice) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "Couldn't grab device from interface (" << std::hex << kr << ")";
|
2009-05-09 03:25:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugInInterface = NULL;
|
|
|
|
score = 0;
|
|
|
|
//* create an intermediate device plugin
|
2009-03-04 04:32:55 +01:00
|
|
|
kr = IOCreatePlugInInterfaceForService(usbDevice,
|
|
|
|
kIOUSBDeviceUserClientTypeID,
|
|
|
|
kIOCFPlugInInterfaceID,
|
|
|
|
&plugInInterface, &score);
|
2009-05-09 03:25:58 +02:00
|
|
|
//* only needed this to find the plugin
|
|
|
|
(void)IOObjectRelease(usbDevice);
|
2009-03-04 04:32:55 +01:00
|
|
|
if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "Unable to create a device plug-in (" << std::hex << kr << ")";
|
2009-05-09 03:25:58 +02:00
|
|
|
continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
result = (*plugInInterface)->QueryInterface(plugInInterface,
|
2015-04-17 04:20:40 +02:00
|
|
|
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID*)&dev);
|
2009-05-09 03:25:58 +02:00
|
|
|
//* only needed this to query the plugin
|
|
|
|
(*plugInInterface)->Release(plugInInterface);
|
2009-03-04 04:32:55 +01:00
|
|
|
if (result || !dev) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "Couldn't create a device interface (" << std::hex << result << ")";
|
2009-05-09 03:25:58 +02:00
|
|
|
continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2009-05-09 03:25:58 +02:00
|
|
|
//* Now after all that, we actually have a ref to the device and
|
|
|
|
//* the interface that matched our criteria
|
2009-03-04 04:32:55 +01:00
|
|
|
kr = (*dev)->GetDeviceVendor(dev, &vendor);
|
|
|
|
kr = (*dev)->GetDeviceProduct(dev, &product);
|
2012-04-20 20:21:14 +02:00
|
|
|
kr = (*dev)->GetLocationID(dev, &locationId);
|
2016-03-25 21:55:07 +01:00
|
|
|
if (kr == KERN_SUCCESS) {
|
|
|
|
devpath = android::base::StringPrintf("usb:%" PRIu32 "X", locationId);
|
|
|
|
if (IsKnownDevice(devpath)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-20 20:21:14 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
|
|
|
|
|
2015-08-19 02:53:16 +02:00
|
|
|
if (serialIndex > 0) {
|
|
|
|
IOUSBDevRequest req;
|
|
|
|
UInt16 buffer[256];
|
|
|
|
UInt16 languages[128];
|
|
|
|
|
|
|
|
memset(languages, 0, sizeof(languages));
|
|
|
|
|
|
|
|
req.bmRequestType =
|
|
|
|
USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
|
|
|
|
req.bRequest = kUSBRqGetDescriptor;
|
|
|
|
req.wValue = (kUSBStringDesc << 8) | 0;
|
|
|
|
req.wIndex = 0;
|
|
|
|
req.pData = languages;
|
|
|
|
req.wLength = sizeof(languages);
|
|
|
|
kr = (*dev)->DeviceRequest(dev, &req);
|
|
|
|
|
|
|
|
if (kr == kIOReturnSuccess && req.wLenDone > 0) {
|
|
|
|
|
|
|
|
int langCount = (req.wLenDone - 2) / 2, lang;
|
|
|
|
|
|
|
|
for (lang = 1; lang <= langCount; lang++) {
|
|
|
|
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
req.bmRequestType =
|
|
|
|
USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
|
|
|
|
req.bRequest = kUSBRqGetDescriptor;
|
|
|
|
req.wValue = (kUSBStringDesc << 8) | serialIndex;
|
|
|
|
req.wIndex = languages[lang];
|
|
|
|
req.pData = buffer;
|
|
|
|
req.wLength = sizeof(buffer);
|
|
|
|
kr = (*dev)->DeviceRequest(dev, &req);
|
|
|
|
|
|
|
|
if (kr == kIOReturnSuccess && req.wLenDone > 0) {
|
|
|
|
int i, count;
|
|
|
|
|
|
|
|
// skip first word, and copy the rest to the serial string,
|
|
|
|
// changing shorts to bytes.
|
|
|
|
count = (req.wLenDone - 1) / 2;
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
serial[i] = buffer[i + 1];
|
|
|
|
serial[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-09 03:25:58 +02:00
|
|
|
(*dev)->Release(dev);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
VLOG(USB) << android::base::StringPrintf("Found vid=%04x pid=%04x serial=%s\n",
|
2015-08-19 02:53:16 +02:00
|
|
|
vendor, product, serial);
|
2016-03-25 21:55:07 +01:00
|
|
|
if (devpath.empty()) {
|
|
|
|
devpath = serial;
|
|
|
|
}
|
|
|
|
if (IsKnownDevice(devpath)) {
|
|
|
|
(*iface)->USBInterfaceClose(iface);
|
2009-05-09 03:25:58 +02:00
|
|
|
(*iface)->Release(iface);
|
|
|
|
continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
std::unique_ptr<usb_handle> handle = CheckInterface((IOUSBInterfaceInterface190**)iface,
|
|
|
|
vendor, product);
|
|
|
|
if (handle == nullptr) {
|
|
|
|
LOG(ERROR) << "Could not find device interface";
|
|
|
|
(*iface)->Release(iface);
|
|
|
|
continue;
|
2009-05-09 03:25:58 +02:00
|
|
|
}
|
2016-03-25 21:55:07 +01:00
|
|
|
handle->devpath = devpath;
|
|
|
|
usb_handle* handle_p = handle.get();
|
|
|
|
VLOG(USB) << "Add usb device " << serial;
|
|
|
|
AddDevice(std::move(handle));
|
|
|
|
register_usb_transport(handle_p, serial, devpath.c_str(), 1);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 17:48:06 +02:00
|
|
|
// Used to clear both the endpoints before starting.
|
|
|
|
// When adb quits, we might clear the host endpoint but not the device.
|
|
|
|
// So we make sure both sides are clear before starting up.
|
|
|
|
static bool ClearPipeStallBothEnds(IOUSBInterfaceInterface190** interface, UInt8 bulkEp) {
|
|
|
|
IOReturn rc = (*interface)->ClearPipeStallBothEnds(interface, bulkEp);
|
|
|
|
if (rc != kIOReturnSuccess) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "Could not clear pipe stall both ends: " << std::hex << rc;
|
2015-08-13 17:48:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-09 03:25:58 +02:00
|
|
|
//* TODO: simplify this further since we only register to get ADB interface
|
|
|
|
//* subclass+protocol events
|
2016-03-25 21:55:07 +01:00
|
|
|
static std::unique_ptr<usb_handle>
|
2015-08-13 17:48:06 +02:00
|
|
|
CheckInterface(IOUSBInterfaceInterface190 **interface, UInt16 vendor, UInt16 product)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2016-03-25 21:55:07 +01:00
|
|
|
std::unique_ptr<usb_handle> handle;
|
2015-08-14 00:01:18 +02:00
|
|
|
IOReturn kr;
|
|
|
|
UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol;
|
|
|
|
UInt8 endpoint;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2009-05-09 03:25:58 +02:00
|
|
|
//* Now open the interface. This will cause the pipes associated with
|
|
|
|
//* the endpoints in the interface descriptor to be instantiated
|
|
|
|
kr = (*interface)->USBInterfaceOpen(interface);
|
2009-03-04 04:32:55 +01:00
|
|
|
if (kr != kIOReturnSuccess) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "Could not open interface: " << std::hex << kr;
|
2009-03-04 04:32:55 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-09 03:25:58 +02:00
|
|
|
//* Get the number of endpoints associated with this interface
|
|
|
|
kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
|
|
|
|
if (kr != kIOReturnSuccess) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "Unable to get number of endpoints: " << std::hex << kr;
|
2009-05-09 03:25:58 +02:00
|
|
|
goto err_get_num_ep;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2009-05-09 03:25:58 +02:00
|
|
|
//* Get interface class, subclass and protocol
|
|
|
|
if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess ||
|
2009-03-04 04:32:55 +01:00
|
|
|
(*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess ||
|
2009-05-09 03:25:58 +02:00
|
|
|
(*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "Unable to get interface class, subclass and protocol";
|
2009-05-09 03:25:58 +02:00
|
|
|
goto err_get_interface_class;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2009-05-09 03:25:58 +02:00
|
|
|
//* check to make sure interface class, subclass and protocol match ADB
|
|
|
|
//* avoid opening mass storage endpoints
|
2015-08-13 17:48:06 +02:00
|
|
|
if (!is_adb_interface(vendor, product, interfaceClass, interfaceSubClass, interfaceProtocol)) {
|
2009-05-09 03:25:58 +02:00
|
|
|
goto err_bad_adb_interface;
|
2015-08-13 17:48:06 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
handle.reset(new usb_handle);
|
|
|
|
if (handle == nullptr) {
|
|
|
|
goto err_bad_adb_interface;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2009-05-09 03:25:58 +02:00
|
|
|
//* Iterate over the endpoints for this interface and find the first
|
|
|
|
//* bulk in/out pipes available. These will be our read/write pipes.
|
2015-08-14 00:01:18 +02:00
|
|
|
for (endpoint = 1; endpoint <= interfaceNumEndpoints; endpoint++) {
|
2009-05-09 03:25:58 +02:00
|
|
|
UInt8 transferType;
|
|
|
|
UInt16 maxPacketSize;
|
|
|
|
UInt8 interval;
|
|
|
|
UInt8 number;
|
|
|
|
UInt8 direction;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2009-05-09 03:25:58 +02:00
|
|
|
kr = (*interface)->GetPipeProperties(interface, endpoint, &direction,
|
|
|
|
&number, &transferType, &maxPacketSize, &interval);
|
2015-08-13 17:48:06 +02:00
|
|
|
if (kr != kIOReturnSuccess) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "FindDeviceInterface - could not get pipe properties: "
|
|
|
|
<< std::hex << kr;
|
2015-08-13 17:48:06 +02:00
|
|
|
goto err_get_pipe_props;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-08-13 17:48:06 +02:00
|
|
|
if (kUSBBulk != transferType) continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-08-13 17:48:06 +02:00
|
|
|
if (kUSBIn == direction) {
|
|
|
|
handle->bulkIn = endpoint;
|
|
|
|
if (!ClearPipeStallBothEnds(interface, handle->bulkIn)) goto err_get_pipe_props;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-08-13 17:48:06 +02:00
|
|
|
if (kUSBOut == direction) {
|
|
|
|
handle->bulkOut = endpoint;
|
|
|
|
if (!ClearPipeStallBothEnds(interface, handle->bulkOut)) goto err_get_pipe_props;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-08-13 17:48:06 +02:00
|
|
|
|
|
|
|
handle->zero_mask = maxPacketSize - 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2009-05-09 03:25:58 +02:00
|
|
|
handle->interface = interface;
|
2009-03-04 04:32:55 +01:00
|
|
|
return handle;
|
2009-05-09 03:25:58 +02:00
|
|
|
|
|
|
|
err_get_pipe_props:
|
|
|
|
err_bad_adb_interface:
|
|
|
|
err_get_interface_class:
|
|
|
|
err_get_num_ep:
|
|
|
|
(*interface)->USBInterfaceClose(interface);
|
2016-03-25 21:55:07 +01:00
|
|
|
return nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
std::mutex& operate_device_lock = *new std::mutex();
|
|
|
|
|
2016-02-12 23:31:15 +01:00
|
|
|
static void RunLoopThread(void* unused) {
|
2015-08-29 01:37:29 +02:00
|
|
|
adb_thread_setname("RunLoop");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
VLOG(USB) << "RunLoopThread started";
|
|
|
|
while (true) {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock_guard(operate_device_lock);
|
|
|
|
FindUSBDevices();
|
|
|
|
KickDisconnectedDevices();
|
|
|
|
}
|
|
|
|
// Signal the parent that we are running
|
|
|
|
usb_inited_flag = true;
|
|
|
|
adb_sleep_ms(1000);
|
|
|
|
}
|
|
|
|
VLOG(USB) << "RunLoopThread done";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-09 01:13:53 +02:00
|
|
|
static void usb_cleanup() {
|
2016-03-25 21:55:07 +01:00
|
|
|
VLOG(USB) << "usb_cleanup";
|
|
|
|
// Wait until usb operations in RunLoopThread finish, and prevent further operations.
|
|
|
|
operate_device_lock.lock();
|
2015-05-09 01:13:53 +02:00
|
|
|
close_usb_devices();
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-05 22:41:21 +02:00
|
|
|
void usb_init() {
|
2015-05-09 01:13:53 +02:00
|
|
|
static bool initialized = false;
|
|
|
|
if (!initialized) {
|
|
|
|
atexit(usb_cleanup);
|
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
usb_inited_flag = false;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-05 22:41:21 +02:00
|
|
|
if (!adb_thread_create(RunLoopThread, nullptr)) {
|
2015-08-31 20:50:24 +02:00
|
|
|
fatal_errno("cannot create RunLoop thread");
|
2015-05-05 22:41:21 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
// Wait for initialization to finish
|
2016-03-25 21:55:07 +01:00
|
|
|
while (!usb_inited_flag) {
|
|
|
|
adb_sleep_ms(100);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-09 01:13:53 +02:00
|
|
|
initialized = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_write(usb_handle *handle, const void *buf, int len)
|
|
|
|
{
|
|
|
|
IOReturn result;
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return 0;
|
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
if (!handle || handle->dead)
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (NULL == handle->interface) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "usb_write interface was null";
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == handle->bulkOut) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "bulkOut endpoint not assigned";
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
result =
|
2015-08-19 02:53:16 +02:00
|
|
|
(*handle->interface)->WritePipe(handle->interface, handle->bulkOut, (void *)buf, len);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if ((result == 0) && (handle->zero_mask)) {
|
|
|
|
/* we need 0-markers and our transfer */
|
|
|
|
if(!(len & handle->zero_mask)) {
|
|
|
|
result =
|
|
|
|
(*handle->interface)->WritePipe(
|
|
|
|
handle->interface, handle->bulkOut, (void *)buf, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == result)
|
|
|
|
return 0;
|
|
|
|
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "usb_write failed with status: " << std::hex << result;
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_read(usb_handle *handle, void *buf, int len)
|
|
|
|
{
|
|
|
|
IOReturn result;
|
|
|
|
UInt32 numBytes = len;
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
if (!handle || handle->dead) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == handle->interface) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "usb_read interface was null";
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == handle->bulkIn) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "bulkIn endpoint not assigned";
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-09-11 20:02:17 +02:00
|
|
|
result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-09-11 20:02:17 +02:00
|
|
|
if (kIOUSBPipeStalled == result) {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "Pipe stalled, clearing stall.\n";
|
2014-09-11 20:02:17 +02:00
|
|
|
(*handle->interface)->ClearPipeStall(handle->interface, handle->bulkIn);
|
|
|
|
result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kIOReturnSuccess == result)
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
else {
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(ERROR) << "usb_read failed with status: " << std::hex << result;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_close(usb_handle *handle)
|
|
|
|
{
|
2016-03-25 21:55:07 +01:00
|
|
|
std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
|
|
|
|
for (auto it = g_usb_handles.begin(); it != g_usb_handles.end(); ++it) {
|
|
|
|
if ((*it).get() == handle) {
|
|
|
|
g_usb_handles.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
static void usb_kick_locked(usb_handle *handle)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-08-19 02:53:16 +02:00
|
|
|
LOG(INFO) << "Kicking handle";
|
2009-03-04 04:32:55 +01:00
|
|
|
/* release the interface */
|
2009-05-09 03:25:58 +02:00
|
|
|
if (!handle)
|
|
|
|
return;
|
|
|
|
|
2016-03-25 21:55:07 +01:00
|
|
|
if (!handle->dead)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2016-03-25 21:55:07 +01:00
|
|
|
handle->dead = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
(*handle->interface)->USBInterfaceClose(handle->interface);
|
|
|
|
(*handle->interface)->Release(handle->interface);
|
|
|
|
}
|
|
|
|
}
|
2016-03-25 21:55:07 +01:00
|
|
|
|
|
|
|
void usb_kick(usb_handle *handle) {
|
|
|
|
// Use the lock to avoid multiple thread kicking the device at the same time.
|
|
|
|
std::lock_guard<std::mutex> lock_guard(g_usb_handles_mutex);
|
|
|
|
usb_kick_locked(handle);
|
|
|
|
}
|