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