blob: afa08c1e3c8bff6939324cbd70528b6e96d38cc6 [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
Dan Albert33134262015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_USB
18
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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include "adb.h"
Dan Albertdc0f8ec2015-02-25 10:26:17 -080033#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
35#define DBG D
36
Elliott Hughes6e02c242015-08-03 18:07:12 -070037// There's no strerror equivalent for the errors returned by IOKit.
38// https://developer.apple.com/library/mac/documentation/DeviceDrivers/Conceptual/AccessingHardware/AH_Handling_Errors/AH_Handling_Errors.html
39// Search the web for "IOReturn.h" to find a complete up to date list.
40
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041static IONotificationPortRef notificationPort = 0;
Al Sutton8e01cc62014-11-21 12:21:12 +000042static io_iterator_t notificationIterator;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
44struct usb_handle
45{
Siva Velusamyd8b48a62015-08-13 08:48:06 -070046 UInt8 bulkIn;
47 UInt8 bulkOut;
48 IOUSBInterfaceInterface190** interface;
49 io_object_t usbNotification;
50 unsigned int zero_mask;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051};
52
53static CFRunLoopRef currentRunLoop = 0;
54static pthread_mutex_t start_lock;
55static pthread_cond_t start_cond;
56
57
Dima Zavin3fd82b82009-05-08 18:25:58 -070058static void AndroidInterfaceAdded(void *refCon, io_iterator_t iterator);
59static void AndroidInterfaceNotify(void *refCon, io_iterator_t iterator,
60 natural_t messageType,
61 void *messageArgument);
Siva Velusamyd8b48a62015-08-13 08:48:06 -070062static usb_handle* CheckInterface(IOUSBInterfaceInterface190 **iface,
Dima Zavin3fd82b82009-05-08 18:25:58 -070063 UInt16 vendor, UInt16 product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
65static int
66InitUSB()
67{
68 CFMutableDictionaryRef matchingDict;
69 CFRunLoopSourceRef runLoopSource;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
71 //* To set up asynchronous notifications, create a notification port and
72 //* add its run loop event source to the program's run loop
73 notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
74 runLoopSource = IONotificationPortGetRunLoopSource(notificationPort);
75 CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
76
Al Sutton8e01cc62014-11-21 12:21:12 +000077 //* Create our matching dictionary to find the Android device's
78 //* adb interface
79 //* IOServiceAddMatchingNotification consumes the reference, so we do
80 //* not need to release this
81 matchingDict = IOServiceMatching(kIOUSBInterfaceClassName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082
Al Sutton8e01cc62014-11-21 12:21:12 +000083 if (!matchingDict) {
84 DBG("ERR: Couldn't create USB matching dictionary.\n");
85 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086 }
87
Al Sutton8e01cc62014-11-21 12:21:12 +000088 //* We have to get notifications for all potential candidates and test them
89 //* at connection time because the matching rules don't allow for a
90 //* USB interface class of 0xff for class+subclass+protocol matches
91 //* See https://developer.apple.com/library/mac/qa/qa1076/_index.html
92 IOServiceAddMatchingNotification(
93 notificationPort,
94 kIOFirstMatchNotification,
95 matchingDict,
96 AndroidInterfaceAdded,
97 NULL,
98 &notificationIterator);
99
100 //* Iterate over set of matching interfaces to access already-present
101 //* devices and to arm the notification
102 AndroidInterfaceAdded(NULL, notificationIterator);
103
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 return 0;
105}
106
107static void
Dima Zavin3fd82b82009-05-08 18:25:58 -0700108AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109{
110 kern_return_t kr;
111 io_service_t usbDevice;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700112 io_service_t usbInterface;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 IOCFPlugInInterface **plugInInterface = NULL;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700114 IOUSBInterfaceInterface220 **iface = NULL;
115 IOUSBDeviceInterface197 **dev = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 HRESULT result;
117 SInt32 score;
Scott Andersone109d262012-04-20 11:21:14 -0700118 UInt32 locationId;
Dan Albert7447dd02015-04-16 19:20:40 -0700119 UInt8 if_class, subclass, protocol;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 UInt16 vendor;
121 UInt16 product;
122 UInt8 serialIndex;
123 char serial[256];
Scott Andersone109d262012-04-20 11:21:14 -0700124 char devpathBuf[64];
125 char *devpath = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126
Dima Zavin3fd82b82009-05-08 18:25:58 -0700127 while ((usbInterface = IOIteratorNext(iterator))) {
128 //* Create an intermediate interface plugin
129 kr = IOCreatePlugInInterfaceForService(usbInterface,
130 kIOUSBInterfaceUserClientTypeID,
131 kIOCFPlugInInterfaceID,
132 &plugInInterface, &score);
133 IOObjectRelease(usbInterface);
134 if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
135 DBG("ERR: Unable to create an interface plug-in (%08x)\n", kr);
136 continue;
137 }
138
139 //* This gets us the interface object
Dan Albert7447dd02015-04-16 19:20:40 -0700140 result = (*plugInInterface)->QueryInterface(
141 plugInInterface,
142 CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID*)&iface);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700143 //* We only needed the plugin to get the interface, so discard it
144 (*plugInInterface)->Release(plugInInterface);
145 if (result || !iface) {
146 DBG("ERR: Couldn't query the interface (%08x)\n", (int) result);
147 continue;
148 }
149
Dan Albert7447dd02015-04-16 19:20:40 -0700150 kr = (*iface)->GetInterfaceClass(iface, &if_class);
Al Sutton8e01cc62014-11-21 12:21:12 +0000151 kr = (*iface)->GetInterfaceSubClass(iface, &subclass);
152 kr = (*iface)->GetInterfaceProtocol(iface, &protocol);
Dan Albert7447dd02015-04-16 19:20:40 -0700153 if(if_class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) {
Al Sutton8e01cc62014-11-21 12:21:12 +0000154 // Ignore non-ADB devices.
Dan Albert7447dd02015-04-16 19:20:40 -0700155 DBG("Ignoring interface with incorrect class/subclass/protocol - %d, %d, %d\n", if_class, subclass, protocol);
Al Sutton8e01cc62014-11-21 12:21:12 +0000156 (*iface)->Release(iface);
157 continue;
158 }
159
Dima Zavin3fd82b82009-05-08 18:25:58 -0700160 //* this gets us an ioservice, with which we will find the actual
161 //* device; after getting a plugin, and querying the interface, of
162 //* course.
163 //* Gotta love OS X
164 kr = (*iface)->GetDevice(iface, &usbDevice);
165 if (kIOReturnSuccess != kr || !usbDevice) {
166 DBG("ERR: Couldn't grab device from interface (%08x)\n", kr);
167 continue;
168 }
169
170 plugInInterface = NULL;
171 score = 0;
172 //* create an intermediate device plugin
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 kr = IOCreatePlugInInterfaceForService(usbDevice,
174 kIOUSBDeviceUserClientTypeID,
175 kIOCFPlugInInterfaceID,
176 &plugInInterface, &score);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700177 //* only needed this to find the plugin
178 (void)IOObjectRelease(usbDevice);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700180 DBG("ERR: Unable to create a device plug-in (%08x)\n", kr);
181 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 }
183
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 result = (*plugInInterface)->QueryInterface(plugInInterface,
Dan Albert7447dd02015-04-16 19:20:40 -0700185 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID*)&dev);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700186 //* only needed this to query the plugin
187 (*plugInInterface)->Release(plugInInterface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 if (result || !dev) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700189 DBG("ERR: Couldn't create a device interface (%08x)\n",
190 (int) result);
191 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 }
193
Dima Zavin3fd82b82009-05-08 18:25:58 -0700194 //* Now after all that, we actually have a ref to the device and
195 //* the interface that matched our criteria
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 kr = (*dev)->GetDeviceVendor(dev, &vendor);
197 kr = (*dev)->GetDeviceProduct(dev, &product);
Scott Andersone109d262012-04-20 11:21:14 -0700198 kr = (*dev)->GetLocationID(dev, &locationId);
199 if (kr == 0) {
Ying Wang42a809b2014-08-14 15:50:13 -0700200 snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X",
201 (unsigned int)locationId);
Scott Andersone109d262012-04-20 11:21:14 -0700202 devpath = devpathBuf;
203 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
205
Guang Zhu1a1f8182009-08-06 17:21:52 -0700206 if (serialIndex > 0) {
207 IOUSBDevRequest req;
208 UInt16 buffer[256];
209 UInt16 languages[128];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210
Guang Zhu1a1f8182009-08-06 17:21:52 -0700211 memset(languages, 0, sizeof(languages));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212
Guang Zhu1a1f8182009-08-06 17:21:52 -0700213 req.bmRequestType =
214 USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
215 req.bRequest = kUSBRqGetDescriptor;
216 req.wValue = (kUSBStringDesc << 8) | 0;
217 req.wIndex = 0;
218 req.pData = languages;
219 req.wLength = sizeof(languages);
220 kr = (*dev)->DeviceRequest(dev, &req);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221
Guang Zhu1a1f8182009-08-06 17:21:52 -0700222 if (kr == kIOReturnSuccess && req.wLenDone > 0) {
223
224 int langCount = (req.wLenDone - 2) / 2, lang;
225
226 for (lang = 1; lang <= langCount; lang++) {
227
228 memset(buffer, 0, sizeof(buffer));
229 memset(&req, 0, sizeof(req));
230
231 req.bmRequestType =
232 USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
233 req.bRequest = kUSBRqGetDescriptor;
234 req.wValue = (kUSBStringDesc << 8) | serialIndex;
235 req.wIndex = languages[lang];
236 req.pData = buffer;
237 req.wLength = sizeof(buffer);
238 kr = (*dev)->DeviceRequest(dev, &req);
239
240 if (kr == kIOReturnSuccess && req.wLenDone > 0) {
241 int i, count;
242
243 // skip first word, and copy the rest to the serial string,
244 // changing shorts to bytes.
245 count = (req.wLenDone - 1) / 2;
246 for (i = 0; i < count; i++)
247 serial[i] = buffer[i + 1];
248 serial[i] = 0;
249 break;
250 }
251 }
252 }
253 }
Dima Zavin3fd82b82009-05-08 18:25:58 -0700254 (*dev)->Release(dev);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255
Dima Zavin3fd82b82009-05-08 18:25:58 -0700256 DBG("INFO: Found vid=%04x pid=%04x serial=%s\n", vendor, product,
257 serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700259 usb_handle* handle = CheckInterface((IOUSBInterfaceInterface190**)iface,
Dima Zavin3fd82b82009-05-08 18:25:58 -0700260 vendor, product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 if (handle == NULL) {
262 DBG("ERR: Could not find device interface: %08x\n", kr);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700263 (*iface)->Release(iface);
264 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 }
266
267 DBG("AndroidDeviceAdded calling register_usb_transport\n");
Scott Andersone109d262012-04-20 11:21:14 -0700268 register_usb_transport(handle, (serial[0] ? serial : NULL), devpath, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269
Dima Zavin3fd82b82009-05-08 18:25:58 -0700270 // Register for an interest notification of this device being removed.
271 // Pass the reference to our private data as the refCon for the
272 // notification.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273 kr = IOServiceAddInterestNotification(notificationPort,
Dima Zavin3fd82b82009-05-08 18:25:58 -0700274 usbInterface,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 kIOGeneralInterest,
Dima Zavin3fd82b82009-05-08 18:25:58 -0700276 AndroidInterfaceNotify,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 handle,
278 &handle->usbNotification);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700279
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 if (kIOReturnSuccess != kr) {
281 DBG("ERR: Unable to create interest notification (%08x)\n", kr);
282 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283 }
284}
285
286static void
Dima Zavin3fd82b82009-05-08 18:25:58 -0700287AndroidInterfaceNotify(void *refCon, io_service_t service, natural_t messageType, void *messageArgument)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288{
289 usb_handle *handle = (usb_handle *)refCon;
290
291 if (messageType == kIOMessageServiceIsTerminated) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700292 if (!handle) {
293 DBG("ERR: NULL handle\n");
294 return;
295 }
296 DBG("AndroidInterfaceNotify\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 IOObjectRelease(handle->usbNotification);
298 usb_kick(handle);
299 }
300}
301
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700302// Used to clear both the endpoints before starting.
303// When adb quits, we might clear the host endpoint but not the device.
304// So we make sure both sides are clear before starting up.
305static bool ClearPipeStallBothEnds(IOUSBInterfaceInterface190** interface, UInt8 bulkEp) {
306 IOReturn rc = (*interface)->ClearPipeStallBothEnds(interface, bulkEp);
307 if (rc != kIOReturnSuccess) {
308 DBG("ERR: Could not clear pipe: (%08x)\n", rc);
309 return false;
310 }
311 return true;
312}
313
Dima Zavin3fd82b82009-05-08 18:25:58 -0700314//* TODO: simplify this further since we only register to get ADB interface
315//* subclass+protocol events
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316static usb_handle*
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700317CheckInterface(IOUSBInterfaceInterface190 **interface, UInt16 vendor, UInt16 product)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318{
Elliott Hughes2d4f8522015-08-13 15:01:18 -0700319 usb_handle* handle;
320 IOReturn kr;
321 UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol;
322 UInt8 endpoint;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323
Dima Zavin3fd82b82009-05-08 18:25:58 -0700324 //* Now open the interface. This will cause the pipes associated with
325 //* the endpoints in the interface descriptor to be instantiated
326 kr = (*interface)->USBInterfaceOpen(interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 if (kr != kIOReturnSuccess) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700328 DBG("ERR: Could not open interface: (%08x)\n", kr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 return NULL;
330 }
331
Dima Zavin3fd82b82009-05-08 18:25:58 -0700332 //* Get the number of endpoints associated with this interface
333 kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
334 if (kr != kIOReturnSuccess) {
335 DBG("ERR: Unable to get number of endpoints: (%08x)\n", kr);
336 goto err_get_num_ep;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 }
338
Dima Zavin3fd82b82009-05-08 18:25:58 -0700339 //* Get interface class, subclass and protocol
340 if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess ||
341 (*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess ||
342 (*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess) {
343 DBG("ERR: Unable to get interface class, subclass and protocol\n");
344 goto err_get_interface_class;
345 }
346
347 //* check to make sure interface class, subclass and protocol match ADB
348 //* avoid opening mass storage endpoints
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700349 if (!is_adb_interface(vendor, product, interfaceClass, interfaceSubClass, interfaceProtocol)) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700350 goto err_bad_adb_interface;
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700351 }
Dima Zavin3fd82b82009-05-08 18:25:58 -0700352
Dan Albert7447dd02015-04-16 19:20:40 -0700353 handle = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700354 if (handle == nullptr) goto err_bad_adb_interface;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700355
356 //* Iterate over the endpoints for this interface and find the first
357 //* bulk in/out pipes available. These will be our read/write pipes.
Elliott Hughes2d4f8522015-08-13 15:01:18 -0700358 for (endpoint = 1; endpoint <= interfaceNumEndpoints; endpoint++) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700359 UInt8 transferType;
360 UInt16 maxPacketSize;
361 UInt8 interval;
362 UInt8 number;
363 UInt8 direction;
364
365 kr = (*interface)->GetPipeProperties(interface, endpoint, &direction,
366 &number, &transferType, &maxPacketSize, &interval);
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700367 if (kr != kIOReturnSuccess) {
Elliott Hughes6e02c242015-08-03 18:07:12 -0700368 DBG("ERR: FindDeviceInterface - could not get pipe properties (%08x)\n", kr);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700369 goto err_get_pipe_props;
370 }
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700371
372 if (kUSBBulk != transferType) continue;
373
374 if (kUSBIn == direction) {
375 handle->bulkIn = endpoint;
376 if (!ClearPipeStallBothEnds(interface, handle->bulkIn)) goto err_get_pipe_props;
377 }
378
379 if (kUSBOut == direction) {
380 handle->bulkOut = endpoint;
381 if (!ClearPipeStallBothEnds(interface, handle->bulkOut)) goto err_get_pipe_props;
382 }
383
384 handle->zero_mask = maxPacketSize - 1;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700385 }
386
387 handle->interface = interface;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 return handle;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700389
390err_get_pipe_props:
391 free(handle);
392err_bad_adb_interface:
393err_get_interface_class:
394err_get_num_ep:
395 (*interface)->USBInterfaceClose(interface);
396 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397}
398
399
400void* RunLoopThread(void* unused)
401{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 InitUSB();
403
404 currentRunLoop = CFRunLoopGetCurrent();
405
406 // Signal the parent that we are running
407 adb_mutex_lock(&start_lock);
408 adb_cond_signal(&start_cond);
409 adb_mutex_unlock(&start_lock);
410
411 CFRunLoopRun();
412 currentRunLoop = 0;
413
Al Sutton8e01cc62014-11-21 12:21:12 +0000414 IOObjectRelease(notificationIterator);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 IONotificationPortDestroy(notificationPort);
416
417 DBG("RunLoopThread done\n");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700418 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419}
420
Dan Albertc89e0cc2015-05-08 16:13:53 -0700421static void usb_cleanup() {
422 DBG("usb_cleanup\n");
423 close_usb_devices();
424 if (currentRunLoop)
425 CFRunLoopStop(currentRunLoop);
426}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700428void usb_init() {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700429 static bool initialized = false;
430 if (!initialized) {
431 atexit(usb_cleanup);
432
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433 adb_mutex_init(&start_lock, NULL);
434 adb_cond_init(&start_cond, NULL);
435
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700436 if (!adb_thread_create(RunLoopThread, nullptr)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 fatal_errno("cannot create input thread");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700438 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439
440 // Wait for initialization to finish
441 adb_mutex_lock(&start_lock);
442 adb_cond_wait(&start_cond, &start_lock);
443 adb_mutex_unlock(&start_lock);
444
445 adb_mutex_destroy(&start_lock);
446 adb_cond_destroy(&start_cond);
447
Dan Albertc89e0cc2015-05-08 16:13:53 -0700448 initialized = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 }
450}
451
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452int usb_write(usb_handle *handle, const void *buf, int len)
453{
454 IOReturn result;
455
456 if (!len)
457 return 0;
458
459 if (!handle)
460 return -1;
461
462 if (NULL == handle->interface) {
463 DBG("ERR: usb_write interface was null\n");
464 return -1;
465 }
466
467 if (0 == handle->bulkOut) {
468 DBG("ERR: bulkOut endpoint not assigned\n");
469 return -1;
470 }
471
472 result =
473 (*handle->interface)->WritePipe(
474 handle->interface, handle->bulkOut, (void *)buf, len);
475
476 if ((result == 0) && (handle->zero_mask)) {
477 /* we need 0-markers and our transfer */
478 if(!(len & handle->zero_mask)) {
479 result =
480 (*handle->interface)->WritePipe(
481 handle->interface, handle->bulkOut, (void *)buf, 0);
482 }
483 }
484
485 if (0 == result)
486 return 0;
487
488 DBG("ERR: usb_write failed with status %d\n", result);
489 return -1;
490}
491
492int usb_read(usb_handle *handle, void *buf, int len)
493{
494 IOReturn result;
495 UInt32 numBytes = len;
496
497 if (!len) {
498 return 0;
499 }
500
501 if (!handle) {
502 return -1;
503 }
504
505 if (NULL == handle->interface) {
506 DBG("ERR: usb_read interface was null\n");
507 return -1;
508 }
509
510 if (0 == handle->bulkIn) {
511 DBG("ERR: bulkIn endpoint not assigned\n");
512 return -1;
513 }
514
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700515 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700517 if (kIOUSBPipeStalled == result) {
518 DBG(" Pipe stalled, clearing stall.\n");
519 (*handle->interface)->ClearPipeStall(handle->interface, handle->bulkIn);
520 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
521 }
522
523 if (kIOReturnSuccess == result)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524 return 0;
525 else {
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700526 DBG("ERR: usb_read failed with status %x\n", result);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 }
528
529 return -1;
530}
531
532int usb_close(usb_handle *handle)
533{
534 return 0;
535}
536
537void usb_kick(usb_handle *handle)
538{
539 /* release the interface */
Dima Zavin3fd82b82009-05-08 18:25:58 -0700540 if (!handle)
541 return;
542
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 if (handle->interface)
544 {
545 (*handle->interface)->USBInterfaceClose(handle->interface);
546 (*handle->interface)->Release(handle->interface);
547 handle->interface = 0;
548 }
549}