blob: 0ce85f35cb60c8e7a9d1f55fba81d47a0d50e70b [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Albertdb6fe642015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_USB
18
19#include "sysdeps.h"
20
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080021#include <CoreFoundation/CoreFoundation.h>
22
23#include <IOKit/IOKitLib.h>
24#include <IOKit/IOCFPlugIn.h>
25#include <IOKit/usb/IOUSBLib.h>
26#include <IOKit/IOMessage.h>
27#include <mach/mach_port.h>
28
Dan Albert25896062015-04-16 19:20:40 -070029#include <inttypes.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080030#include <stdio.h>
31
Siva Velusamy878e36b2015-08-18 17:53:16 -070032#include <base/logging.h>
33#include <base/stringprintf.h>
34
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080035#include "adb.h"
Dan Albertbd3d4482015-02-25 10:26:17 -080036#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080037
38#define DBG D
39
Elliott Hughesd9ce0ff2015-08-03 18:07:12 -070040// There's no strerror equivalent for the errors returned by IOKit.
41// https://developer.apple.com/library/mac/documentation/DeviceDrivers/Conceptual/AccessingHardware/AH_Handling_Errors/AH_Handling_Errors.html
42// Search the web for "IOReturn.h" to find a complete up to date list.
43
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080044static IONotificationPortRef notificationPort = 0;
Al Sutton178bae82014-11-21 12:21:12 +000045static io_iterator_t notificationIterator;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080046
47struct usb_handle
48{
Siva Velusamy3ce46072015-08-13 08:48:06 -070049 UInt8 bulkIn;
50 UInt8 bulkOut;
51 IOUSBInterfaceInterface190** interface;
52 io_object_t usbNotification;
53 unsigned int zero_mask;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080054};
55
56static CFRunLoopRef currentRunLoop = 0;
57static pthread_mutex_t start_lock;
58static pthread_cond_t start_cond;
59
60
Dima Zavin3e824912009-05-08 18:25:58 -070061static void AndroidInterfaceAdded(void *refCon, io_iterator_t iterator);
62static void AndroidInterfaceNotify(void *refCon, io_iterator_t iterator,
63 natural_t messageType,
64 void *messageArgument);
Siva Velusamy3ce46072015-08-13 08:48:06 -070065static usb_handle* CheckInterface(IOUSBInterfaceInterface190 **iface,
Dima Zavin3e824912009-05-08 18:25:58 -070066 UInt16 vendor, UInt16 product);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080067
68static int
69InitUSB()
70{
71 CFMutableDictionaryRef matchingDict;
72 CFRunLoopSourceRef runLoopSource;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080073
74 //* To set up asynchronous notifications, create a notification port and
75 //* add its run loop event source to the program's run loop
76 notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
77 runLoopSource = IONotificationPortGetRunLoopSource(notificationPort);
78 CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
79
Al Sutton178bae82014-11-21 12:21:12 +000080 //* Create our matching dictionary to find the Android device's
81 //* adb interface
82 //* IOServiceAddMatchingNotification consumes the reference, so we do
83 //* not need to release this
84 matchingDict = IOServiceMatching(kIOUSBInterfaceClassName);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080085
Al Sutton178bae82014-11-21 12:21:12 +000086 if (!matchingDict) {
Siva Velusamy878e36b2015-08-18 17:53:16 -070087 LOG(ERROR) << "Couldn't create USB matching dictionary.";
Al Sutton178bae82014-11-21 12:21:12 +000088 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080089 }
90
Al Sutton178bae82014-11-21 12:21:12 +000091 //* We have to get notifications for all potential candidates and test them
92 //* at connection time because the matching rules don't allow for a
93 //* USB interface class of 0xff for class+subclass+protocol matches
94 //* See https://developer.apple.com/library/mac/qa/qa1076/_index.html
95 IOServiceAddMatchingNotification(
96 notificationPort,
97 kIOFirstMatchNotification,
98 matchingDict,
99 AndroidInterfaceAdded,
100 NULL,
101 &notificationIterator);
102
103 //* Iterate over set of matching interfaces to access already-present
104 //* devices and to arm the notification
105 AndroidInterfaceAdded(NULL, notificationIterator);
106
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800107 return 0;
108}
109
110static void
Dima Zavin3e824912009-05-08 18:25:58 -0700111AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800112{
113 kern_return_t kr;
114 io_service_t usbDevice;
Dima Zavin3e824912009-05-08 18:25:58 -0700115 io_service_t usbInterface;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800116 IOCFPlugInInterface **plugInInterface = NULL;
Dima Zavin3e824912009-05-08 18:25:58 -0700117 IOUSBInterfaceInterface220 **iface = NULL;
118 IOUSBDeviceInterface197 **dev = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800119 HRESULT result;
120 SInt32 score;
Elliott Hughes8dc9c862015-08-25 17:48:12 -0700121 uint32_t locationId;
Dan Albert25896062015-04-16 19:20:40 -0700122 UInt8 if_class, subclass, protocol;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800123 UInt16 vendor;
124 UInt16 product;
125 UInt8 serialIndex;
126 char serial[256];
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700127 char devpathBuf[64];
128 char *devpath = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800129
Dima Zavin3e824912009-05-08 18:25:58 -0700130 while ((usbInterface = IOIteratorNext(iterator))) {
131 //* Create an intermediate interface plugin
132 kr = IOCreatePlugInInterfaceForService(usbInterface,
133 kIOUSBInterfaceUserClientTypeID,
134 kIOCFPlugInInterfaceID,
135 &plugInInterface, &score);
136 IOObjectRelease(usbInterface);
137 if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700138 LOG(ERROR) << "Unable to create an interface plug-in (" << std::hex << kr << ")";
Dima Zavin3e824912009-05-08 18:25:58 -0700139 continue;
140 }
141
142 //* This gets us the interface object
Dan Albert25896062015-04-16 19:20:40 -0700143 result = (*plugInInterface)->QueryInterface(
144 plugInInterface,
145 CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID*)&iface);
Dima Zavin3e824912009-05-08 18:25:58 -0700146 //* We only needed the plugin to get the interface, so discard it
147 (*plugInInterface)->Release(plugInInterface);
148 if (result || !iface) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700149 LOG(ERROR) << "Couldn't query the interface (" << std::hex << result << ")";
Dima Zavin3e824912009-05-08 18:25:58 -0700150 continue;
151 }
152
Dan Albert25896062015-04-16 19:20:40 -0700153 kr = (*iface)->GetInterfaceClass(iface, &if_class);
Al Sutton178bae82014-11-21 12:21:12 +0000154 kr = (*iface)->GetInterfaceSubClass(iface, &subclass);
155 kr = (*iface)->GetInterfaceProtocol(iface, &protocol);
Dan Albert25896062015-04-16 19:20:40 -0700156 if(if_class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) {
Al Sutton178bae82014-11-21 12:21:12 +0000157 // Ignore non-ADB devices.
Siva Velusamy878e36b2015-08-18 17:53:16 -0700158 LOG(DEBUG) << "Ignoring interface with incorrect class/subclass/protocol - " << if_class
159 << ", " << subclass << ", " << protocol;
Al Sutton178bae82014-11-21 12:21:12 +0000160 (*iface)->Release(iface);
161 continue;
162 }
163
Dima Zavin3e824912009-05-08 18:25:58 -0700164 //* this gets us an ioservice, with which we will find the actual
165 //* device; after getting a plugin, and querying the interface, of
166 //* course.
167 //* Gotta love OS X
168 kr = (*iface)->GetDevice(iface, &usbDevice);
169 if (kIOReturnSuccess != kr || !usbDevice) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700170 LOG(ERROR) << "Couldn't grab device from interface (" << std::hex << kr << ")";
Dima Zavin3e824912009-05-08 18:25:58 -0700171 continue;
172 }
173
174 plugInInterface = NULL;
175 score = 0;
176 //* create an intermediate device plugin
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800177 kr = IOCreatePlugInInterfaceForService(usbDevice,
178 kIOUSBDeviceUserClientTypeID,
179 kIOCFPlugInInterfaceID,
180 &plugInInterface, &score);
Dima Zavin3e824912009-05-08 18:25:58 -0700181 //* only needed this to find the plugin
182 (void)IOObjectRelease(usbDevice);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800183 if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700184 LOG(ERROR) << "Unable to create a device plug-in (" << std::hex << kr << ")";
Dima Zavin3e824912009-05-08 18:25:58 -0700185 continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800186 }
187
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800188 result = (*plugInInterface)->QueryInterface(plugInInterface,
Dan Albert25896062015-04-16 19:20:40 -0700189 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID*)&dev);
Dima Zavin3e824912009-05-08 18:25:58 -0700190 //* only needed this to query the plugin
191 (*plugInInterface)->Release(plugInInterface);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800192 if (result || !dev) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700193 LOG(ERROR) << "Couldn't create a device interface (" << std::hex << result << ")";
Dima Zavin3e824912009-05-08 18:25:58 -0700194 continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800195 }
196
Dima Zavin3e824912009-05-08 18:25:58 -0700197 //* Now after all that, we actually have a ref to the device and
198 //* the interface that matched our criteria
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800199 kr = (*dev)->GetDeviceVendor(dev, &vendor);
200 kr = (*dev)->GetDeviceProduct(dev, &product);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700201 kr = (*dev)->GetLocationID(dev, &locationId);
202 if (kr == 0) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700203 snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X", locationId);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700204 devpath = devpathBuf;
205 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800206 kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
207
Siva Velusamy878e36b2015-08-18 17:53:16 -0700208 if (serialIndex > 0) {
209 IOUSBDevRequest req;
210 UInt16 buffer[256];
211 UInt16 languages[128];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800212
Siva Velusamy878e36b2015-08-18 17:53:16 -0700213 memset(languages, 0, sizeof(languages));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800214
Siva Velusamy878e36b2015-08-18 17:53:16 -0700215 req.bmRequestType =
216 USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
217 req.bRequest = kUSBRqGetDescriptor;
218 req.wValue = (kUSBStringDesc << 8) | 0;
219 req.wIndex = 0;
220 req.pData = languages;
221 req.wLength = sizeof(languages);
222 kr = (*dev)->DeviceRequest(dev, &req);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800223
Siva Velusamy878e36b2015-08-18 17:53:16 -0700224 if (kr == kIOReturnSuccess && req.wLenDone > 0) {
Guang Zhu84b5bd22009-08-06 17:21:52 -0700225
Siva Velusamy878e36b2015-08-18 17:53:16 -0700226 int langCount = (req.wLenDone - 2) / 2, lang;
Guang Zhu84b5bd22009-08-06 17:21:52 -0700227
Siva Velusamy878e36b2015-08-18 17:53:16 -0700228 for (lang = 1; lang <= langCount; lang++) {
Guang Zhu84b5bd22009-08-06 17:21:52 -0700229
Siva Velusamy878e36b2015-08-18 17:53:16 -0700230 memset(buffer, 0, sizeof(buffer));
231 memset(&req, 0, sizeof(req));
Guang Zhu84b5bd22009-08-06 17:21:52 -0700232
Siva Velusamy878e36b2015-08-18 17:53:16 -0700233 req.bmRequestType =
234 USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
235 req.bRequest = kUSBRqGetDescriptor;
236 req.wValue = (kUSBStringDesc << 8) | serialIndex;
237 req.wIndex = languages[lang];
238 req.pData = buffer;
239 req.wLength = sizeof(buffer);
240 kr = (*dev)->DeviceRequest(dev, &req);
Guang Zhu84b5bd22009-08-06 17:21:52 -0700241
Siva Velusamy878e36b2015-08-18 17:53:16 -0700242 if (kr == kIOReturnSuccess && req.wLenDone > 0) {
243 int i, count;
Guang Zhu84b5bd22009-08-06 17:21:52 -0700244
Siva Velusamy878e36b2015-08-18 17:53:16 -0700245 // skip first word, and copy the rest to the serial string,
246 // changing shorts to bytes.
247 count = (req.wLenDone - 1) / 2;
248 for (i = 0; i < count; i++)
249 serial[i] = buffer[i + 1];
250 serial[i] = 0;
251 break;
252 }
253 }
254 }
255 }
256
Dima Zavin3e824912009-05-08 18:25:58 -0700257 (*dev)->Release(dev);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800258
Siva Velusamy878e36b2015-08-18 17:53:16 -0700259 LOG(INFO) << android::base::StringPrintf("Found vid=%04x pid=%04x serial=%s\n",
260 vendor, product, serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800261
Siva Velusamy3ce46072015-08-13 08:48:06 -0700262 usb_handle* handle = CheckInterface((IOUSBInterfaceInterface190**)iface,
Dima Zavin3e824912009-05-08 18:25:58 -0700263 vendor, product);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800264 if (handle == NULL) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700265 LOG(ERROR) << "Could not find device interface";
Dima Zavin3e824912009-05-08 18:25:58 -0700266 (*iface)->Release(iface);
267 continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800268 }
269
Siva Velusamy878e36b2015-08-18 17:53:16 -0700270 LOG(DEBUG) << "AndroidDeviceAdded calling register_usb_transport";
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700271 register_usb_transport(handle, (serial[0] ? serial : NULL), devpath, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800272
Dima Zavin3e824912009-05-08 18:25:58 -0700273 // Register for an interest notification of this device being removed.
274 // Pass the reference to our private data as the refCon for the
275 // notification.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800276 kr = IOServiceAddInterestNotification(notificationPort,
Dima Zavin3e824912009-05-08 18:25:58 -0700277 usbInterface,
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800278 kIOGeneralInterest,
Dima Zavin3e824912009-05-08 18:25:58 -0700279 AndroidInterfaceNotify,
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800280 handle,
281 &handle->usbNotification);
Dima Zavin3e824912009-05-08 18:25:58 -0700282
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800283 if (kIOReturnSuccess != kr) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700284 LOG(ERROR) << "Unable to create interest notification (" << std::hex << kr << ")";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800285 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800286 }
287}
288
289static void
Dima Zavin3e824912009-05-08 18:25:58 -0700290AndroidInterfaceNotify(void *refCon, io_service_t service, natural_t messageType, void *messageArgument)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800291{
292 usb_handle *handle = (usb_handle *)refCon;
293
294 if (messageType == kIOMessageServiceIsTerminated) {
Dima Zavin3e824912009-05-08 18:25:58 -0700295 if (!handle) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700296 LOG(ERROR) << "NULL handle";
Dima Zavin3e824912009-05-08 18:25:58 -0700297 return;
298 }
Siva Velusamy878e36b2015-08-18 17:53:16 -0700299 LOG(DEBUG) << "AndroidInterfaceNotify";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800300 IOObjectRelease(handle->usbNotification);
301 usb_kick(handle);
302 }
303}
304
Siva Velusamy3ce46072015-08-13 08:48:06 -0700305// Used to clear both the endpoints before starting.
306// When adb quits, we might clear the host endpoint but not the device.
307// So we make sure both sides are clear before starting up.
308static bool ClearPipeStallBothEnds(IOUSBInterfaceInterface190** interface, UInt8 bulkEp) {
309 IOReturn rc = (*interface)->ClearPipeStallBothEnds(interface, bulkEp);
310 if (rc != kIOReturnSuccess) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700311 LOG(ERROR) << "Could not clear pipe stall both ends: " << std::hex << rc;
Siva Velusamy3ce46072015-08-13 08:48:06 -0700312 return false;
313 }
314 return true;
315}
316
Dima Zavin3e824912009-05-08 18:25:58 -0700317//* TODO: simplify this further since we only register to get ADB interface
318//* subclass+protocol events
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800319static usb_handle*
Siva Velusamy3ce46072015-08-13 08:48:06 -0700320CheckInterface(IOUSBInterfaceInterface190 **interface, UInt16 vendor, UInt16 product)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800321{
Elliott Hughes6d6cb542015-08-13 15:01:18 -0700322 usb_handle* handle;
323 IOReturn kr;
324 UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol;
325 UInt8 endpoint;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800326
Dima Zavin3e824912009-05-08 18:25:58 -0700327 //* Now open the interface. This will cause the pipes associated with
328 //* the endpoints in the interface descriptor to be instantiated
329 kr = (*interface)->USBInterfaceOpen(interface);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800330 if (kr != kIOReturnSuccess) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700331 LOG(ERROR) << "Could not open interface: " << std::hex << kr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800332 return NULL;
333 }
334
Dima Zavin3e824912009-05-08 18:25:58 -0700335 //* Get the number of endpoints associated with this interface
336 kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
337 if (kr != kIOReturnSuccess) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700338 LOG(ERROR) << "Unable to get number of endpoints: " << std::hex << kr;
Dima Zavin3e824912009-05-08 18:25:58 -0700339 goto err_get_num_ep;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800340 }
341
Dima Zavin3e824912009-05-08 18:25:58 -0700342 //* Get interface class, subclass and protocol
343 if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess ||
344 (*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess ||
345 (*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700346 LOG(ERROR) << "Unable to get interface class, subclass and protocol";
Dima Zavin3e824912009-05-08 18:25:58 -0700347 goto err_get_interface_class;
348 }
349
350 //* check to make sure interface class, subclass and protocol match ADB
351 //* avoid opening mass storage endpoints
Siva Velusamy3ce46072015-08-13 08:48:06 -0700352 if (!is_adb_interface(vendor, product, interfaceClass, interfaceSubClass, interfaceProtocol)) {
Dima Zavin3e824912009-05-08 18:25:58 -0700353 goto err_bad_adb_interface;
Siva Velusamy3ce46072015-08-13 08:48:06 -0700354 }
Dima Zavin3e824912009-05-08 18:25:58 -0700355
Dan Albert25896062015-04-16 19:20:40 -0700356 handle = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesd0269c92015-04-21 19:39:52 -0700357 if (handle == nullptr) goto err_bad_adb_interface;
Dima Zavin3e824912009-05-08 18:25:58 -0700358
359 //* Iterate over the endpoints for this interface and find the first
360 //* bulk in/out pipes available. These will be our read/write pipes.
Elliott Hughes6d6cb542015-08-13 15:01:18 -0700361 for (endpoint = 1; endpoint <= interfaceNumEndpoints; endpoint++) {
Dima Zavin3e824912009-05-08 18:25:58 -0700362 UInt8 transferType;
363 UInt16 maxPacketSize;
364 UInt8 interval;
365 UInt8 number;
366 UInt8 direction;
367
368 kr = (*interface)->GetPipeProperties(interface, endpoint, &direction,
369 &number, &transferType, &maxPacketSize, &interval);
Siva Velusamy3ce46072015-08-13 08:48:06 -0700370 if (kr != kIOReturnSuccess) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700371 LOG(ERROR) << "FindDeviceInterface - could not get pipe properties: "
372 << std::hex << kr;
Dima Zavin3e824912009-05-08 18:25:58 -0700373 goto err_get_pipe_props;
374 }
Siva Velusamy3ce46072015-08-13 08:48:06 -0700375
376 if (kUSBBulk != transferType) continue;
377
378 if (kUSBIn == direction) {
379 handle->bulkIn = endpoint;
380 if (!ClearPipeStallBothEnds(interface, handle->bulkIn)) goto err_get_pipe_props;
381 }
382
383 if (kUSBOut == direction) {
384 handle->bulkOut = endpoint;
385 if (!ClearPipeStallBothEnds(interface, handle->bulkOut)) goto err_get_pipe_props;
386 }
387
388 handle->zero_mask = maxPacketSize - 1;
Dima Zavin3e824912009-05-08 18:25:58 -0700389 }
390
391 handle->interface = interface;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800392 return handle;
Dima Zavin3e824912009-05-08 18:25:58 -0700393
394err_get_pipe_props:
395 free(handle);
396err_bad_adb_interface:
397err_get_interface_class:
398err_get_num_ep:
399 (*interface)->USBInterfaceClose(interface);
400 return NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800401}
402
403
404void* RunLoopThread(void* unused)
405{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800406 InitUSB();
407
408 currentRunLoop = CFRunLoopGetCurrent();
409
410 // Signal the parent that we are running
411 adb_mutex_lock(&start_lock);
412 adb_cond_signal(&start_cond);
413 adb_mutex_unlock(&start_lock);
414
415 CFRunLoopRun();
416 currentRunLoop = 0;
417
Al Sutton178bae82014-11-21 12:21:12 +0000418 IOObjectRelease(notificationIterator);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800419 IONotificationPortDestroy(notificationPort);
420
Siva Velusamy878e36b2015-08-18 17:53:16 -0700421 LOG(DEBUG) << "RunLoopThread done";
Elliott Hughesf2517142015-05-05 13:41:21 -0700422 return NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800423}
424
Dan Albert53a37442015-05-08 16:13:53 -0700425static void usb_cleanup() {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700426 LOG(DEBUG) << "usb_cleanup";
Dan Albert53a37442015-05-08 16:13:53 -0700427 close_usb_devices();
428 if (currentRunLoop)
429 CFRunLoopStop(currentRunLoop);
430}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800431
Elliott Hughesf2517142015-05-05 13:41:21 -0700432void usb_init() {
Dan Albert53a37442015-05-08 16:13:53 -0700433 static bool initialized = false;
434 if (!initialized) {
435 atexit(usb_cleanup);
436
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800437 adb_mutex_init(&start_lock, NULL);
438 adb_cond_init(&start_cond, NULL);
439
Elliott Hughesf2517142015-05-05 13:41:21 -0700440 if (!adb_thread_create(RunLoopThread, nullptr)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800441 fatal_errno("cannot create input thread");
Elliott Hughesf2517142015-05-05 13:41:21 -0700442 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800443
444 // Wait for initialization to finish
445 adb_mutex_lock(&start_lock);
446 adb_cond_wait(&start_cond, &start_lock);
447 adb_mutex_unlock(&start_lock);
448
449 adb_mutex_destroy(&start_lock);
450 adb_cond_destroy(&start_cond);
451
Dan Albert53a37442015-05-08 16:13:53 -0700452 initialized = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800453 }
454}
455
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800456int usb_write(usb_handle *handle, const void *buf, int len)
457{
458 IOReturn result;
459
460 if (!len)
461 return 0;
462
463 if (!handle)
464 return -1;
465
466 if (NULL == handle->interface) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700467 LOG(ERROR) << "usb_write interface was null";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800468 return -1;
469 }
470
471 if (0 == handle->bulkOut) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700472 LOG(ERROR) << "bulkOut endpoint not assigned";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800473 return -1;
474 }
475
476 result =
Siva Velusamy878e36b2015-08-18 17:53:16 -0700477 (*handle->interface)->WritePipe(handle->interface, handle->bulkOut, (void *)buf, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800478
479 if ((result == 0) && (handle->zero_mask)) {
480 /* we need 0-markers and our transfer */
481 if(!(len & handle->zero_mask)) {
482 result =
483 (*handle->interface)->WritePipe(
484 handle->interface, handle->bulkOut, (void *)buf, 0);
485 }
486 }
487
488 if (0 == result)
489 return 0;
490
Siva Velusamy878e36b2015-08-18 17:53:16 -0700491 LOG(ERROR) << "usb_write failed with status: " << std::hex << result;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800492 return -1;
493}
494
495int usb_read(usb_handle *handle, void *buf, int len)
496{
497 IOReturn result;
498 UInt32 numBytes = len;
499
500 if (!len) {
501 return 0;
502 }
503
504 if (!handle) {
505 return -1;
506 }
507
508 if (NULL == handle->interface) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700509 LOG(ERROR) << "usb_read interface was null";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800510 return -1;
511 }
512
513 if (0 == handle->bulkIn) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700514 LOG(ERROR) << "bulkIn endpoint not assigned";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800515 return -1;
516 }
517
Esteban de la Canal647231a2014-09-11 11:02:17 -0700518 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800519
Esteban de la Canal647231a2014-09-11 11:02:17 -0700520 if (kIOUSBPipeStalled == result) {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700521 LOG(ERROR) << "Pipe stalled, clearing stall.\n";
Esteban de la Canal647231a2014-09-11 11:02:17 -0700522 (*handle->interface)->ClearPipeStall(handle->interface, handle->bulkIn);
523 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
524 }
525
526 if (kIOReturnSuccess == result)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800527 return 0;
528 else {
Siva Velusamy878e36b2015-08-18 17:53:16 -0700529 LOG(ERROR) << "usb_read failed with status: " << std::hex << result;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800530 }
531
532 return -1;
533}
534
535int usb_close(usb_handle *handle)
536{
537 return 0;
538}
539
540void usb_kick(usb_handle *handle)
541{
Siva Velusamy878e36b2015-08-18 17:53:16 -0700542 LOG(INFO) << "Kicking handle";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800543 /* release the interface */
Dima Zavin3e824912009-05-08 18:25:58 -0700544 if (!handle)
545 return;
546
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800547 if (handle->interface)
548 {
549 (*handle->interface)->USBInterfaceClose(handle->interface);
550 (*handle->interface)->Release(handle->interface);
551 handle->interface = 0;
552 }
553}