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