The Android Open Source Project | 9ca14dc | 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 | db6fe64 | 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 | 9ca14dc | 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 | 2589606 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 29 | #include <inttypes.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 32 | #include <base/logging.h> |
| 33 | #include <base/stringprintf.h> |
| 34 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | #include "adb.h" |
Dan Albert | bd3d448 | 2015-02-25 10:26:17 -0800 | [diff] [blame] | 36 | #include "transport.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | |
| 38 | #define DBG D |
| 39 | |
Elliott Hughes | d9ce0ff | 2015-08-03 18:07:12 -0700 | [diff] [blame] | 40 | // There's no strerror equivalent for the errors returned by IOKit. |
| 41 | // https://developer.apple.com/library/mac/documentation/DeviceDrivers/Conceptual/AccessingHardware/AH_Handling_Errors/AH_Handling_Errors.html |
| 42 | // Search the web for "IOReturn.h" to find a complete up to date list. |
| 43 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | static IONotificationPortRef notificationPort = 0; |
Al Sutton | 178bae8 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 45 | static io_iterator_t notificationIterator; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | |
| 47 | struct usb_handle |
| 48 | { |
Siva Velusamy | 3ce4607 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 49 | UInt8 bulkIn; |
| 50 | UInt8 bulkOut; |
| 51 | IOUSBInterfaceInterface190** interface; |
| 52 | io_object_t usbNotification; |
| 53 | unsigned int zero_mask; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static CFRunLoopRef currentRunLoop = 0; |
| 57 | static pthread_mutex_t start_lock; |
| 58 | static pthread_cond_t start_cond; |
| 59 | |
| 60 | |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 61 | static void AndroidInterfaceAdded(void *refCon, io_iterator_t iterator); |
| 62 | static void AndroidInterfaceNotify(void *refCon, io_iterator_t iterator, |
| 63 | natural_t messageType, |
| 64 | void *messageArgument); |
Siva Velusamy | 3ce4607 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 65 | static usb_handle* CheckInterface(IOUSBInterfaceInterface190 **iface, |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 66 | UInt16 vendor, UInt16 product); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | |
| 68 | static int |
| 69 | InitUSB() |
| 70 | { |
| 71 | CFMutableDictionaryRef matchingDict; |
| 72 | CFRunLoopSourceRef runLoopSource; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 73 | |
| 74 | //* To set up asynchronous notifications, create a notification port and |
| 75 | //* add its run loop event source to the program's run loop |
| 76 | notificationPort = IONotificationPortCreate(kIOMasterPortDefault); |
| 77 | runLoopSource = IONotificationPortGetRunLoopSource(notificationPort); |
| 78 | CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode); |
| 79 | |
Al Sutton | 178bae8 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 80 | //* Create our matching dictionary to find the Android device's |
| 81 | //* adb interface |
| 82 | //* IOServiceAddMatchingNotification consumes the reference, so we do |
| 83 | //* not need to release this |
| 84 | matchingDict = IOServiceMatching(kIOUSBInterfaceClassName); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | |
Al Sutton | 178bae8 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 86 | if (!matchingDict) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 87 | LOG(ERROR) << "Couldn't create USB matching dictionary."; |
Al Sutton | 178bae8 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 88 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Al Sutton | 178bae8 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 91 | //* We have to get notifications for all potential candidates and test them |
| 92 | //* at connection time because the matching rules don't allow for a |
| 93 | //* USB interface class of 0xff for class+subclass+protocol matches |
| 94 | //* See https://developer.apple.com/library/mac/qa/qa1076/_index.html |
| 95 | IOServiceAddMatchingNotification( |
| 96 | notificationPort, |
| 97 | kIOFirstMatchNotification, |
| 98 | matchingDict, |
| 99 | AndroidInterfaceAdded, |
| 100 | NULL, |
| 101 | ¬ificationIterator); |
| 102 | |
| 103 | //* Iterate over set of matching interfaces to access already-present |
| 104 | //* devices and to arm the notification |
| 105 | AndroidInterfaceAdded(NULL, notificationIterator); |
| 106 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static void |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 111 | AndroidInterfaceAdded(void *refCon, io_iterator_t iterator) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | { |
| 113 | kern_return_t kr; |
| 114 | io_service_t usbDevice; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 115 | io_service_t usbInterface; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | IOCFPlugInInterface **plugInInterface = NULL; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 117 | IOUSBInterfaceInterface220 **iface = NULL; |
| 118 | IOUSBDeviceInterface197 **dev = NULL; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 119 | HRESULT result; |
| 120 | SInt32 score; |
Elliott Hughes | 8dc9c86 | 2015-08-25 17:48:12 -0700 | [diff] [blame] | 121 | uint32_t locationId; |
Dan Albert | 2589606 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 122 | UInt8 if_class, subclass, protocol; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 123 | UInt16 vendor; |
| 124 | UInt16 product; |
| 125 | UInt8 serialIndex; |
| 126 | char serial[256]; |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 127 | char devpathBuf[64]; |
| 128 | char *devpath = NULL; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 130 | while ((usbInterface = IOIteratorNext(iterator))) { |
| 131 | //* Create an intermediate interface plugin |
| 132 | kr = IOCreatePlugInInterfaceForService(usbInterface, |
| 133 | kIOUSBInterfaceUserClientTypeID, |
| 134 | kIOCFPlugInInterfaceID, |
| 135 | &plugInInterface, &score); |
| 136 | IOObjectRelease(usbInterface); |
| 137 | if ((kIOReturnSuccess != kr) || (!plugInInterface)) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 138 | LOG(ERROR) << "Unable to create an interface plug-in (" << std::hex << kr << ")"; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 139 | continue; |
| 140 | } |
| 141 | |
| 142 | //* This gets us the interface object |
Dan Albert | 2589606 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 143 | result = (*plugInInterface)->QueryInterface( |
| 144 | plugInInterface, |
| 145 | CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID*)&iface); |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 146 | //* We only needed the plugin to get the interface, so discard it |
| 147 | (*plugInInterface)->Release(plugInInterface); |
| 148 | if (result || !iface) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 149 | LOG(ERROR) << "Couldn't query the interface (" << std::hex << result << ")"; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 150 | continue; |
| 151 | } |
| 152 | |
Dan Albert | 2589606 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 153 | kr = (*iface)->GetInterfaceClass(iface, &if_class); |
Al Sutton | 178bae8 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 154 | kr = (*iface)->GetInterfaceSubClass(iface, &subclass); |
| 155 | kr = (*iface)->GetInterfaceProtocol(iface, &protocol); |
Dan Albert | 2589606 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 156 | if(if_class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) { |
Al Sutton | 178bae8 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 157 | // Ignore non-ADB devices. |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 158 | LOG(DEBUG) << "Ignoring interface with incorrect class/subclass/protocol - " << if_class |
| 159 | << ", " << subclass << ", " << protocol; |
Al Sutton | 178bae8 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 160 | (*iface)->Release(iface); |
| 161 | continue; |
| 162 | } |
| 163 | |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 164 | //* this gets us an ioservice, with which we will find the actual |
| 165 | //* device; after getting a plugin, and querying the interface, of |
| 166 | //* course. |
| 167 | //* Gotta love OS X |
| 168 | kr = (*iface)->GetDevice(iface, &usbDevice); |
| 169 | if (kIOReturnSuccess != kr || !usbDevice) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 170 | LOG(ERROR) << "Couldn't grab device from interface (" << std::hex << kr << ")"; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 171 | continue; |
| 172 | } |
| 173 | |
| 174 | plugInInterface = NULL; |
| 175 | score = 0; |
| 176 | //* create an intermediate device plugin |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | kr = IOCreatePlugInInterfaceForService(usbDevice, |
| 178 | kIOUSBDeviceUserClientTypeID, |
| 179 | kIOCFPlugInInterfaceID, |
| 180 | &plugInInterface, &score); |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 181 | //* only needed this to find the plugin |
| 182 | (void)IOObjectRelease(usbDevice); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 183 | if ((kIOReturnSuccess != kr) || (!plugInInterface)) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 184 | LOG(ERROR) << "Unable to create a device plug-in (" << std::hex << kr << ")"; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 185 | continue; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 186 | } |
| 187 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 188 | result = (*plugInInterface)->QueryInterface(plugInInterface, |
Dan Albert | 2589606 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 189 | CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID*)&dev); |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 190 | //* only needed this to query the plugin |
| 191 | (*plugInInterface)->Release(plugInInterface); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 192 | if (result || !dev) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 193 | LOG(ERROR) << "Couldn't create a device interface (" << std::hex << result << ")"; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 194 | continue; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 197 | //* Now after all that, we actually have a ref to the device and |
| 198 | //* the interface that matched our criteria |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | kr = (*dev)->GetDeviceVendor(dev, &vendor); |
| 200 | kr = (*dev)->GetDeviceProduct(dev, &product); |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 201 | kr = (*dev)->GetLocationID(dev, &locationId); |
| 202 | if (kr == 0) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 203 | snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X", locationId); |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 204 | devpath = devpathBuf; |
| 205 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 206 | kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex); |
| 207 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 208 | if (serialIndex > 0) { |
| 209 | IOUSBDevRequest req; |
| 210 | UInt16 buffer[256]; |
| 211 | UInt16 languages[128]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 212 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 213 | memset(languages, 0, sizeof(languages)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 215 | req.bmRequestType = |
| 216 | USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); |
| 217 | req.bRequest = kUSBRqGetDescriptor; |
| 218 | req.wValue = (kUSBStringDesc << 8) | 0; |
| 219 | req.wIndex = 0; |
| 220 | req.pData = languages; |
| 221 | req.wLength = sizeof(languages); |
| 222 | kr = (*dev)->DeviceRequest(dev, &req); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 223 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 224 | if (kr == kIOReturnSuccess && req.wLenDone > 0) { |
Guang Zhu | 84b5bd2 | 2009-08-06 17:21:52 -0700 | [diff] [blame] | 225 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 226 | int langCount = (req.wLenDone - 2) / 2, lang; |
Guang Zhu | 84b5bd2 | 2009-08-06 17:21:52 -0700 | [diff] [blame] | 227 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 228 | for (lang = 1; lang <= langCount; lang++) { |
Guang Zhu | 84b5bd2 | 2009-08-06 17:21:52 -0700 | [diff] [blame] | 229 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 230 | memset(buffer, 0, sizeof(buffer)); |
| 231 | memset(&req, 0, sizeof(req)); |
Guang Zhu | 84b5bd2 | 2009-08-06 17:21:52 -0700 | [diff] [blame] | 232 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 233 | req.bmRequestType = |
| 234 | USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); |
| 235 | req.bRequest = kUSBRqGetDescriptor; |
| 236 | req.wValue = (kUSBStringDesc << 8) | serialIndex; |
| 237 | req.wIndex = languages[lang]; |
| 238 | req.pData = buffer; |
| 239 | req.wLength = sizeof(buffer); |
| 240 | kr = (*dev)->DeviceRequest(dev, &req); |
Guang Zhu | 84b5bd2 | 2009-08-06 17:21:52 -0700 | [diff] [blame] | 241 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 242 | if (kr == kIOReturnSuccess && req.wLenDone > 0) { |
| 243 | int i, count; |
Guang Zhu | 84b5bd2 | 2009-08-06 17:21:52 -0700 | [diff] [blame] | 244 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 245 | // skip first word, and copy the rest to the serial string, |
| 246 | // changing shorts to bytes. |
| 247 | count = (req.wLenDone - 1) / 2; |
| 248 | for (i = 0; i < count; i++) |
| 249 | serial[i] = buffer[i + 1]; |
| 250 | serial[i] = 0; |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 257 | (*dev)->Release(dev); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 259 | LOG(INFO) << android::base::StringPrintf("Found vid=%04x pid=%04x serial=%s\n", |
| 260 | vendor, product, serial); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 261 | |
Siva Velusamy | 3ce4607 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 262 | usb_handle* handle = CheckInterface((IOUSBInterfaceInterface190**)iface, |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 263 | vendor, product); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 264 | if (handle == NULL) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 265 | LOG(ERROR) << "Could not find device interface"; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 266 | (*iface)->Release(iface); |
| 267 | continue; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 270 | LOG(DEBUG) << "AndroidDeviceAdded calling register_usb_transport"; |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 271 | register_usb_transport(handle, (serial[0] ? serial : NULL), devpath, 1); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 272 | |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 273 | // Register for an interest notification of this device being removed. |
| 274 | // Pass the reference to our private data as the refCon for the |
| 275 | // notification. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 276 | kr = IOServiceAddInterestNotification(notificationPort, |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 277 | usbInterface, |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 278 | kIOGeneralInterest, |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 279 | AndroidInterfaceNotify, |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 280 | handle, |
| 281 | &handle->usbNotification); |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 282 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 283 | if (kIOReturnSuccess != kr) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 284 | LOG(ERROR) << "Unable to create interest notification (" << std::hex << kr << ")"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 285 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | static void |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 290 | AndroidInterfaceNotify(void *refCon, io_service_t service, natural_t messageType, void *messageArgument) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 291 | { |
| 292 | usb_handle *handle = (usb_handle *)refCon; |
| 293 | |
| 294 | if (messageType == kIOMessageServiceIsTerminated) { |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 295 | if (!handle) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 296 | LOG(ERROR) << "NULL handle"; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 297 | return; |
| 298 | } |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 299 | LOG(DEBUG) << "AndroidInterfaceNotify"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 300 | IOObjectRelease(handle->usbNotification); |
| 301 | usb_kick(handle); |
| 302 | } |
| 303 | } |
| 304 | |
Siva Velusamy | 3ce4607 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 305 | // Used to clear both the endpoints before starting. |
| 306 | // When adb quits, we might clear the host endpoint but not the device. |
| 307 | // So we make sure both sides are clear before starting up. |
| 308 | static bool ClearPipeStallBothEnds(IOUSBInterfaceInterface190** interface, UInt8 bulkEp) { |
| 309 | IOReturn rc = (*interface)->ClearPipeStallBothEnds(interface, bulkEp); |
| 310 | if (rc != kIOReturnSuccess) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 311 | LOG(ERROR) << "Could not clear pipe stall both ends: " << std::hex << rc; |
Siva Velusamy | 3ce4607 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 312 | return false; |
| 313 | } |
| 314 | return true; |
| 315 | } |
| 316 | |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 317 | //* TODO: simplify this further since we only register to get ADB interface |
| 318 | //* subclass+protocol events |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 319 | static usb_handle* |
Siva Velusamy | 3ce4607 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 320 | CheckInterface(IOUSBInterfaceInterface190 **interface, UInt16 vendor, UInt16 product) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 321 | { |
Elliott Hughes | 6d6cb54 | 2015-08-13 15:01:18 -0700 | [diff] [blame] | 322 | usb_handle* handle; |
| 323 | IOReturn kr; |
| 324 | UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol; |
| 325 | UInt8 endpoint; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 326 | |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 327 | //* Now open the interface. This will cause the pipes associated with |
| 328 | //* the endpoints in the interface descriptor to be instantiated |
| 329 | kr = (*interface)->USBInterfaceOpen(interface); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 330 | if (kr != kIOReturnSuccess) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 331 | LOG(ERROR) << "Could not open interface: " << std::hex << kr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 332 | return NULL; |
| 333 | } |
| 334 | |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 335 | //* Get the number of endpoints associated with this interface |
| 336 | kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints); |
| 337 | if (kr != kIOReturnSuccess) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 338 | LOG(ERROR) << "Unable to get number of endpoints: " << std::hex << kr; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 339 | goto err_get_num_ep; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 342 | //* Get interface class, subclass and protocol |
| 343 | if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess || |
| 344 | (*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess || |
| 345 | (*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 346 | LOG(ERROR) << "Unable to get interface class, subclass and protocol"; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 347 | goto err_get_interface_class; |
| 348 | } |
| 349 | |
| 350 | //* check to make sure interface class, subclass and protocol match ADB |
| 351 | //* avoid opening mass storage endpoints |
Siva Velusamy | 3ce4607 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 352 | if (!is_adb_interface(vendor, product, interfaceClass, interfaceSubClass, interfaceProtocol)) { |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 353 | goto err_bad_adb_interface; |
Siva Velusamy | 3ce4607 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 354 | } |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 355 | |
Dan Albert | 2589606 | 2015-04-16 19:20:40 -0700 | [diff] [blame] | 356 | handle = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle))); |
Elliott Hughes | d0269c9 | 2015-04-21 19:39:52 -0700 | [diff] [blame] | 357 | if (handle == nullptr) goto err_bad_adb_interface; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 358 | |
| 359 | //* Iterate over the endpoints for this interface and find the first |
| 360 | //* bulk in/out pipes available. These will be our read/write pipes. |
Elliott Hughes | 6d6cb54 | 2015-08-13 15:01:18 -0700 | [diff] [blame] | 361 | for (endpoint = 1; endpoint <= interfaceNumEndpoints; endpoint++) { |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 362 | UInt8 transferType; |
| 363 | UInt16 maxPacketSize; |
| 364 | UInt8 interval; |
| 365 | UInt8 number; |
| 366 | UInt8 direction; |
| 367 | |
| 368 | kr = (*interface)->GetPipeProperties(interface, endpoint, &direction, |
| 369 | &number, &transferType, &maxPacketSize, &interval); |
Siva Velusamy | 3ce4607 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 370 | if (kr != kIOReturnSuccess) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 371 | LOG(ERROR) << "FindDeviceInterface - could not get pipe properties: " |
| 372 | << std::hex << kr; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 373 | goto err_get_pipe_props; |
| 374 | } |
Siva Velusamy | 3ce4607 | 2015-08-13 08:48:06 -0700 | [diff] [blame] | 375 | |
| 376 | if (kUSBBulk != transferType) continue; |
| 377 | |
| 378 | if (kUSBIn == direction) { |
| 379 | handle->bulkIn = endpoint; |
| 380 | if (!ClearPipeStallBothEnds(interface, handle->bulkIn)) goto err_get_pipe_props; |
| 381 | } |
| 382 | |
| 383 | if (kUSBOut == direction) { |
| 384 | handle->bulkOut = endpoint; |
| 385 | if (!ClearPipeStallBothEnds(interface, handle->bulkOut)) goto err_get_pipe_props; |
| 386 | } |
| 387 | |
| 388 | handle->zero_mask = maxPacketSize - 1; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | handle->interface = interface; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 392 | return handle; |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 393 | |
| 394 | err_get_pipe_props: |
| 395 | free(handle); |
| 396 | err_bad_adb_interface: |
| 397 | err_get_interface_class: |
| 398 | err_get_num_ep: |
| 399 | (*interface)->USBInterfaceClose(interface); |
| 400 | return NULL; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
| 404 | void* RunLoopThread(void* unused) |
| 405 | { |
Siva Velusamy | 2669cf9 | 2015-08-28 16:37:29 -0700 | [diff] [blame^] | 406 | adb_thread_setname("RunLoop"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 407 | InitUSB(); |
| 408 | |
| 409 | currentRunLoop = CFRunLoopGetCurrent(); |
| 410 | |
| 411 | // Signal the parent that we are running |
| 412 | adb_mutex_lock(&start_lock); |
| 413 | adb_cond_signal(&start_cond); |
| 414 | adb_mutex_unlock(&start_lock); |
| 415 | |
| 416 | CFRunLoopRun(); |
| 417 | currentRunLoop = 0; |
| 418 | |
Al Sutton | 178bae8 | 2014-11-21 12:21:12 +0000 | [diff] [blame] | 419 | IOObjectRelease(notificationIterator); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 420 | IONotificationPortDestroy(notificationPort); |
| 421 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 422 | LOG(DEBUG) << "RunLoopThread done"; |
Elliott Hughes | f251714 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 423 | return NULL; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 426 | static void usb_cleanup() { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 427 | LOG(DEBUG) << "usb_cleanup"; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 428 | close_usb_devices(); |
| 429 | if (currentRunLoop) |
| 430 | CFRunLoopStop(currentRunLoop); |
| 431 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 432 | |
Elliott Hughes | f251714 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 433 | void usb_init() { |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 434 | static bool initialized = false; |
| 435 | if (!initialized) { |
| 436 | atexit(usb_cleanup); |
| 437 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 438 | adb_mutex_init(&start_lock, NULL); |
| 439 | adb_cond_init(&start_cond, NULL); |
| 440 | |
Elliott Hughes | f251714 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 441 | if (!adb_thread_create(RunLoopThread, nullptr)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 442 | fatal_errno("cannot create input thread"); |
Elliott Hughes | f251714 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 443 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 444 | |
| 445 | // Wait for initialization to finish |
| 446 | adb_mutex_lock(&start_lock); |
| 447 | adb_cond_wait(&start_cond, &start_lock); |
| 448 | adb_mutex_unlock(&start_lock); |
| 449 | |
| 450 | adb_mutex_destroy(&start_lock); |
| 451 | adb_cond_destroy(&start_cond); |
| 452 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 453 | initialized = true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 457 | int usb_write(usb_handle *handle, const void *buf, int len) |
| 458 | { |
| 459 | IOReturn result; |
| 460 | |
| 461 | if (!len) |
| 462 | return 0; |
| 463 | |
| 464 | if (!handle) |
| 465 | return -1; |
| 466 | |
| 467 | if (NULL == handle->interface) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 468 | LOG(ERROR) << "usb_write interface was null"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 469 | return -1; |
| 470 | } |
| 471 | |
| 472 | if (0 == handle->bulkOut) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 473 | LOG(ERROR) << "bulkOut endpoint not assigned"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | result = |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 478 | (*handle->interface)->WritePipe(handle->interface, handle->bulkOut, (void *)buf, len); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 479 | |
| 480 | if ((result == 0) && (handle->zero_mask)) { |
| 481 | /* we need 0-markers and our transfer */ |
| 482 | if(!(len & handle->zero_mask)) { |
| 483 | result = |
| 484 | (*handle->interface)->WritePipe( |
| 485 | handle->interface, handle->bulkOut, (void *)buf, 0); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if (0 == result) |
| 490 | return 0; |
| 491 | |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 492 | LOG(ERROR) << "usb_write failed with status: " << std::hex << result; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 493 | return -1; |
| 494 | } |
| 495 | |
| 496 | int usb_read(usb_handle *handle, void *buf, int len) |
| 497 | { |
| 498 | IOReturn result; |
| 499 | UInt32 numBytes = len; |
| 500 | |
| 501 | if (!len) { |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | if (!handle) { |
| 506 | return -1; |
| 507 | } |
| 508 | |
| 509 | if (NULL == handle->interface) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 510 | LOG(ERROR) << "usb_read interface was null"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 511 | return -1; |
| 512 | } |
| 513 | |
| 514 | if (0 == handle->bulkIn) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 515 | LOG(ERROR) << "bulkIn endpoint not assigned"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 516 | return -1; |
| 517 | } |
| 518 | |
Esteban de la Canal | 647231a | 2014-09-11 11:02:17 -0700 | [diff] [blame] | 519 | result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 520 | |
Esteban de la Canal | 647231a | 2014-09-11 11:02:17 -0700 | [diff] [blame] | 521 | if (kIOUSBPipeStalled == result) { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 522 | LOG(ERROR) << "Pipe stalled, clearing stall.\n"; |
Esteban de la Canal | 647231a | 2014-09-11 11:02:17 -0700 | [diff] [blame] | 523 | (*handle->interface)->ClearPipeStall(handle->interface, handle->bulkIn); |
| 524 | result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes); |
| 525 | } |
| 526 | |
| 527 | if (kIOReturnSuccess == result) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 528 | return 0; |
| 529 | else { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 530 | LOG(ERROR) << "usb_read failed with status: " << std::hex << result; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | return -1; |
| 534 | } |
| 535 | |
| 536 | int usb_close(usb_handle *handle) |
| 537 | { |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | void usb_kick(usb_handle *handle) |
| 542 | { |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 543 | LOG(INFO) << "Kicking handle"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 544 | /* release the interface */ |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 545 | if (!handle) |
| 546 | return; |
| 547 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 548 | if (handle->interface) |
| 549 | { |
| 550 | (*handle->interface)->USBInterfaceClose(handle->interface); |
| 551 | (*handle->interface)->Release(handle->interface); |
| 552 | handle->interface = 0; |
| 553 | } |
| 554 | } |