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