| rjw | 2e8229f | 2022-02-15 21:08:12 +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 |  | 
|  | 34 | static IONotificationPortRef    notificationPort = 0; | 
|  | 35 | static io_iterator_t            notificationIterator; | 
|  | 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 |  | 
|  | 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); | 
|  | 57 |  | 
|  | 58 | static int | 
|  | 59 | InitUSB() | 
|  | 60 | { | 
|  | 61 | CFMutableDictionaryRef  matchingDict; | 
|  | 62 | CFRunLoopSourceRef      runLoopSource; | 
|  | 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 |  | 
|  | 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); | 
|  | 75 |  | 
|  | 76 | if (!matchingDict) { | 
|  | 77 | DBG("ERR: Couldn't create USB matching dictionary.\n"); | 
|  | 78 | return -1; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 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 |  | 
|  | 97 | return 0; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | static void | 
|  | 101 | AndroidInterfaceAdded(void *refCon, io_iterator_t iterator) | 
|  | 102 | { | 
|  | 103 | kern_return_t            kr; | 
|  | 104 | io_service_t             usbDevice; | 
|  | 105 | io_service_t             usbInterface; | 
|  | 106 | IOCFPlugInInterface      **plugInInterface = NULL; | 
|  | 107 | IOUSBInterfaceInterface220  **iface = NULL; | 
|  | 108 | IOUSBDeviceInterface197  **dev = NULL; | 
|  | 109 | HRESULT                  result; | 
|  | 110 | SInt32                   score; | 
|  | 111 | UInt32                   locationId; | 
|  | 112 | UInt8                    class, subclass, protocol; | 
|  | 113 | UInt16                   vendor; | 
|  | 114 | UInt16                   product; | 
|  | 115 | UInt8                    serialIndex; | 
|  | 116 | char                     serial[256]; | 
|  | 117 | char                     devpathBuf[64]; | 
|  | 118 | char                     *devpath = NULL; | 
|  | 119 |  | 
|  | 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 |  | 
|  | 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 |  | 
|  | 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 | 
|  | 166 | kr = IOCreatePlugInInterfaceForService(usbDevice, | 
|  | 167 | kIOUSBDeviceUserClientTypeID, | 
|  | 168 | kIOCFPlugInInterfaceID, | 
|  | 169 | &plugInInterface, &score); | 
|  | 170 | //* only needed this to find the plugin | 
|  | 171 | (void)IOObjectRelease(usbDevice); | 
|  | 172 | if ((kIOReturnSuccess != kr) || (!plugInInterface)) { | 
|  | 173 | DBG("ERR: Unable to create a device plug-in (%08x)\n", kr); | 
|  | 174 | continue; | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | result = (*plugInInterface)->QueryInterface(plugInInterface, | 
|  | 178 | CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID) &dev); | 
|  | 179 | //* only needed this to query the plugin | 
|  | 180 | (*plugInInterface)->Release(plugInInterface); | 
|  | 181 | if (result || !dev) { | 
|  | 182 | DBG("ERR: Couldn't create a device interface (%08x)\n", | 
|  | 183 | (int) result); | 
|  | 184 | continue; | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | //* Now after all that, we actually have a ref to the device and | 
|  | 188 | //* the interface that matched our criteria | 
|  | 189 | kr = (*dev)->GetDeviceVendor(dev, &vendor); | 
|  | 190 | kr = (*dev)->GetDeviceProduct(dev, &product); | 
|  | 191 | kr = (*dev)->GetLocationID(dev, &locationId); | 
|  | 192 | if (kr == 0) { | 
|  | 193 | snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X", | 
|  | 194 | (unsigned int)locationId); | 
|  | 195 | devpath = devpathBuf; | 
|  | 196 | } | 
|  | 197 | kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex); | 
|  | 198 |  | 
|  | 199 | if (serialIndex > 0) { | 
|  | 200 | IOUSBDevRequest req; | 
|  | 201 | UInt16          buffer[256]; | 
|  | 202 | UInt16          languages[128]; | 
|  | 203 |  | 
|  | 204 | memset(languages, 0, sizeof(languages)); | 
|  | 205 |  | 
|  | 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); | 
|  | 214 |  | 
|  | 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 | } | 
|  | 247 | (*dev)->Release(dev); | 
|  | 248 |  | 
|  | 249 | DBG("INFO: Found vid=%04x pid=%04x serial=%s\n", vendor, product, | 
|  | 250 | serial); | 
|  | 251 |  | 
|  | 252 | usb_handle* handle = CheckInterface((IOUSBInterfaceInterface**)iface, | 
|  | 253 | vendor, product); | 
|  | 254 | if (handle == NULL) { | 
|  | 255 | DBG("ERR: Could not find device interface: %08x\n", kr); | 
|  | 256 | (*iface)->Release(iface); | 
|  | 257 | continue; | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | DBG("AndroidDeviceAdded calling register_usb_transport\n"); | 
|  | 261 | register_usb_transport(handle, (serial[0] ? serial : NULL), devpath, 1); | 
|  | 262 |  | 
|  | 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. | 
|  | 266 | kr = IOServiceAddInterestNotification(notificationPort, | 
|  | 267 | usbInterface, | 
|  | 268 | kIOGeneralInterest, | 
|  | 269 | AndroidInterfaceNotify, | 
|  | 270 | handle, | 
|  | 271 | &handle->usbNotification); | 
|  | 272 |  | 
|  | 273 | if (kIOReturnSuccess != kr) { | 
|  | 274 | DBG("ERR: Unable to create interest notification (%08x)\n", kr); | 
|  | 275 | } | 
|  | 276 | } | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | static void | 
|  | 280 | AndroidInterfaceNotify(void *refCon, io_service_t service, natural_t messageType, void *messageArgument) | 
|  | 281 | { | 
|  | 282 | usb_handle *handle = (usb_handle *)refCon; | 
|  | 283 |  | 
|  | 284 | if (messageType == kIOMessageServiceIsTerminated) { | 
|  | 285 | if (!handle) { | 
|  | 286 | DBG("ERR: NULL handle\n"); | 
|  | 287 | return; | 
|  | 288 | } | 
|  | 289 | DBG("AndroidInterfaceNotify\n"); | 
|  | 290 | IOObjectRelease(handle->usbNotification); | 
|  | 291 | usb_kick(handle); | 
|  | 292 | } | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | //* TODO: simplify this further since we only register to get ADB interface | 
|  | 296 | //* subclass+protocol events | 
|  | 297 | static usb_handle* | 
|  | 298 | CheckInterface(IOUSBInterfaceInterface **interface, UInt16 vendor, UInt16 product) | 
|  | 299 | { | 
|  | 300 | usb_handle*                 handle = NULL; | 
|  | 301 | IOReturn                    kr; | 
|  | 302 | UInt8  interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol; | 
|  | 303 | UInt8  endpoint; | 
|  | 304 |  | 
|  | 305 |  | 
|  | 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); | 
|  | 309 | if (kr != kIOReturnSuccess) { | 
|  | 310 | DBG("ERR: Could not open interface: (%08x)\n", kr); | 
|  | 311 | return NULL; | 
|  | 312 | } | 
|  | 313 |  | 
|  | 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; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 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; | 
|  | 367 | return handle; | 
|  | 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; | 
|  | 376 | } | 
|  | 377 |  | 
|  | 378 |  | 
|  | 379 | void* RunLoopThread(void* unused) | 
|  | 380 | { | 
|  | 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 |  | 
|  | 393 | IOObjectRelease(notificationIterator); | 
|  | 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 |  | 
|  | 497 | result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes); | 
|  | 498 |  | 
|  | 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) | 
|  | 506 | return 0; | 
|  | 507 | else { | 
|  | 508 | DBG("ERR: usb_read failed with status %x\n", result); | 
|  | 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 */ | 
|  | 522 | if (!handle) | 
|  | 523 | return; | 
|  | 524 |  | 
|  | 525 | if (handle->interface) | 
|  | 526 | { | 
|  | 527 | (*handle->interface)->USBInterfaceClose(handle->interface); | 
|  | 528 | (*handle->interface)->Release(handle->interface); | 
|  | 529 | handle->interface = 0; | 
|  | 530 | } | 
|  | 531 | } |