blob: b3b3a2f2f136b09e94926d688504cf2d0d0bb52e [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{
319 usb_handle* handle = NULL;
320 IOReturn kr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321 UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700322 UInt8 endpoint;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324
Dima Zavin3fd82b82009-05-08 18:25:58 -0700325 //* Now open the interface. This will cause the pipes associated with
326 //* the endpoints in the interface descriptor to be instantiated
327 kr = (*interface)->USBInterfaceOpen(interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 if (kr != kIOReturnSuccess) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700329 DBG("ERR: Could not open interface: (%08x)\n", kr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 return NULL;
331 }
332
Dima Zavin3fd82b82009-05-08 18:25:58 -0700333 //* Get the number of endpoints associated with this interface
334 kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
335 if (kr != kIOReturnSuccess) {
336 DBG("ERR: Unable to get number of endpoints: (%08x)\n", kr);
337 goto err_get_num_ep;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 }
339
Dima Zavin3fd82b82009-05-08 18:25:58 -0700340 //* Get interface class, subclass and protocol
341 if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess ||
342 (*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess ||
343 (*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess) {
344 DBG("ERR: Unable to get interface class, subclass and protocol\n");
345 goto err_get_interface_class;
346 }
347
348 //* check to make sure interface class, subclass and protocol match ADB
349 //* avoid opening mass storage endpoints
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700350 if (!is_adb_interface(vendor, product, interfaceClass, interfaceSubClass, interfaceProtocol)) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700351 goto err_bad_adb_interface;
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700352 }
Dima Zavin3fd82b82009-05-08 18:25:58 -0700353
Dan Albert7447dd02015-04-16 19:20:40 -0700354 handle = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700355 if (handle == nullptr) goto err_bad_adb_interface;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700356
357 //* Iterate over the endpoints for this interface and find the first
358 //* bulk in/out pipes available. These will be our read/write pipes.
359 for (endpoint = 0; endpoint <= interfaceNumEndpoints; endpoint++) {
360 UInt8 transferType;
361 UInt16 maxPacketSize;
362 UInt8 interval;
363 UInt8 number;
364 UInt8 direction;
365
366 kr = (*interface)->GetPipeProperties(interface, endpoint, &direction,
367 &number, &transferType, &maxPacketSize, &interval);
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700368 if (kr != kIOReturnSuccess) {
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 }
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700372
373 if (kUSBBulk != transferType) continue;
374
375 if (kUSBIn == direction) {
376 handle->bulkIn = endpoint;
377 if (!ClearPipeStallBothEnds(interface, handle->bulkIn)) goto err_get_pipe_props;
378 }
379
380 if (kUSBOut == direction) {
381 handle->bulkOut = endpoint;
382 if (!ClearPipeStallBothEnds(interface, handle->bulkOut)) goto err_get_pipe_props;
383 }
384
385 handle->zero_mask = maxPacketSize - 1;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700386 }
387
388 handle->interface = interface;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 return handle;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700390
391err_get_pipe_props:
392 free(handle);
393err_bad_adb_interface:
394err_get_interface_class:
395err_get_num_ep:
396 (*interface)->USBInterfaceClose(interface);
397 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398}
399
400
401void* RunLoopThread(void* unused)
402{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403 InitUSB();
404
405 currentRunLoop = CFRunLoopGetCurrent();
406
407 // Signal the parent that we are running
408 adb_mutex_lock(&start_lock);
409 adb_cond_signal(&start_cond);
410 adb_mutex_unlock(&start_lock);
411
412 CFRunLoopRun();
413 currentRunLoop = 0;
414
Al Sutton8e01cc62014-11-21 12:21:12 +0000415 IOObjectRelease(notificationIterator);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 IONotificationPortDestroy(notificationPort);
417
418 DBG("RunLoopThread done\n");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700419 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420}
421
Dan Albertc89e0cc2015-05-08 16:13:53 -0700422static void usb_cleanup() {
423 DBG("usb_cleanup\n");
424 close_usb_devices();
425 if (currentRunLoop)
426 CFRunLoopStop(currentRunLoop);
427}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700429void usb_init() {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700430 static bool initialized = false;
431 if (!initialized) {
432 atexit(usb_cleanup);
433
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434 adb_mutex_init(&start_lock, NULL);
435 adb_cond_init(&start_cond, NULL);
436
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700437 if (!adb_thread_create(RunLoopThread, nullptr)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 fatal_errno("cannot create input thread");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700439 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440
441 // Wait for initialization to finish
442 adb_mutex_lock(&start_lock);
443 adb_cond_wait(&start_cond, &start_lock);
444 adb_mutex_unlock(&start_lock);
445
446 adb_mutex_destroy(&start_lock);
447 adb_cond_destroy(&start_cond);
448
Dan Albertc89e0cc2015-05-08 16:13:53 -0700449 initialized = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 }
451}
452
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453int usb_write(usb_handle *handle, const void *buf, int len)
454{
455 IOReturn result;
456
457 if (!len)
458 return 0;
459
460 if (!handle)
461 return -1;
462
463 if (NULL == handle->interface) {
464 DBG("ERR: usb_write interface was null\n");
465 return -1;
466 }
467
468 if (0 == handle->bulkOut) {
469 DBG("ERR: bulkOut endpoint not assigned\n");
470 return -1;
471 }
472
473 result =
474 (*handle->interface)->WritePipe(
475 handle->interface, handle->bulkOut, (void *)buf, len);
476
477 if ((result == 0) && (handle->zero_mask)) {
478 /* we need 0-markers and our transfer */
479 if(!(len & handle->zero_mask)) {
480 result =
481 (*handle->interface)->WritePipe(
482 handle->interface, handle->bulkOut, (void *)buf, 0);
483 }
484 }
485
486 if (0 == result)
487 return 0;
488
489 DBG("ERR: usb_write failed with status %d\n", result);
490 return -1;
491}
492
493int usb_read(usb_handle *handle, void *buf, int len)
494{
495 IOReturn result;
496 UInt32 numBytes = len;
497
498 if (!len) {
499 return 0;
500 }
501
502 if (!handle) {
503 return -1;
504 }
505
506 if (NULL == handle->interface) {
507 DBG("ERR: usb_read interface was null\n");
508 return -1;
509 }
510
511 if (0 == handle->bulkIn) {
512 DBG("ERR: bulkIn endpoint not assigned\n");
513 return -1;
514 }
515
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700516 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700518 if (kIOUSBPipeStalled == result) {
519 DBG(" Pipe stalled, clearing stall.\n");
520 (*handle->interface)->ClearPipeStall(handle->interface, handle->bulkIn);
521 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
522 }
523
524 if (kIOReturnSuccess == result)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 return 0;
526 else {
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700527 DBG("ERR: usb_read failed with status %x\n", result);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528 }
529
530 return -1;
531}
532
533int usb_close(usb_handle *handle)
534{
535 return 0;
536}
537
538void usb_kick(usb_handle *handle)
539{
540 /* release the interface */
Dima Zavin3fd82b82009-05-08 18:25:58 -0700541 if (!handle)
542 return;
543
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544 if (handle->interface)
545 {
546 (*handle->interface)->USBInterfaceClose(handle->interface);
547 (*handle->interface)->Release(handle->interface);
548 handle->interface = 0;
549 }
550}