blob: 939f319ab56fb515a75de5d82f158937f41b54c3 [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{
46 UInt8 bulkIn;
47 UInt8 bulkOut;
48 IOUSBInterfaceInterface **interface;
49 io_object_t usbNotification;
50 unsigned int zero_mask;
51};
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);
62static usb_handle* CheckInterface(IOUSBInterfaceInterface **iface,
63 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
Dima Zavin3fd82b82009-05-08 18:25:58 -0700259 usb_handle* handle = CheckInterface((IOUSBInterfaceInterface**)iface,
260 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
Dima Zavin3fd82b82009-05-08 18:25:58 -0700302//* TODO: simplify this further since we only register to get ADB interface
303//* subclass+protocol events
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304static usb_handle*
Dima Zavin3fd82b82009-05-08 18:25:58 -0700305CheckInterface(IOUSBInterfaceInterface **interface, UInt16 vendor, UInt16 product)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306{
307 usb_handle* handle = NULL;
308 IOReturn kr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700310 UInt8 endpoint;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312
Dima Zavin3fd82b82009-05-08 18:25:58 -0700313 //* Now open the interface. This will cause the pipes associated with
314 //* the endpoints in the interface descriptor to be instantiated
315 kr = (*interface)->USBInterfaceOpen(interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 if (kr != kIOReturnSuccess) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700317 DBG("ERR: Could not open interface: (%08x)\n", kr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318 return NULL;
319 }
320
Dima Zavin3fd82b82009-05-08 18:25:58 -0700321 //* Get the number of endpoints associated with this interface
322 kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
323 if (kr != kIOReturnSuccess) {
324 DBG("ERR: Unable to get number of endpoints: (%08x)\n", kr);
325 goto err_get_num_ep;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 }
327
Dima Zavin3fd82b82009-05-08 18:25:58 -0700328 //* Get interface class, subclass and protocol
329 if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess ||
330 (*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess ||
331 (*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess) {
332 DBG("ERR: Unable to get interface class, subclass and protocol\n");
333 goto err_get_interface_class;
334 }
335
336 //* check to make sure interface class, subclass and protocol match ADB
337 //* avoid opening mass storage endpoints
338 if (!is_adb_interface(vendor, product, interfaceClass,
339 interfaceSubClass, interfaceProtocol))
340 goto err_bad_adb_interface;
341
Dan Albert7447dd02015-04-16 19:20:40 -0700342 handle = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700343 if (handle == nullptr) goto err_bad_adb_interface;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700344
345 //* Iterate over the endpoints for this interface and find the first
346 //* bulk in/out pipes available. These will be our read/write pipes.
347 for (endpoint = 0; endpoint <= interfaceNumEndpoints; endpoint++) {
348 UInt8 transferType;
349 UInt16 maxPacketSize;
350 UInt8 interval;
351 UInt8 number;
352 UInt8 direction;
353
354 kr = (*interface)->GetPipeProperties(interface, endpoint, &direction,
355 &number, &transferType, &maxPacketSize, &interval);
356
357 if (kIOReturnSuccess == kr) {
358 if (kUSBBulk != transferType)
359 continue;
360
361 if (kUSBIn == direction)
362 handle->bulkIn = endpoint;
363
364 if (kUSBOut == direction)
365 handle->bulkOut = endpoint;
366
367 handle->zero_mask = maxPacketSize - 1;
368 } else {
Elliott Hughes6e02c242015-08-03 18:07:12 -0700369 DBG("ERR: FindDeviceInterface - could not get pipe properties (%08x)\n", kr);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700370 goto err_get_pipe_props;
371 }
372 }
373
374 handle->interface = interface;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 return handle;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700376
377err_get_pipe_props:
378 free(handle);
379err_bad_adb_interface:
380err_get_interface_class:
381err_get_num_ep:
382 (*interface)->USBInterfaceClose(interface);
383 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384}
385
386
387void* RunLoopThread(void* unused)
388{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 InitUSB();
390
391 currentRunLoop = CFRunLoopGetCurrent();
392
393 // Signal the parent that we are running
394 adb_mutex_lock(&start_lock);
395 adb_cond_signal(&start_cond);
396 adb_mutex_unlock(&start_lock);
397
398 CFRunLoopRun();
399 currentRunLoop = 0;
400
Al Sutton8e01cc62014-11-21 12:21:12 +0000401 IOObjectRelease(notificationIterator);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 IONotificationPortDestroy(notificationPort);
403
404 DBG("RunLoopThread done\n");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700405 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406}
407
Dan Albertc89e0cc2015-05-08 16:13:53 -0700408static void usb_cleanup() {
409 DBG("usb_cleanup\n");
410 close_usb_devices();
411 if (currentRunLoop)
412 CFRunLoopStop(currentRunLoop);
413}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700415void usb_init() {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700416 static bool initialized = false;
417 if (!initialized) {
418 atexit(usb_cleanup);
419
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 adb_mutex_init(&start_lock, NULL);
421 adb_cond_init(&start_cond, NULL);
422
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700423 if (!adb_thread_create(RunLoopThread, nullptr)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 fatal_errno("cannot create input thread");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700425 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426
427 // Wait for initialization to finish
428 adb_mutex_lock(&start_lock);
429 adb_cond_wait(&start_cond, &start_lock);
430 adb_mutex_unlock(&start_lock);
431
432 adb_mutex_destroy(&start_lock);
433 adb_cond_destroy(&start_cond);
434
Dan Albertc89e0cc2015-05-08 16:13:53 -0700435 initialized = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 }
437}
438
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439int usb_write(usb_handle *handle, const void *buf, int len)
440{
441 IOReturn result;
442
443 if (!len)
444 return 0;
445
446 if (!handle)
447 return -1;
448
449 if (NULL == handle->interface) {
450 DBG("ERR: usb_write interface was null\n");
451 return -1;
452 }
453
454 if (0 == handle->bulkOut) {
455 DBG("ERR: bulkOut endpoint not assigned\n");
456 return -1;
457 }
458
459 result =
460 (*handle->interface)->WritePipe(
461 handle->interface, handle->bulkOut, (void *)buf, len);
462
463 if ((result == 0) && (handle->zero_mask)) {
464 /* we need 0-markers and our transfer */
465 if(!(len & handle->zero_mask)) {
466 result =
467 (*handle->interface)->WritePipe(
468 handle->interface, handle->bulkOut, (void *)buf, 0);
469 }
470 }
471
472 if (0 == result)
473 return 0;
474
475 DBG("ERR: usb_write failed with status %d\n", result);
476 return -1;
477}
478
479int usb_read(usb_handle *handle, void *buf, int len)
480{
481 IOReturn result;
482 UInt32 numBytes = len;
483
484 if (!len) {
485 return 0;
486 }
487
488 if (!handle) {
489 return -1;
490 }
491
492 if (NULL == handle->interface) {
493 DBG("ERR: usb_read interface was null\n");
494 return -1;
495 }
496
497 if (0 == handle->bulkIn) {
498 DBG("ERR: bulkIn endpoint not assigned\n");
499 return -1;
500 }
501
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700502 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700504 if (kIOUSBPipeStalled == result) {
505 DBG(" Pipe stalled, clearing stall.\n");
506 (*handle->interface)->ClearPipeStall(handle->interface, handle->bulkIn);
507 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
508 }
509
510 if (kIOReturnSuccess == result)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 return 0;
512 else {
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700513 DBG("ERR: usb_read failed with status %x\n", result);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514 }
515
516 return -1;
517}
518
519int usb_close(usb_handle *handle)
520{
521 return 0;
522}
523
524void usb_kick(usb_handle *handle)
525{
526 /* release the interface */
Dima Zavin3fd82b82009-05-08 18:25:58 -0700527 if (!handle)
528 return;
529
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 if (handle->interface)
531 {
532 (*handle->interface)->USBInterfaceClose(handle->interface);
533 (*handle->interface)->Release(handle->interface);
534 handle->interface = 0;
535 }
536}