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