The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRACE_USB |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #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 Albert | 7447dd0 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 29 | #include <inttypes.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | #include "adb.h" |
Dan Albert | dc0f8ec | 2015-02-25 10:26:17 -0800 | [diff] [blame] | 33 | #include "transport.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | |
| 35 | #define DBG D |
| 36 | |
Elliott Hughes | 6e02c24 | 2015-08-03 18:07:12 -0700 | [diff] [blame] | 37 | // 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | static IONotificationPortRef notificationPort = 0; |
Al Sutton | 8e01cc6 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 42 | static io_iterator_t notificationIterator; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
| 44 | struct usb_handle |
| 45 | { |
Siva Velusamy | d8b48a6 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 46 | UInt8 bulkIn; |
| 47 | UInt8 bulkOut; |
| 48 | IOUSBInterfaceInterface190** interface; |
| 49 | io_object_t usbNotification; |
| 50 | unsigned int zero_mask; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static CFRunLoopRef currentRunLoop = 0; |
| 54 | static pthread_mutex_t start_lock; |
| 55 | static pthread_cond_t start_cond; |
| 56 | |
| 57 | |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 58 | static void AndroidInterfaceAdded(void *refCon, io_iterator_t iterator); |
| 59 | static void AndroidInterfaceNotify(void *refCon, io_iterator_t iterator, |
| 60 | natural_t messageType, |
| 61 | void *messageArgument); |
Siva Velusamy | d8b48a6 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 62 | static usb_handle* CheckInterface(IOUSBInterfaceInterface190 **iface, |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 63 | UInt16 vendor, UInt16 product); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | |
| 65 | static int |
| 66 | InitUSB() |
| 67 | { |
| 68 | CFMutableDictionaryRef matchingDict; |
| 69 | CFRunLoopSourceRef runLoopSource; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | |
| 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 Sutton | 8e01cc6 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 77 | //* 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | |
Al Sutton | 8e01cc6 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 83 | if (!matchingDict) { |
| 84 | DBG("ERR: Couldn't create USB matching dictionary.\n"); |
| 85 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Al Sutton | 8e01cc6 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 88 | //* 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 | ¬ificationIterator); |
| 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static void |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 108 | AndroidInterfaceAdded(void *refCon, io_iterator_t iterator) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 | { |
| 110 | kern_return_t kr; |
| 111 | io_service_t usbDevice; |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 112 | io_service_t usbInterface; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | IOCFPlugInInterface **plugInInterface = NULL; |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 114 | IOUSBInterfaceInterface220 **iface = NULL; |
| 115 | IOUSBDeviceInterface197 **dev = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | HRESULT result; |
| 117 | SInt32 score; |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 118 | UInt32 locationId; |
Dan Albert | 7447dd0 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 119 | UInt8 if_class, subclass, protocol; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 120 | UInt16 vendor; |
| 121 | UInt16 product; |
| 122 | UInt8 serialIndex; |
| 123 | char serial[256]; |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 124 | char devpathBuf[64]; |
| 125 | char *devpath = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 126 | |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 127 | 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 Albert | 7447dd0 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 140 | result = (*plugInInterface)->QueryInterface( |
| 141 | plugInInterface, |
| 142 | CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID*)&iface); |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 143 | //* 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 Albert | 7447dd0 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 150 | kr = (*iface)->GetInterfaceClass(iface, &if_class); |
Al Sutton | 8e01cc6 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 151 | kr = (*iface)->GetInterfaceSubClass(iface, &subclass); |
| 152 | kr = (*iface)->GetInterfaceProtocol(iface, &protocol); |
Dan Albert | 7447dd0 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 153 | if(if_class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) { |
Al Sutton | 8e01cc6 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 154 | // Ignore non-ADB devices. |
Dan Albert | 7447dd0 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 155 | DBG("Ignoring interface with incorrect class/subclass/protocol - %d, %d, %d\n", if_class, subclass, protocol); |
Al Sutton | 8e01cc6 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 156 | (*iface)->Release(iface); |
| 157 | continue; |
| 158 | } |
| 159 | |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 160 | //* 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | kr = IOCreatePlugInInterfaceForService(usbDevice, |
| 174 | kIOUSBDeviceUserClientTypeID, |
| 175 | kIOCFPlugInInterfaceID, |
| 176 | &plugInInterface, &score); |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 177 | //* only needed this to find the plugin |
| 178 | (void)IOObjectRelease(usbDevice); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 179 | if ((kIOReturnSuccess != kr) || (!plugInInterface)) { |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 180 | DBG("ERR: Unable to create a device plug-in (%08x)\n", kr); |
| 181 | continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 182 | } |
| 183 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 184 | result = (*plugInInterface)->QueryInterface(plugInInterface, |
Dan Albert | 7447dd0 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 185 | CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID*)&dev); |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 186 | //* only needed this to query the plugin |
| 187 | (*plugInInterface)->Release(plugInInterface); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 188 | if (result || !dev) { |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 189 | DBG("ERR: Couldn't create a device interface (%08x)\n", |
| 190 | (int) result); |
| 191 | continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 194 | //* Now after all that, we actually have a ref to the device and |
| 195 | //* the interface that matched our criteria |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | kr = (*dev)->GetDeviceVendor(dev, &vendor); |
| 197 | kr = (*dev)->GetDeviceProduct(dev, &product); |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 198 | kr = (*dev)->GetLocationID(dev, &locationId); |
| 199 | if (kr == 0) { |
Ying Wang | 42a809b | 2014-08-14 15:50:13 -0700 | [diff] [blame] | 200 | snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X", |
| 201 | (unsigned int)locationId); |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 202 | devpath = devpathBuf; |
| 203 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex); |
| 205 | |
Guang Zhu | 1a1f818 | 2009-08-06 17:21:52 -0700 | [diff] [blame] | 206 | if (serialIndex > 0) { |
| 207 | IOUSBDevRequest req; |
| 208 | UInt16 buffer[256]; |
| 209 | UInt16 languages[128]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | |
Guang Zhu | 1a1f818 | 2009-08-06 17:21:52 -0700 | [diff] [blame] | 211 | memset(languages, 0, sizeof(languages)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 212 | |
Guang Zhu | 1a1f818 | 2009-08-06 17:21:52 -0700 | [diff] [blame] | 213 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 221 | |
Guang Zhu | 1a1f818 | 2009-08-06 17:21:52 -0700 | [diff] [blame] | 222 | 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 Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 254 | (*dev)->Release(dev); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 | |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 256 | DBG("INFO: Found vid=%04x pid=%04x serial=%s\n", vendor, product, |
| 257 | serial); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 | |
Siva Velusamy | d8b48a6 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 259 | usb_handle* handle = CheckInterface((IOUSBInterfaceInterface190**)iface, |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 260 | vendor, product); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 261 | if (handle == NULL) { |
| 262 | DBG("ERR: Could not find device interface: %08x\n", kr); |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 263 | (*iface)->Release(iface); |
| 264 | continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | DBG("AndroidDeviceAdded calling register_usb_transport\n"); |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 268 | register_usb_transport(handle, (serial[0] ? serial : NULL), devpath, 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 269 | |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 270 | // 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 273 | kr = IOServiceAddInterestNotification(notificationPort, |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 274 | usbInterface, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 275 | kIOGeneralInterest, |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 276 | AndroidInterfaceNotify, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 277 | handle, |
| 278 | &handle->usbNotification); |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 279 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 280 | if (kIOReturnSuccess != kr) { |
| 281 | DBG("ERR: Unable to create interest notification (%08x)\n", kr); |
| 282 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | static void |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 287 | AndroidInterfaceNotify(void *refCon, io_service_t service, natural_t messageType, void *messageArgument) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 288 | { |
| 289 | usb_handle *handle = (usb_handle *)refCon; |
| 290 | |
| 291 | if (messageType == kIOMessageServiceIsTerminated) { |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 292 | if (!handle) { |
| 293 | DBG("ERR: NULL handle\n"); |
| 294 | return; |
| 295 | } |
| 296 | DBG("AndroidInterfaceNotify\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 297 | IOObjectRelease(handle->usbNotification); |
| 298 | usb_kick(handle); |
| 299 | } |
| 300 | } |
| 301 | |
Siva Velusamy | d8b48a6 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 302 | // 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. |
| 305 | static 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 Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 314 | //* TODO: simplify this further since we only register to get ADB interface |
| 315 | //* subclass+protocol events |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 316 | static usb_handle* |
Siva Velusamy | d8b48a6 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 317 | CheckInterface(IOUSBInterfaceInterface190 **interface, UInt16 vendor, UInt16 product) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 318 | { |
Elliott Hughes | 2d4f852 | 2015-08-13 15:01:18 -0700 | [diff] [blame] | 319 | usb_handle* handle; |
| 320 | IOReturn kr; |
| 321 | UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol; |
| 322 | UInt8 endpoint; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 323 | |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 324 | //* 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 327 | if (kr != kIOReturnSuccess) { |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 328 | DBG("ERR: Could not open interface: (%08x)\n", kr); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 329 | return NULL; |
| 330 | } |
| 331 | |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 332 | //* 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 339 | //* 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 Velusamy | d8b48a6 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 349 | if (!is_adb_interface(vendor, product, interfaceClass, interfaceSubClass, interfaceProtocol)) { |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 350 | goto err_bad_adb_interface; |
Siva Velusamy | d8b48a6 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 351 | } |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 352 | |
Dan Albert | 7447dd0 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 353 | handle = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle))); |
Elliott Hughes | dc3b459 | 2015-04-21 19:39:52 -0700 | [diff] [blame] | 354 | if (handle == nullptr) goto err_bad_adb_interface; |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 355 | |
| 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 Hughes | 2d4f852 | 2015-08-13 15:01:18 -0700 | [diff] [blame] | 358 | for (endpoint = 1; endpoint <= interfaceNumEndpoints; endpoint++) { |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 359 | 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 Velusamy | d8b48a6 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 367 | if (kr != kIOReturnSuccess) { |
Elliott Hughes | 6e02c24 | 2015-08-03 18:07:12 -0700 | [diff] [blame] | 368 | DBG("ERR: FindDeviceInterface - could not get pipe properties (%08x)\n", kr); |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 369 | goto err_get_pipe_props; |
| 370 | } |
Siva Velusamy | d8b48a6 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 371 | |
| 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 Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | handle->interface = interface; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 388 | return handle; |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 389 | |
| 390 | err_get_pipe_props: |
| 391 | free(handle); |
| 392 | err_bad_adb_interface: |
| 393 | err_get_interface_class: |
| 394 | err_get_num_ep: |
| 395 | (*interface)->USBInterfaceClose(interface); |
| 396 | return NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | |
| 400 | void* RunLoopThread(void* unused) |
| 401 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 402 | 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 Sutton | 8e01cc6 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 414 | IOObjectRelease(notificationIterator); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 415 | IONotificationPortDestroy(notificationPort); |
| 416 | |
| 417 | DBG("RunLoopThread done\n"); |
Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 418 | return NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 421 | static void usb_cleanup() { |
| 422 | DBG("usb_cleanup\n"); |
| 423 | close_usb_devices(); |
| 424 | if (currentRunLoop) |
| 425 | CFRunLoopStop(currentRunLoop); |
| 426 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 427 | |
Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 428 | void usb_init() { |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 429 | static bool initialized = false; |
| 430 | if (!initialized) { |
| 431 | atexit(usb_cleanup); |
| 432 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 433 | adb_mutex_init(&start_lock, NULL); |
| 434 | adb_cond_init(&start_cond, NULL); |
| 435 | |
Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 436 | if (!adb_thread_create(RunLoopThread, nullptr)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 437 | fatal_errno("cannot create input thread"); |
Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 438 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 439 | |
| 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 Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 448 | initialized = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 452 | int 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 | |
| 492 | int 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 Canal | 9dd83dc | 2014-09-11 11:02:17 -0700 | [diff] [blame] | 515 | result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 516 | |
Esteban de la Canal | 9dd83dc | 2014-09-11 11:02:17 -0700 | [diff] [blame] | 517 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 524 | return 0; |
| 525 | else { |
Esteban de la Canal | 9dd83dc | 2014-09-11 11:02:17 -0700 | [diff] [blame] | 526 | DBG("ERR: usb_read failed with status %x\n", result); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | return -1; |
| 530 | } |
| 531 | |
| 532 | int usb_close(usb_handle *handle) |
| 533 | { |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | void usb_kick(usb_handle *handle) |
| 538 | { |
| 539 | /* release the interface */ |
Dima Zavin | 3fd82b8 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 540 | if (!handle) |
| 541 | return; |
| 542 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 543 | if (handle->interface) |
| 544 | { |
| 545 | (*handle->interface)->USBInterfaceClose(handle->interface); |
| 546 | (*handle->interface)->Release(handle->interface); |
| 547 | handle->interface = 0; |
| 548 | } |
| 549 | } |