2009-03-04 11:32:55 +08: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define TRACE_TAG TRACE_USB
|
|
|
|
#include "adb.h"
|
2009-05-21 08:33:53 +08:00
|
|
|
#include "usb_vendors.h"
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
#define DBG D
|
|
|
|
|
|
|
|
static IONotificationPortRef notificationPort = 0;
|
2009-05-21 08:33:53 +08:00
|
|
|
static io_iterator_t* notificationIterators;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
struct usb_handle
|
|
|
|
{
|
|
|
|
UInt8 bulkIn;
|
|
|
|
UInt8 bulkOut;
|
|
|
|
IOUSBInterfaceInterface **interface;
|
|
|
|
io_object_t usbNotification;
|
|
|
|
unsigned int zero_mask;
|
|
|
|
};
|
|
|
|
|
|
|
|
static CFRunLoopRef currentRunLoop = 0;
|
|
|
|
static pthread_mutex_t start_lock;
|
|
|
|
static pthread_cond_t start_cond;
|
|
|
|
|
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
static void AndroidInterfaceAdded(void *refCon, io_iterator_t iterator);
|
|
|
|
static void AndroidInterfaceNotify(void *refCon, io_iterator_t iterator,
|
|
|
|
natural_t messageType,
|
|
|
|
void *messageArgument);
|
|
|
|
static usb_handle* CheckInterface(IOUSBInterfaceInterface **iface,
|
|
|
|
UInt16 vendor, UInt16 product);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
InitUSB()
|
|
|
|
{
|
|
|
|
CFMutableDictionaryRef matchingDict;
|
|
|
|
CFRunLoopSourceRef runLoopSource;
|
2009-05-09 09:25:58 +08:00
|
|
|
SInt32 vendor, if_subclass, if_protocol;
|
|
|
|
unsigned i;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
//* To set up asynchronous notifications, create a notification port and
|
|
|
|
//* add its run loop event source to the program's run loop
|
|
|
|
notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
|
|
|
|
runLoopSource = IONotificationPortGetRunLoopSource(notificationPort);
|
|
|
|
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
|
|
|
|
|
|
|
|
memset(notificationIterators, 0, sizeof(notificationIterators));
|
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
//* loop through all supported vendors
|
2009-05-21 08:33:53 +08:00
|
|
|
for (i = 0; i < vendorIdCount; i++) {
|
2009-05-09 09:25:58 +08:00
|
|
|
//* Create our matching dictionary to find the Android device's
|
|
|
|
//* adb interface
|
|
|
|
//* IOServiceAddMatchingNotification consumes the reference, so we do
|
|
|
|
//* not need to release this
|
|
|
|
matchingDict = IOServiceMatching(kIOUSBInterfaceClassName);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
if (!matchingDict) {
|
|
|
|
DBG("ERR: Couldn't create USB matching dictionary.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
//* Match based on vendor id, interface subclass and protocol
|
|
|
|
vendor = vendorIds[i];
|
|
|
|
if_subclass = ADB_SUBCLASS;
|
|
|
|
if_protocol = ADB_PROTOCOL;
|
|
|
|
CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorID),
|
|
|
|
CFNumberCreate(kCFAllocatorDefault,
|
|
|
|
kCFNumberSInt32Type, &vendor));
|
|
|
|
CFDictionarySetValue(matchingDict, CFSTR(kUSBInterfaceSubClass),
|
|
|
|
CFNumberCreate(kCFAllocatorDefault,
|
|
|
|
kCFNumberSInt32Type, &if_subclass));
|
|
|
|
CFDictionarySetValue(matchingDict, CFSTR(kUSBInterfaceProtocol),
|
|
|
|
CFNumberCreate(kCFAllocatorDefault,
|
|
|
|
kCFNumberSInt32Type, &if_protocol));
|
2009-03-04 11:32:55 +08:00
|
|
|
IOServiceAddMatchingNotification(
|
|
|
|
notificationPort,
|
|
|
|
kIOFirstMatchNotification,
|
|
|
|
matchingDict,
|
2009-05-09 09:25:58 +08:00
|
|
|
AndroidInterfaceAdded,
|
2009-03-04 11:32:55 +08:00
|
|
|
NULL,
|
|
|
|
¬ificationIterators[i]);
|
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
//* Iterate over set of matching interfaces to access already-present
|
|
|
|
//* devices and to arm the notification
|
|
|
|
AndroidInterfaceAdded(NULL, notificationIterators[i]);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-09 09:25:58 +08:00
|
|
|
AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
|
|
|
kern_return_t kr;
|
|
|
|
io_service_t usbDevice;
|
2009-05-09 09:25:58 +08:00
|
|
|
io_service_t usbInterface;
|
2009-03-04 11:32:55 +08:00
|
|
|
IOCFPlugInInterface **plugInInterface = NULL;
|
2009-05-09 09:25:58 +08:00
|
|
|
IOUSBInterfaceInterface220 **iface = NULL;
|
|
|
|
IOUSBDeviceInterface197 **dev = NULL;
|
2009-03-04 11:32:55 +08:00
|
|
|
HRESULT result;
|
|
|
|
SInt32 score;
|
|
|
|
UInt16 vendor;
|
|
|
|
UInt16 product;
|
|
|
|
UInt8 serialIndex;
|
|
|
|
char serial[256];
|
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
while ((usbInterface = IOIteratorNext(iterator))) {
|
|
|
|
//* Create an intermediate interface plugin
|
|
|
|
kr = IOCreatePlugInInterfaceForService(usbInterface,
|
|
|
|
kIOUSBInterfaceUserClientTypeID,
|
|
|
|
kIOCFPlugInInterfaceID,
|
|
|
|
&plugInInterface, &score);
|
|
|
|
IOObjectRelease(usbInterface);
|
|
|
|
if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
|
|
|
|
DBG("ERR: Unable to create an interface plug-in (%08x)\n", kr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//* This gets us the interface object
|
|
|
|
result = (*plugInInterface)->QueryInterface(plugInInterface,
|
|
|
|
CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID)
|
|
|
|
&iface);
|
|
|
|
//* We only needed the plugin to get the interface, so discard it
|
|
|
|
(*plugInInterface)->Release(plugInInterface);
|
|
|
|
if (result || !iface) {
|
|
|
|
DBG("ERR: Couldn't query the interface (%08x)\n", (int) result);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//* 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) {
|
|
|
|
DBG("ERR: Couldn't grab device from interface (%08x)\n", kr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugInInterface = NULL;
|
|
|
|
score = 0;
|
|
|
|
//* create an intermediate device plugin
|
2009-03-04 11:32:55 +08:00
|
|
|
kr = IOCreatePlugInInterfaceForService(usbDevice,
|
|
|
|
kIOUSBDeviceUserClientTypeID,
|
|
|
|
kIOCFPlugInInterfaceID,
|
|
|
|
&plugInInterface, &score);
|
2009-05-09 09:25:58 +08:00
|
|
|
//* only needed this to find the plugin
|
|
|
|
(void)IOObjectRelease(usbDevice);
|
2009-03-04 11:32:55 +08:00
|
|
|
if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
|
2009-05-09 09:25:58 +08:00
|
|
|
DBG("ERR: Unable to create a device plug-in (%08x)\n", kr);
|
|
|
|
continue;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
result = (*plugInInterface)->QueryInterface(plugInInterface,
|
|
|
|
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID) &dev);
|
2009-05-09 09:25:58 +08:00
|
|
|
//* only needed this to query the plugin
|
|
|
|
(*plugInInterface)->Release(plugInInterface);
|
2009-03-04 11:32:55 +08:00
|
|
|
if (result || !dev) {
|
2009-05-09 09:25:58 +08:00
|
|
|
DBG("ERR: Couldn't create a device interface (%08x)\n",
|
|
|
|
(int) result);
|
|
|
|
continue;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
//* Now after all that, we actually have a ref to the device and
|
|
|
|
//* the interface that matched our criteria
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
kr = (*dev)->GetDeviceVendor(dev, &vendor);
|
|
|
|
kr = (*dev)->GetDeviceProduct(dev, &product);
|
|
|
|
kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
|
|
|
|
|
2009-08-07 08:21:52 +08: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 09:25:58 +08:00
|
|
|
(*dev)->Release(dev);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
DBG("INFO: Found vid=%04x pid=%04x serial=%s\n", vendor, product,
|
|
|
|
serial);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
usb_handle* handle = CheckInterface((IOUSBInterfaceInterface**)iface,
|
|
|
|
vendor, product);
|
2009-03-04 11:32:55 +08:00
|
|
|
if (handle == NULL) {
|
|
|
|
DBG("ERR: Could not find device interface: %08x\n", kr);
|
2009-05-09 09:25:58 +08:00
|
|
|
(*iface)->Release(iface);
|
|
|
|
continue;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DBG("AndroidDeviceAdded calling register_usb_transport\n");
|
2009-08-09 01:16:54 +08:00
|
|
|
register_usb_transport(handle, (serial[0] ? serial : NULL), 1);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
// Register for an interest notification of this device being removed.
|
|
|
|
// Pass the reference to our private data as the refCon for the
|
|
|
|
// notification.
|
2009-03-04 11:32:55 +08:00
|
|
|
kr = IOServiceAddInterestNotification(notificationPort,
|
2009-05-09 09:25:58 +08:00
|
|
|
usbInterface,
|
2009-03-04 11:32:55 +08:00
|
|
|
kIOGeneralInterest,
|
2009-05-09 09:25:58 +08:00
|
|
|
AndroidInterfaceNotify,
|
2009-03-04 11:32:55 +08:00
|
|
|
handle,
|
|
|
|
&handle->usbNotification);
|
2009-05-09 09:25:58 +08:00
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
if (kIOReturnSuccess != kr) {
|
|
|
|
DBG("ERR: Unable to create interest notification (%08x)\n", kr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-09 09:25:58 +08:00
|
|
|
AndroidInterfaceNotify(void *refCon, io_service_t service, natural_t messageType, void *messageArgument)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
|
|
|
usb_handle *handle = (usb_handle *)refCon;
|
|
|
|
|
|
|
|
if (messageType == kIOMessageServiceIsTerminated) {
|
2009-05-09 09:25:58 +08:00
|
|
|
if (!handle) {
|
|
|
|
DBG("ERR: NULL handle\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DBG("AndroidInterfaceNotify\n");
|
2009-03-04 11:32:55 +08:00
|
|
|
IOObjectRelease(handle->usbNotification);
|
|
|
|
usb_kick(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
//* TODO: simplify this further since we only register to get ADB interface
|
|
|
|
//* subclass+protocol events
|
2009-03-04 11:32:55 +08:00
|
|
|
static usb_handle*
|
2009-05-09 09:25:58 +08:00
|
|
|
CheckInterface(IOUSBInterfaceInterface **interface, UInt16 vendor, UInt16 product)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
|
|
|
usb_handle* handle = NULL;
|
|
|
|
IOReturn kr;
|
|
|
|
UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol;
|
2009-05-09 09:25:58 +08:00
|
|
|
UInt8 endpoint;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
|
2009-05-09 09:25:58 +08: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 11:32:55 +08:00
|
|
|
if (kr != kIOReturnSuccess) {
|
2009-05-09 09:25:58 +08:00
|
|
|
DBG("ERR: Could not open interface: (%08x)\n", kr);
|
2009-03-04 11:32:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
//* Get the number of endpoints associated with this interface
|
|
|
|
kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
|
|
|
|
if (kr != kIOReturnSuccess) {
|
|
|
|
DBG("ERR: Unable to get number of endpoints: (%08x)\n", kr);
|
|
|
|
goto err_get_num_ep;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
//* Get interface class, subclass and protocol
|
|
|
|
if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess ||
|
2009-03-04 11:32:55 +08:00
|
|
|
(*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess ||
|
2009-05-09 09:25:58 +08:00
|
|
|
(*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess) {
|
2009-03-04 11:32:55 +08:00
|
|
|
DBG("ERR: Unable to get interface class, subclass and protocol\n");
|
2009-05-09 09:25:58 +08:00
|
|
|
goto err_get_interface_class;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
//* check to make sure interface class, subclass and protocol match ADB
|
|
|
|
//* avoid opening mass storage endpoints
|
|
|
|
if (!is_adb_interface(vendor, product, interfaceClass,
|
|
|
|
interfaceSubClass, interfaceProtocol))
|
|
|
|
goto err_bad_adb_interface;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
handle = calloc(1, sizeof(usb_handle));
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
//* Iterate over the endpoints for this interface and find the first
|
|
|
|
//* bulk in/out pipes available. These will be our read/write pipes.
|
|
|
|
for (endpoint = 0; endpoint <= interfaceNumEndpoints; endpoint++) {
|
|
|
|
UInt8 transferType;
|
|
|
|
UInt16 maxPacketSize;
|
|
|
|
UInt8 interval;
|
|
|
|
UInt8 number;
|
|
|
|
UInt8 direction;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
kr = (*interface)->GetPipeProperties(interface, endpoint, &direction,
|
|
|
|
&number, &transferType, &maxPacketSize, &interval);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
if (kIOReturnSuccess == kr) {
|
|
|
|
if (kUSBBulk != transferType)
|
|
|
|
continue;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
if (kUSBIn == direction)
|
|
|
|
handle->bulkIn = endpoint;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
if (kUSBOut == direction)
|
|
|
|
handle->bulkOut = endpoint;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
handle->zero_mask = maxPacketSize - 1;
|
|
|
|
} else {
|
|
|
|
DBG("ERR: FindDeviceInterface - could not get pipe properties\n");
|
|
|
|
goto err_get_pipe_props;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-09 09:25:58 +08:00
|
|
|
handle->interface = interface;
|
2009-03-04 11:32:55 +08:00
|
|
|
return handle;
|
2009-05-09 09:25:58 +08:00
|
|
|
|
|
|
|
err_get_pipe_props:
|
|
|
|
free(handle);
|
|
|
|
err_bad_adb_interface:
|
|
|
|
err_get_interface_class:
|
|
|
|
err_get_num_ep:
|
|
|
|
(*interface)->USBInterfaceClose(interface);
|
|
|
|
return NULL;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RunLoopThread(void* unused)
|
|
|
|
{
|
2009-05-09 09:25:58 +08:00
|
|
|
unsigned i;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
InitUSB();
|
|
|
|
|
|
|
|
currentRunLoop = CFRunLoopGetCurrent();
|
|
|
|
|
|
|
|
// Signal the parent that we are running
|
|
|
|
adb_mutex_lock(&start_lock);
|
|
|
|
adb_cond_signal(&start_cond);
|
|
|
|
adb_mutex_unlock(&start_lock);
|
|
|
|
|
|
|
|
CFRunLoopRun();
|
|
|
|
currentRunLoop = 0;
|
|
|
|
|
2009-05-21 08:33:53 +08:00
|
|
|
for (i = 0; i < vendorIdCount; i++) {
|
2009-03-04 11:32:55 +08:00
|
|
|
IOObjectRelease(notificationIterators[i]);
|
|
|
|
}
|
|
|
|
IONotificationPortDestroy(notificationPort);
|
|
|
|
|
|
|
|
DBG("RunLoopThread done\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int initialized = 0;
|
|
|
|
void usb_init()
|
|
|
|
{
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
adb_thread_t tid;
|
|
|
|
|
2009-05-21 08:33:53 +08:00
|
|
|
notificationIterators = (io_iterator_t*)malloc(
|
|
|
|
vendorIdCount * sizeof(io_iterator_t));
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
adb_mutex_init(&start_lock, NULL);
|
|
|
|
adb_cond_init(&start_cond, NULL);
|
|
|
|
|
|
|
|
if(adb_thread_create(&tid, RunLoopThread, NULL))
|
|
|
|
fatal_errno("cannot create input thread");
|
|
|
|
|
|
|
|
// Wait for initialization to finish
|
|
|
|
adb_mutex_lock(&start_lock);
|
|
|
|
adb_cond_wait(&start_cond, &start_lock);
|
|
|
|
adb_mutex_unlock(&start_lock);
|
|
|
|
|
|
|
|
adb_mutex_destroy(&start_lock);
|
|
|
|
adb_cond_destroy(&start_cond);
|
|
|
|
|
|
|
|
initialized = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_cleanup()
|
|
|
|
{
|
|
|
|
DBG("usb_cleanup\n");
|
|
|
|
close_usb_devices();
|
|
|
|
if (currentRunLoop)
|
|
|
|
CFRunLoopStop(currentRunLoop);
|
2009-05-21 08:33:53 +08:00
|
|
|
|
|
|
|
if (notificationIterators != NULL) {
|
|
|
|
free(notificationIterators);
|
|
|
|
notificationIterators = NULL;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int usb_write(usb_handle *handle, const void *buf, int len)
|
|
|
|
{
|
|
|
|
IOReturn result;
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!handle)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (NULL == handle->interface) {
|
|
|
|
DBG("ERR: usb_write interface was null\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == handle->bulkOut) {
|
|
|
|
DBG("ERR: bulkOut endpoint not assigned\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
result =
|
|
|
|
(*handle->interface)->WritePipe(
|
|
|
|
handle->interface, handle->bulkOut, (void *)buf, len);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
DBG("ERR: usb_write failed with status %d\n", result);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_read(usb_handle *handle, void *buf, int len)
|
|
|
|
{
|
|
|
|
IOReturn result;
|
|
|
|
UInt32 numBytes = len;
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handle) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == handle->interface) {
|
|
|
|
DBG("ERR: usb_read interface was null\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == handle->bulkIn) {
|
|
|
|
DBG("ERR: bulkIn endpoint not assigned\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
result =
|
|
|
|
(*handle->interface)->ReadPipe(handle->interface,
|
|
|
|
handle->bulkIn, buf, &numBytes);
|
|
|
|
|
|
|
|
if (0 == result)
|
|
|
|
return 0;
|
|
|
|
else {
|
|
|
|
DBG("ERR: usb_read failed with status %d\n", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_close(usb_handle *handle)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_kick(usb_handle *handle)
|
|
|
|
{
|
|
|
|
/* release the interface */
|
2009-05-09 09:25:58 +08:00
|
|
|
if (!handle)
|
|
|
|
return;
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
if (handle->interface)
|
|
|
|
{
|
|
|
|
(*handle->interface)->USBInterfaceClose(handle->interface);
|
|
|
|
(*handle->interface)->Release(handle->interface);
|
|
|
|
handle->interface = 0;
|
|
|
|
}
|
|
|
|
}
|