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