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-06-24 22:28:24 +02:00
|
|
|
#include <inttypes.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#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-10-30 19:22:01 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "usb.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal helper functions and associated definitions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if TRACE_USB
|
|
|
|
#define WARN(x...) fprintf(stderr, x)
|
|
|
|
#else
|
|
|
|
#define WARN(x...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ERR(x...) fprintf(stderr, "ERROR: " x)
|
|
|
|
|
|
|
|
/** An open usb device */
|
|
|
|
struct usb_handle
|
|
|
|
{
|
|
|
|
int success;
|
|
|
|
ifc_match_func callback;
|
|
|
|
usb_ifc_info info;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
UInt8 bulkIn;
|
|
|
|
UInt8 bulkOut;
|
2018-11-11 00:56:19 +01:00
|
|
|
IOUSBInterfaceInterface500** interface;
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned int zero_mask;
|
|
|
|
};
|
|
|
|
|
2018-07-26 17:56:09 +02:00
|
|
|
class OsxUsbTransport : public UsbTransport {
|
2015-10-30 19:22:01 +01:00
|
|
|
public:
|
2018-07-26 17:56:09 +02:00
|
|
|
// A timeout of 0 is blocking
|
|
|
|
OsxUsbTransport(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
|
|
|
~OsxUsbTransport() 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
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<usb_handle> handle_;
|
2018-07-26 17:56:09 +02:00
|
|
|
const uint32_t ms_timeout_;
|
2015-10-30 19:22:01 +01:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(OsxUsbTransport);
|
|
|
|
};
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/** Try out all the interfaces and see if there's a match. Returns 0 on
|
|
|
|
* success, -1 on failure. */
|
2018-11-11 00:56:19 +01:00
|
|
|
static int try_interfaces(IOUSBDeviceInterface500** dev, usb_handle* handle) {
|
2009-03-04 04:32:55 +01:00
|
|
|
IOReturn kr;
|
|
|
|
IOUSBFindInterfaceRequest request;
|
|
|
|
io_iterator_t iterator;
|
|
|
|
io_service_t usbInterface;
|
|
|
|
IOCFPlugInInterface **plugInInterface;
|
2018-11-11 00:56:19 +01:00
|
|
|
IOUSBInterfaceInterface500** interface = NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
HRESULT result;
|
|
|
|
SInt32 score;
|
|
|
|
UInt8 interfaceNumEndpoints;
|
|
|
|
|
2017-08-19 03:25:44 +02:00
|
|
|
request.bInterfaceClass = 0xff;
|
|
|
|
request.bInterfaceSubClass = 0x42;
|
|
|
|
request.bInterfaceProtocol = 0x03;
|
2009-03-04 04:32:55 +01:00
|
|
|
request.bAlternateSetting = kIOUSBFindInterfaceDontCare;
|
|
|
|
|
|
|
|
// Get an iterator for the interfaces on the device
|
|
|
|
kr = (*dev)->CreateInterfaceIterator(dev, &request, &iterator);
|
|
|
|
|
|
|
|
if (kr != 0) {
|
2019-05-10 20:28:38 +02:00
|
|
|
WARN("Couldn't create a device interface iterator: (%08x)\n", kr);
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((usbInterface = IOIteratorNext(iterator))) {
|
|
|
|
// Create an intermediate plugin
|
|
|
|
kr = IOCreatePlugInInterfaceForService(
|
|
|
|
usbInterface,
|
|
|
|
kIOUSBInterfaceUserClientTypeID,
|
|
|
|
kIOCFPlugInInterfaceID,
|
|
|
|
&plugInInterface,
|
|
|
|
&score);
|
|
|
|
|
|
|
|
// No longer need the usbInterface object now that we have the plugin
|
|
|
|
(void) IOObjectRelease(usbInterface);
|
|
|
|
|
|
|
|
if ((kr != 0) || (!plugInInterface)) {
|
|
|
|
WARN("Unable to create plugin (%08x)\n", kr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now create the interface interface for the interface
|
2018-11-11 00:56:19 +01:00
|
|
|
result = (*plugInInterface)
|
|
|
|
->QueryInterface(plugInInterface,
|
|
|
|
CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID500),
|
|
|
|
(LPVOID*)&interface);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
// No longer need the intermediate plugin
|
|
|
|
(*plugInInterface)->Release(plugInInterface);
|
|
|
|
|
|
|
|
if (result || !interface) {
|
|
|
|
ERR("Couldn't create interface interface: (%08x)\n",
|
|
|
|
(unsigned int) result);
|
|
|
|
// continue so we can try the next interface
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Now open the interface. This will cause the pipes
|
|
|
|
* associated with the endpoints in the interface descriptor
|
|
|
|
* to be instantiated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: Earlier comments here indicated that it was a bad
|
|
|
|
* idea to just open any interface, because opening "mass
|
|
|
|
* storage endpoints" is bad. However, the only way to find
|
|
|
|
* out if an interface does bulk in or out is to open it, and
|
|
|
|
* the framework in this application wants to be told about
|
|
|
|
* bulk in / out before deciding whether it actually wants to
|
|
|
|
* use the interface. Maybe something needs to be done about
|
|
|
|
* this situation.
|
|
|
|
*/
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
kr = (*interface)->USBInterfaceOpen(interface);
|
|
|
|
|
|
|
|
if (kr != 0) {
|
|
|
|
WARN("Could not open interface: (%08x)\n", kr);
|
|
|
|
(void) (*interface)->Release(interface);
|
|
|
|
// continue so we can try the next interface
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
// Get the number of endpoints associated with this interface.
|
|
|
|
kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
|
|
|
|
|
|
|
|
if (kr != 0) {
|
|
|
|
ERR("Unable to get number of endpoints: (%08x)\n", kr);
|
|
|
|
goto next_interface;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get interface class, subclass and protocol
|
|
|
|
if ((*interface)->GetInterfaceClass(interface, &handle->info.ifc_class) != 0 ||
|
|
|
|
(*interface)->GetInterfaceSubClass(interface, &handle->info.ifc_subclass) != 0 ||
|
|
|
|
(*interface)->GetInterfaceProtocol(interface, &handle->info.ifc_protocol) != 0)
|
|
|
|
{
|
|
|
|
ERR("Unable to get interface class, subclass and protocol\n");
|
|
|
|
goto next_interface;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle->info.has_bulk_in = 0;
|
|
|
|
handle->info.has_bulk_out = 0;
|
|
|
|
|
|
|
|
// Iterate over the endpoints for this interface and see if there
|
|
|
|
// are any that do bulk in/out.
|
2022-12-20 06:28:51 +01:00
|
|
|
for (UInt8 endpoint = 1; endpoint <= interfaceNumEndpoints; ++endpoint) {
|
2009-03-04 04:32:55 +01:00
|
|
|
UInt8 transferType;
|
2022-12-20 06:28:51 +01:00
|
|
|
UInt16 endPointMaxPacketSize = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
UInt8 interval;
|
2022-12-20 06:28:51 +01:00
|
|
|
|
|
|
|
// Attempt to retrieve the 'true' packet-size from supported interface.
|
|
|
|
kr = (*interface)
|
|
|
|
->GetEndpointProperties(interface, 0, endpoint,
|
|
|
|
kUSBOut,
|
|
|
|
&transferType,
|
|
|
|
&endPointMaxPacketSize, &interval);
|
|
|
|
if (kr == kIOReturnSuccess && !endPointMaxPacketSize) {
|
|
|
|
ERR("GetEndpointProperties() returned zero len packet-size");
|
|
|
|
}
|
|
|
|
|
|
|
|
UInt16 pipePropMaxPacketSize;
|
2009-03-04 04:32:55 +01:00
|
|
|
UInt8 number;
|
|
|
|
UInt8 direction;
|
|
|
|
|
2022-12-20 06:28:51 +01:00
|
|
|
// Proceed with extracting the transfer direction, so we can fill in the
|
|
|
|
// appropriate fields (bulkIn or bulkOut).
|
2009-03-04 04:32:55 +01:00
|
|
|
kr = (*interface)->GetPipeProperties(interface, endpoint,
|
|
|
|
&direction,
|
2022-12-20 06:28:51 +01:00
|
|
|
&number, &transferType, &pipePropMaxPacketSize, &interval);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (kr == 0) {
|
|
|
|
if (transferType != kUSBBulk) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction == kUSBIn) {
|
|
|
|
handle->info.has_bulk_in = 1;
|
|
|
|
handle->bulkIn = endpoint;
|
|
|
|
} else if (direction == kUSBOut) {
|
|
|
|
handle->info.has_bulk_out = 1;
|
|
|
|
handle->bulkOut = endpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handle->info.ifc_protocol == 0x01) {
|
2022-12-20 06:28:51 +01:00
|
|
|
handle->zero_mask = (endPointMaxPacketSize == 0) ?
|
|
|
|
pipePropMaxPacketSize - 1 : endPointMaxPacketSize - 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-07 19:49:36 +02:00
|
|
|
ERR("could not get pipe properties for endpoint %u (%08x)\n", endpoint, kr);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (handle->info.has_bulk_in && handle->info.has_bulk_out) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handle->callback(&handle->info) == 0) {
|
|
|
|
handle->interface = interface;
|
|
|
|
handle->success = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear both the endpoints, because it has been observed
|
|
|
|
* that the Mac may otherwise (incorrectly) start out with
|
|
|
|
* them in bad state.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (handle->info.has_bulk_in) {
|
|
|
|
kr = (*interface)->ClearPipeStallBothEnds(interface,
|
|
|
|
handle->bulkIn);
|
|
|
|
if (kr != 0) {
|
2010-07-22 19:59:55 +02:00
|
|
|
ERR("could not clear input pipe; result %x, ignoring...\n", kr);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handle->info.has_bulk_out) {
|
|
|
|
kr = (*interface)->ClearPipeStallBothEnds(interface,
|
|
|
|
handle->bulkOut);
|
|
|
|
if (kr != 0) {
|
2010-07-22 19:59:55 +02:00
|
|
|
ERR("could not clear output pipe; result %x, ignoring....\n", kr);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
next_interface:
|
|
|
|
(*interface)->USBInterfaceClose(interface);
|
|
|
|
(*interface)->Release(interface);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Try out the given device and see if there's a match. Returns 0 on
|
|
|
|
* success, -1 on failure.
|
|
|
|
*/
|
|
|
|
static int try_device(io_service_t device, usb_handle *handle) {
|
|
|
|
kern_return_t kr;
|
|
|
|
IOCFPlugInInterface **plugin = NULL;
|
2018-11-11 00:56:19 +01:00
|
|
|
IOUSBDeviceInterface500** dev = NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
SInt32 score;
|
|
|
|
HRESULT result;
|
|
|
|
UInt8 serialIndex;
|
2012-04-06 21:39:30 +02:00
|
|
|
UInt32 locationId;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
// Create an intermediate plugin.
|
|
|
|
kr = IOCreatePlugInInterfaceForService(device,
|
|
|
|
kIOUSBDeviceUserClientTypeID,
|
|
|
|
kIOCFPlugInInterfaceID,
|
|
|
|
&plugin, &score);
|
|
|
|
|
|
|
|
if ((kr != 0) || (plugin == NULL)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now create the device interface.
|
2018-11-11 00:56:19 +01:00
|
|
|
result = (*plugin)->QueryInterface(plugin, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID500),
|
|
|
|
(LPVOID*)&dev);
|
2009-03-04 04:32:55 +01:00
|
|
|
if ((result != 0) || (dev == NULL)) {
|
|
|
|
ERR("Couldn't create a device interface (%08x)\n", (int) result);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2012-02-28 16:21:08 +01:00
|
|
|
/*
|
2009-03-04 04:32:55 +01:00
|
|
|
* We don't need the intermediate interface after the device interface
|
|
|
|
* is created.
|
|
|
|
*/
|
|
|
|
IODestroyPlugInInterface(plugin);
|
|
|
|
|
|
|
|
// So, we have a device, finally. Grab its vitals.
|
|
|
|
|
|
|
|
kr = (*dev)->GetDeviceVendor(dev, &handle->info.dev_vendor);
|
|
|
|
if (kr != 0) {
|
|
|
|
ERR("GetDeviceVendor");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
kr = (*dev)->GetDeviceProduct(dev, &handle->info.dev_product);
|
|
|
|
if (kr != 0) {
|
|
|
|
ERR("GetDeviceProduct");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
kr = (*dev)->GetDeviceClass(dev, &handle->info.dev_class);
|
|
|
|
if (kr != 0) {
|
|
|
|
ERR("GetDeviceClass");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
kr = (*dev)->GetDeviceSubClass(dev, &handle->info.dev_subclass);
|
|
|
|
if (kr != 0) {
|
|
|
|
ERR("GetDeviceSubClass");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
kr = (*dev)->GetDeviceProtocol(dev, &handle->info.dev_protocol);
|
|
|
|
if (kr != 0) {
|
|
|
|
ERR("GetDeviceProtocol");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2012-04-06 21:39:30 +02:00
|
|
|
kr = (*dev)->GetLocationID(dev, &locationId);
|
|
|
|
if (kr != 0) {
|
|
|
|
ERR("GetLocationId");
|
|
|
|
goto error;
|
|
|
|
}
|
2014-08-15 00:50:13 +02:00
|
|
|
snprintf(handle->info.device_path, sizeof(handle->info.device_path),
|
|
|
|
"usb:%" PRIu32 "X", (unsigned int)locationId);
|
2012-04-06 21:39:30 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
|
|
|
|
|
|
|
|
if (serialIndex > 0) {
|
|
|
|
IOUSBDevRequest req;
|
|
|
|
UInt16 buffer[256];
|
|
|
|
|
|
|
|
req.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
|
|
|
|
req.bRequest = kUSBRqGetDescriptor;
|
|
|
|
req.wValue = (kUSBStringDesc << 8) | serialIndex;
|
2011-07-08 02:08:10 +02:00
|
|
|
//language ID (en-us) for serial number string
|
|
|
|
req.wIndex = 0x0409;
|
2009-03-04 04:32:55 +01:00
|
|
|
req.pData = buffer;
|
|
|
|
req.wLength = sizeof(buffer);
|
|
|
|
kr = (*dev)->DeviceRequest(dev, &req);
|
|
|
|
|
|
|
|
if (kr == kIOReturnSuccess && req.wLenDone > 0) {
|
|
|
|
int i, count;
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
// 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++)
|
|
|
|
handle->info.serial_number[i] = buffer[i + 1];
|
|
|
|
handle->info.serial_number[i] = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// device has no serial number
|
|
|
|
handle->info.serial_number[0] = 0;
|
|
|
|
}
|
2020-05-28 06:39:37 +02:00
|
|
|
handle->info.interface[0] = 0;
|
2009-10-07 03:07:49 +02:00
|
|
|
handle->info.writable = 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (try_interfaces(dev, handle)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*dev)->Release(dev);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
|
|
|
if (dev != NULL) {
|
|
|
|
(*dev)->Release(dev);
|
|
|
|
}
|
2012-02-28 16:21:08 +01:00
|
|
|
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Initializes the USB system. Returns 0 on success, -1 on error. */
|
2015-10-30 19:22:01 +01:00
|
|
|
static int init_usb(ifc_match_func callback, std::unique_ptr<usb_handle>* handle) {
|
2009-03-04 04:32:55 +01:00
|
|
|
int ret = -1;
|
|
|
|
CFMutableDictionaryRef matchingDict;
|
|
|
|
kern_return_t result;
|
|
|
|
io_iterator_t iterator;
|
|
|
|
usb_handle h;
|
|
|
|
|
|
|
|
h.success = 0;
|
|
|
|
h.callback = callback;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create our matching dictionary to find appropriate devices.
|
|
|
|
* IOServiceAddMatchingNotification consumes the reference, so we
|
|
|
|
* do not need to release it.
|
|
|
|
*/
|
|
|
|
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
|
|
|
|
|
|
|
|
if (matchingDict == NULL) {
|
|
|
|
ERR("Couldn't create USB matching dictionary.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = IOServiceGetMatchingServices(
|
|
|
|
kIOMasterPortDefault, matchingDict, &iterator);
|
|
|
|
|
|
|
|
if (result != 0) {
|
|
|
|
ERR("Could not create iterator.");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
for (;;) {
|
|
|
|
if (! IOIteratorIsValid(iterator)) {
|
2023-06-20 23:44:53 +02:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
io_service_t device = IOIteratorNext(iterator);
|
|
|
|
|
|
|
|
if (device == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (try_device(device, &h) != 0) {
|
|
|
|
IOObjectRelease(device);
|
2017-08-19 03:25:44 +02:00
|
|
|
continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (h.success) {
|
2023-02-15 16:21:22 +01:00
|
|
|
handle->reset(new usb_handle(h));
|
2009-03-04 04:32:55 +01:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOObjectRelease(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
IOObjectRelease(iterator);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Definitions of this file's public functions.
|
|
|
|
*/
|
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;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (init_usb(callback, &handle) < 0) {
|
|
|
|
/* Something went wrong initializing USB. */
|
2023-09-03 18:30:46 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handle) {
|
|
|
|
result = std::make_unique<OsxUsbTransport>(std::move(handle), timeout_ms);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2023-09-03 18:30:46 +02:00
|
|
|
return result;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-09-04 23:32:54 +02:00
|
|
|
OsxUsbTransport::~OsxUsbTransport() {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
int OsxUsbTransport::Close() {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* TODO: Something better here? */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-26 17:56:09 +02:00
|
|
|
/*
|
|
|
|
TODO: this SHOULD be easy to do with ResetDevice() from IOUSBDeviceInterface.
|
|
|
|
However to perform operations that manipulate the state of the device, you must
|
|
|
|
claim ownership of the device with USBDeviceOpenSeize(). However, this operation
|
|
|
|
always fails with kIOReturnExclusiveAccess.
|
|
|
|
It seems that the kext com.apple.driver.usb.AppleUSBHostCompositeDevice
|
|
|
|
always loads and claims ownership of the device and refuses to give it up.
|
|
|
|
*/
|
|
|
|
int OsxUsbTransport::Reset() {
|
|
|
|
ERR("USB reset is currently unsupported on osx\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
ssize_t OsxUsbTransport::Read(void* data, size_t len) {
|
2009-03-04 04:32:55 +01:00
|
|
|
IOReturn result;
|
|
|
|
UInt32 numBytes = len;
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (handle_ == nullptr) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (handle_->interface == nullptr) {
|
2009-03-04 04:32:55 +01:00
|
|
|
ERR("usb_read interface was null\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (handle_->bulkIn == 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
ERR("bulkIn endpoint not assigned\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-26 17:56:09 +02:00
|
|
|
if (!ms_timeout_) {
|
|
|
|
result = (*handle_->interface)
|
|
|
|
->ReadPipe(handle_->interface, handle_->bulkIn, data, &numBytes);
|
|
|
|
} else {
|
|
|
|
result = (*handle_->interface)
|
|
|
|
->ReadPipeTO(handle_->interface, handle_->bulkIn, data, &numBytes,
|
|
|
|
ms_timeout_, ms_timeout_);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (result == 0) {
|
|
|
|
return (int) numBytes;
|
|
|
|
} else {
|
|
|
|
ERR("usb_read failed with status %x\n", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
ssize_t OsxUsbTransport::Write(const void* data, size_t len) {
|
2009-03-04 04:32:55 +01:00
|
|
|
IOReturn result;
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (handle_ == NULL) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (handle_->interface == NULL) {
|
2009-03-04 04:32:55 +01:00
|
|
|
ERR("usb_write interface was null\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (handle_->bulkOut == 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
ERR("bulkOut endpoint not assigned\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-05-21 22:20:47 +02:00
|
|
|
#if 0
|
2015-10-30 19:22:01 +01:00
|
|
|
result = (*handle_->interface)->WritePipe(
|
|
|
|
handle_->interface, handle_->bulkOut, (void *)data, len);
|
2010-05-21 22:20:47 +02:00
|
|
|
#else
|
|
|
|
/* Attempt to work around crashes in the USB driver that may be caused
|
|
|
|
* by trying to write too much data at once. The kernel IOCopyMapper
|
|
|
|
* panics if a single iovmAlloc needs more than half of its mapper pages.
|
|
|
|
*/
|
|
|
|
const int maxLenToSend = 1048576; // 1 MiB
|
|
|
|
int lenRemaining = len;
|
|
|
|
result = 0;
|
|
|
|
while (lenRemaining > 0) {
|
|
|
|
int lenToSend = lenRemaining > maxLenToSend
|
|
|
|
? maxLenToSend : lenRemaining;
|
|
|
|
|
2018-07-26 17:56:09 +02:00
|
|
|
if (!ms_timeout_) { // blocking
|
|
|
|
result = (*handle_->interface)
|
|
|
|
->WritePipe(handle_->interface, handle_->bulkOut, (void*)data,
|
|
|
|
lenToSend);
|
|
|
|
} else {
|
|
|
|
result = (*handle_->interface)
|
|
|
|
->WritePipeTO(handle_->interface, handle_->bulkOut, (void*)data,
|
|
|
|
lenToSend, ms_timeout_, ms_timeout_);
|
|
|
|
}
|
|
|
|
|
2010-05-21 22:20:47 +02:00
|
|
|
if (result != 0) break;
|
|
|
|
|
|
|
|
lenRemaining -= lenToSend;
|
|
|
|
data = (const char*)data + lenToSend;
|
|
|
|
}
|
|
|
|
#endif
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#if 0
|
2015-10-30 19:22:01 +01:00
|
|
|
if ((result == 0) && (handle_->zero_mask)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* we need 0-markers and our transfer */
|
2015-10-30 19:22:01 +01:00
|
|
|
if(!(len & handle_->zero_mask)) {
|
|
|
|
result = (*handle_->interface)->WritePipe(
|
|
|
|
handle_->interface, handle_->bulkOut, (void *)data, 0);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (result != 0) {
|
|
|
|
ERR("usb_write failed with status %x\n", result);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|