blob: 148be1d78a1fd8a1df410255a73b2c9bcdd41703 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG USB
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
The Android Open Source Projectdd7bc332009-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 Albert7447dd02015-04-16 19:20:40 -070029#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <stdio.h>
31
Elliott Hughes4f713192015-12-04 22:00:26 -080032#include <android-base/logging.h>
33#include <android-base/stringprintf.h>
Siva Velusamy743883b2015-08-18 17:53:16 -070034
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include "adb.h"
Dan Albertdc0f8ec2015-02-25 10:26:17 -080036#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037
38#define DBG D
39
Elliott Hughes6e02c242015-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 Projectdd7bc332009-03-03 19:32:55 -080044static IONotificationPortRef notificationPort = 0;
Al Sutton8e01cc62014-11-21 12:21:12 +000045static io_iterator_t notificationIterator;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
47struct usb_handle
48{
Siva Velusamyd8b48a62015-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 Projectdd7bc332009-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 Zavin3fd82b82009-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 Velusamyd8b48a62015-08-13 08:48:06 -070065static usb_handle* CheckInterface(IOUSBInterfaceInterface190 **iface,
Dima Zavin3fd82b82009-05-08 18:25:58 -070066 UInt16 vendor, UInt16 product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
68static int
69InitUSB()
70{
71 CFMutableDictionaryRef matchingDict;
72 CFRunLoopSourceRef runLoopSource;
The Android Open Source Projectdd7bc332009-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 Sutton8e01cc62014-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 Projectdd7bc332009-03-03 19:32:55 -080085
Al Sutton8e01cc62014-11-21 12:21:12 +000086 if (!matchingDict) {
Siva Velusamy743883b2015-08-18 17:53:16 -070087 LOG(ERROR) << "Couldn't create USB matching dictionary.";
Al Sutton8e01cc62014-11-21 12:21:12 +000088 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089 }
90
Al Sutton8e01cc62014-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 Projectdd7bc332009-03-03 19:32:55 -0800107 return 0;
108}
109
110static void
Dima Zavin3fd82b82009-05-08 18:25:58 -0700111AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112{
113 kern_return_t kr;
114 io_service_t usbDevice;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700115 io_service_t usbInterface;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 IOCFPlugInInterface **plugInInterface = NULL;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700117 IOUSBInterfaceInterface220 **iface = NULL;
118 IOUSBDeviceInterface197 **dev = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 HRESULT result;
120 SInt32 score;
Elliott Hughes62077d32015-08-25 17:48:12 -0700121 uint32_t locationId;
Dan Albert7447dd02015-04-16 19:20:40 -0700122 UInt8 if_class, subclass, protocol;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 UInt16 vendor;
124 UInt16 product;
125 UInt8 serialIndex;
126 char serial[256];
Scott Andersone109d262012-04-20 11:21:14 -0700127 char devpathBuf[64];
128 char *devpath = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129
Dima Zavin3fd82b82009-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 Velusamy743883b2015-08-18 17:53:16 -0700138 LOG(ERROR) << "Unable to create an interface plug-in (" << std::hex << kr << ")";
Dima Zavin3fd82b82009-05-08 18:25:58 -0700139 continue;
140 }
141
142 //* This gets us the interface object
Dan Albert7447dd02015-04-16 19:20:40 -0700143 result = (*plugInInterface)->QueryInterface(
144 plugInInterface,
145 CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID*)&iface);
Dima Zavin3fd82b82009-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 Velusamy743883b2015-08-18 17:53:16 -0700149 LOG(ERROR) << "Couldn't query the interface (" << std::hex << result << ")";
Dima Zavin3fd82b82009-05-08 18:25:58 -0700150 continue;
151 }
152
Dan Albert7447dd02015-04-16 19:20:40 -0700153 kr = (*iface)->GetInterfaceClass(iface, &if_class);
Al Sutton8e01cc62014-11-21 12:21:12 +0000154 kr = (*iface)->GetInterfaceSubClass(iface, &subclass);
155 kr = (*iface)->GetInterfaceProtocol(iface, &protocol);
Dan Albert7447dd02015-04-16 19:20:40 -0700156 if(if_class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) {
Al Sutton8e01cc62014-11-21 12:21:12 +0000157 // Ignore non-ADB devices.
Siva Velusamy743883b2015-08-18 17:53:16 -0700158 LOG(DEBUG) << "Ignoring interface with incorrect class/subclass/protocol - " << if_class
159 << ", " << subclass << ", " << protocol;
Al Sutton8e01cc62014-11-21 12:21:12 +0000160 (*iface)->Release(iface);
161 continue;
162 }
163
Dima Zavin3fd82b82009-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 Velusamy743883b2015-08-18 17:53:16 -0700170 LOG(ERROR) << "Couldn't grab device from interface (" << std::hex << kr << ")";
Dima Zavin3fd82b82009-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 Projectdd7bc332009-03-03 19:32:55 -0800177 kr = IOCreatePlugInInterfaceForService(usbDevice,
178 kIOUSBDeviceUserClientTypeID,
179 kIOCFPlugInInterfaceID,
180 &plugInInterface, &score);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700181 //* only needed this to find the plugin
182 (void)IOObjectRelease(usbDevice);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700184 LOG(ERROR) << "Unable to create a device plug-in (" << std::hex << kr << ")";
Dima Zavin3fd82b82009-05-08 18:25:58 -0700185 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 }
187
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 result = (*plugInInterface)->QueryInterface(plugInInterface,
Dan Albert7447dd02015-04-16 19:20:40 -0700189 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID*)&dev);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700190 //* only needed this to query the plugin
191 (*plugInInterface)->Release(plugInInterface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 if (result || !dev) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700193 LOG(ERROR) << "Couldn't create a device interface (" << std::hex << result << ")";
Dima Zavin3fd82b82009-05-08 18:25:58 -0700194 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 }
196
Dima Zavin3fd82b82009-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 Projectdd7bc332009-03-03 19:32:55 -0800199 kr = (*dev)->GetDeviceVendor(dev, &vendor);
200 kr = (*dev)->GetDeviceProduct(dev, &product);
Scott Andersone109d262012-04-20 11:21:14 -0700201 kr = (*dev)->GetLocationID(dev, &locationId);
202 if (kr == 0) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700203 snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X", locationId);
Scott Andersone109d262012-04-20 11:21:14 -0700204 devpath = devpathBuf;
205 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
207
Siva Velusamy743883b2015-08-18 17:53:16 -0700208 if (serialIndex > 0) {
209 IOUSBDevRequest req;
210 UInt16 buffer[256];
211 UInt16 languages[128];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212
Siva Velusamy743883b2015-08-18 17:53:16 -0700213 memset(languages, 0, sizeof(languages));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214
Siva Velusamy743883b2015-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 Projectdd7bc332009-03-03 19:32:55 -0800223
Siva Velusamy743883b2015-08-18 17:53:16 -0700224 if (kr == kIOReturnSuccess && req.wLenDone > 0) {
Guang Zhu1a1f8182009-08-06 17:21:52 -0700225
Siva Velusamy743883b2015-08-18 17:53:16 -0700226 int langCount = (req.wLenDone - 2) / 2, lang;
Guang Zhu1a1f8182009-08-06 17:21:52 -0700227
Siva Velusamy743883b2015-08-18 17:53:16 -0700228 for (lang = 1; lang <= langCount; lang++) {
Guang Zhu1a1f8182009-08-06 17:21:52 -0700229
Siva Velusamy743883b2015-08-18 17:53:16 -0700230 memset(buffer, 0, sizeof(buffer));
231 memset(&req, 0, sizeof(req));
Guang Zhu1a1f8182009-08-06 17:21:52 -0700232
Siva Velusamy743883b2015-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 Zhu1a1f8182009-08-06 17:21:52 -0700241
Siva Velusamy743883b2015-08-18 17:53:16 -0700242 if (kr == kIOReturnSuccess && req.wLenDone > 0) {
243 int i, count;
Guang Zhu1a1f8182009-08-06 17:21:52 -0700244
Siva Velusamy743883b2015-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 Zavin3fd82b82009-05-08 18:25:58 -0700257 (*dev)->Release(dev);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258
Siva Velusamy743883b2015-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 Projectdd7bc332009-03-03 19:32:55 -0800261
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700262 usb_handle* handle = CheckInterface((IOUSBInterfaceInterface190**)iface,
Dima Zavin3fd82b82009-05-08 18:25:58 -0700263 vendor, product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264 if (handle == NULL) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700265 LOG(ERROR) << "Could not find device interface";
Dima Zavin3fd82b82009-05-08 18:25:58 -0700266 (*iface)->Release(iface);
267 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268 }
269
Siva Velusamy743883b2015-08-18 17:53:16 -0700270 LOG(DEBUG) << "AndroidDeviceAdded calling register_usb_transport";
Scott Andersone109d262012-04-20 11:21:14 -0700271 register_usb_transport(handle, (serial[0] ? serial : NULL), devpath, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272
Dima Zavin3fd82b82009-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 Projectdd7bc332009-03-03 19:32:55 -0800276 kr = IOServiceAddInterestNotification(notificationPort,
Dima Zavin3fd82b82009-05-08 18:25:58 -0700277 usbInterface,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 kIOGeneralInterest,
Dima Zavin3fd82b82009-05-08 18:25:58 -0700279 AndroidInterfaceNotify,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 handle,
281 &handle->usbNotification);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700282
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283 if (kIOReturnSuccess != kr) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700284 LOG(ERROR) << "Unable to create interest notification (" << std::hex << kr << ")";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 }
287}
288
289static void
Dima Zavin3fd82b82009-05-08 18:25:58 -0700290AndroidInterfaceNotify(void *refCon, io_service_t service, natural_t messageType, void *messageArgument)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291{
292 usb_handle *handle = (usb_handle *)refCon;
293
294 if (messageType == kIOMessageServiceIsTerminated) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700295 if (!handle) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700296 LOG(ERROR) << "NULL handle";
Dima Zavin3fd82b82009-05-08 18:25:58 -0700297 return;
298 }
Siva Velusamy743883b2015-08-18 17:53:16 -0700299 LOG(DEBUG) << "AndroidInterfaceNotify";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 IOObjectRelease(handle->usbNotification);
301 usb_kick(handle);
302 }
303}
304
Siva Velusamyd8b48a62015-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 Velusamy743883b2015-08-18 17:53:16 -0700311 LOG(ERROR) << "Could not clear pipe stall both ends: " << std::hex << rc;
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700312 return false;
313 }
314 return true;
315}
316
Dima Zavin3fd82b82009-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 Projectdd7bc332009-03-03 19:32:55 -0800319static usb_handle*
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700320CheckInterface(IOUSBInterfaceInterface190 **interface, UInt16 vendor, UInt16 product)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321{
Elliott Hughes2d4f8522015-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 Projectdd7bc332009-03-03 19:32:55 -0800326
Dima Zavin3fd82b82009-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 Projectdd7bc332009-03-03 19:32:55 -0800330 if (kr != kIOReturnSuccess) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700331 LOG(ERROR) << "Could not open interface: " << std::hex << kr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 return NULL;
333 }
334
Dima Zavin3fd82b82009-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 Velusamy743883b2015-08-18 17:53:16 -0700338 LOG(ERROR) << "Unable to get number of endpoints: " << std::hex << kr;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700339 goto err_get_num_ep;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 }
341
Dima Zavin3fd82b82009-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 Velusamy743883b2015-08-18 17:53:16 -0700346 LOG(ERROR) << "Unable to get interface class, subclass and protocol";
Dima Zavin3fd82b82009-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 Velusamyd8b48a62015-08-13 08:48:06 -0700352 if (!is_adb_interface(vendor, product, interfaceClass, interfaceSubClass, interfaceProtocol)) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700353 goto err_bad_adb_interface;
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700354 }
Dima Zavin3fd82b82009-05-08 18:25:58 -0700355
Dan Albert7447dd02015-04-16 19:20:40 -0700356 handle = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700357 if (handle == nullptr) goto err_bad_adb_interface;
Dima Zavin3fd82b82009-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 Hughes2d4f8522015-08-13 15:01:18 -0700361 for (endpoint = 1; endpoint <= interfaceNumEndpoints; endpoint++) {
Dima Zavin3fd82b82009-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 Velusamyd8b48a62015-08-13 08:48:06 -0700370 if (kr != kIOReturnSuccess) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700371 LOG(ERROR) << "FindDeviceInterface - could not get pipe properties: "
372 << std::hex << kr;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700373 goto err_get_pipe_props;
374 }
Siva Velusamyd8b48a62015-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 Zavin3fd82b82009-05-08 18:25:58 -0700389 }
390
391 handle->interface = interface;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 return handle;
Dima Zavin3fd82b82009-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 Projectdd7bc332009-03-03 19:32:55 -0800401}
402
403
404void* RunLoopThread(void* unused)
405{
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700406 adb_thread_setname("RunLoop");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407 InitUSB();
408
409 currentRunLoop = CFRunLoopGetCurrent();
410
411 // Signal the parent that we are running
412 adb_mutex_lock(&start_lock);
413 adb_cond_signal(&start_cond);
414 adb_mutex_unlock(&start_lock);
415
416 CFRunLoopRun();
417 currentRunLoop = 0;
418
Al Sutton8e01cc62014-11-21 12:21:12 +0000419 IOObjectRelease(notificationIterator);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 IONotificationPortDestroy(notificationPort);
421
Siva Velusamy743883b2015-08-18 17:53:16 -0700422 LOG(DEBUG) << "RunLoopThread done";
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700423 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424}
425
Dan Albertc89e0cc2015-05-08 16:13:53 -0700426static void usb_cleanup() {
Siva Velusamy743883b2015-08-18 17:53:16 -0700427 LOG(DEBUG) << "usb_cleanup";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700428 close_usb_devices();
429 if (currentRunLoop)
430 CFRunLoopStop(currentRunLoop);
431}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700433void usb_init() {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700434 static bool initialized = false;
435 if (!initialized) {
436 atexit(usb_cleanup);
437
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 adb_mutex_init(&start_lock, NULL);
439 adb_cond_init(&start_cond, NULL);
440
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700441 if (!adb_thread_create(RunLoopThread, nullptr)) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700442 fatal_errno("cannot create RunLoop thread");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700443 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444
445 // Wait for initialization to finish
446 adb_mutex_lock(&start_lock);
447 adb_cond_wait(&start_cond, &start_lock);
448 adb_mutex_unlock(&start_lock);
449
450 adb_mutex_destroy(&start_lock);
451 adb_cond_destroy(&start_cond);
452
Dan Albertc89e0cc2015-05-08 16:13:53 -0700453 initialized = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454 }
455}
456
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457int usb_write(usb_handle *handle, const void *buf, int len)
458{
459 IOReturn result;
460
461 if (!len)
462 return 0;
463
464 if (!handle)
465 return -1;
466
467 if (NULL == handle->interface) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700468 LOG(ERROR) << "usb_write interface was null";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 return -1;
470 }
471
472 if (0 == handle->bulkOut) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700473 LOG(ERROR) << "bulkOut endpoint not assigned";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 return -1;
475 }
476
477 result =
Siva Velusamy743883b2015-08-18 17:53:16 -0700478 (*handle->interface)->WritePipe(handle->interface, handle->bulkOut, (void *)buf, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479
480 if ((result == 0) && (handle->zero_mask)) {
481 /* we need 0-markers and our transfer */
482 if(!(len & handle->zero_mask)) {
483 result =
484 (*handle->interface)->WritePipe(
485 handle->interface, handle->bulkOut, (void *)buf, 0);
486 }
487 }
488
489 if (0 == result)
490 return 0;
491
Siva Velusamy743883b2015-08-18 17:53:16 -0700492 LOG(ERROR) << "usb_write failed with status: " << std::hex << result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493 return -1;
494}
495
496int usb_read(usb_handle *handle, void *buf, int len)
497{
498 IOReturn result;
499 UInt32 numBytes = len;
500
501 if (!len) {
502 return 0;
503 }
504
505 if (!handle) {
506 return -1;
507 }
508
509 if (NULL == handle->interface) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700510 LOG(ERROR) << "usb_read interface was null";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 return -1;
512 }
513
514 if (0 == handle->bulkIn) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700515 LOG(ERROR) << "bulkIn endpoint not assigned";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 return -1;
517 }
518
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700519 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700521 if (kIOUSBPipeStalled == result) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700522 LOG(ERROR) << "Pipe stalled, clearing stall.\n";
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700523 (*handle->interface)->ClearPipeStall(handle->interface, handle->bulkIn);
524 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
525 }
526
527 if (kIOReturnSuccess == result)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528 return 0;
529 else {
Siva Velusamy743883b2015-08-18 17:53:16 -0700530 LOG(ERROR) << "usb_read failed with status: " << std::hex << result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 }
532
533 return -1;
534}
535
536int usb_close(usb_handle *handle)
537{
538 return 0;
539}
540
541void usb_kick(usb_handle *handle)
542{
Siva Velusamy743883b2015-08-18 17:53:16 -0700543 LOG(INFO) << "Kicking handle";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544 /* release the interface */
Dima Zavin3fd82b82009-05-08 18:25:58 -0700545 if (!handle)
546 return;
547
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548 if (handle->interface)
549 {
550 (*handle->interface)->USBInterfaceClose(handle->interface);
551 (*handle->interface)->Release(handle->interface);
552 handle->interface = 0;
553 }
554}