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