blob: ee893f5447949cb9324ea8512378cf98cbd6e936 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
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 Ducrohet2ef5fc22009-05-20 17:33:53 -070031#include "usb_vendors.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080032
33#define DBG D
34
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080035static IONotificationPortRef notificationPort = 0;
Xavier Ducrohet2ef5fc22009-05-20 17:33:53 -070036static io_iterator_t* notificationIterators;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080037
38struct usb_handle
39{
40 UInt8 bulkIn;
41 UInt8 bulkOut;
42 IOUSBInterfaceInterface **interface;
43 io_object_t usbNotification;
44 unsigned int zero_mask;
45};
46
47static CFRunLoopRef currentRunLoop = 0;
48static pthread_mutex_t start_lock;
49static pthread_cond_t start_cond;
50
51
Dima Zavin3e824912009-05-08 18:25:58 -070052static void AndroidInterfaceAdded(void *refCon, io_iterator_t iterator);
53static void AndroidInterfaceNotify(void *refCon, io_iterator_t iterator,
54 natural_t messageType,
55 void *messageArgument);
56static usb_handle* CheckInterface(IOUSBInterfaceInterface **iface,
57 UInt16 vendor, UInt16 product);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080058
59static int
60InitUSB()
61{
62 CFMutableDictionaryRef matchingDict;
63 CFRunLoopSourceRef runLoopSource;
Dima Zavin3e824912009-05-08 18:25:58 -070064 SInt32 vendor, if_subclass, if_protocol;
65 unsigned i;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080066
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 Zavin3e824912009-05-08 18:25:58 -070075 //* loop through all supported vendors
Xavier Ducrohet2ef5fc22009-05-20 17:33:53 -070076 for (i = 0; i < vendorIdCount; i++) {
Dima Zavin3e824912009-05-08 18:25:58 -070077 //* 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 Project9ca14dc2009-03-03 19:32:55 -080082
83 if (!matchingDict) {
84 DBG("ERR: Couldn't create USB matching dictionary.\n");
85 return -1;
86 }
87
Dima Zavin3e824912009-05-08 18:25:58 -070088 //* 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 Project9ca14dc2009-03-03 19:32:55 -0800101 IOServiceAddMatchingNotification(
102 notificationPort,
103 kIOFirstMatchNotification,
104 matchingDict,
Dima Zavin3e824912009-05-08 18:25:58 -0700105 AndroidInterfaceAdded,
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800106 NULL,
107 &notificationIterators[i]);
108
Dima Zavin3e824912009-05-08 18:25:58 -0700109 //* 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 Project9ca14dc2009-03-03 19:32:55 -0800112 }
113
114 return 0;
115}
116
117static void
Dima Zavin3e824912009-05-08 18:25:58 -0700118AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800119{
120 kern_return_t kr;
121 io_service_t usbDevice;
Dima Zavin3e824912009-05-08 18:25:58 -0700122 io_service_t usbInterface;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800123 IOCFPlugInInterface **plugInInterface = NULL;
Dima Zavin3e824912009-05-08 18:25:58 -0700124 IOUSBInterfaceInterface220 **iface = NULL;
125 IOUSBDeviceInterface197 **dev = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800126 HRESULT result;
127 SInt32 score;
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700128 UInt32 locationId;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800129 UInt16 vendor;
130 UInt16 product;
131 UInt8 serialIndex;
132 char serial[256];
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700133 char devpathBuf[64];
134 char *devpath = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800135
Dima Zavin3e824912009-05-08 18:25:58 -0700136 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 Project9ca14dc2009-03-03 19:32:55 -0800172 kr = IOCreatePlugInInterfaceForService(usbDevice,
173 kIOUSBDeviceUserClientTypeID,
174 kIOCFPlugInInterfaceID,
175 &plugInInterface, &score);
Dima Zavin3e824912009-05-08 18:25:58 -0700176 //* only needed this to find the plugin
177 (void)IOObjectRelease(usbDevice);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800178 if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
Dima Zavin3e824912009-05-08 18:25:58 -0700179 DBG("ERR: Unable to create a device plug-in (%08x)\n", kr);
180 continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800181 }
182
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800183 result = (*plugInInterface)->QueryInterface(plugInInterface,
184 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID) &dev);
Dima Zavin3e824912009-05-08 18:25:58 -0700185 //* only needed this to query the plugin
186 (*plugInInterface)->Release(plugInInterface);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800187 if (result || !dev) {
Dima Zavin3e824912009-05-08 18:25:58 -0700188 DBG("ERR: Couldn't create a device interface (%08x)\n",
189 (int) result);
190 continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800191 }
192
Dima Zavin3e824912009-05-08 18:25:58 -0700193 //* 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 Project9ca14dc2009-03-03 19:32:55 -0800196 kr = (*dev)->GetDeviceVendor(dev, &vendor);
197 kr = (*dev)->GetDeviceProduct(dev, &product);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700198 kr = (*dev)->GetLocationID(dev, &locationId);
199 if (kr == 0) {
Ying Wangf6d4f9a2014-08-14 15:50:13 -0700200 snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X",
201 (unsigned int)locationId);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700202 devpath = devpathBuf;
203 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800204 kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
205
Guang Zhu84b5bd22009-08-06 17:21:52 -0700206 if (serialIndex > 0) {
207 IOUSBDevRequest req;
208 UInt16 buffer[256];
209 UInt16 languages[128];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800210
Guang Zhu84b5bd22009-08-06 17:21:52 -0700211 memset(languages, 0, sizeof(languages));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800212
Guang Zhu84b5bd22009-08-06 17:21:52 -0700213 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 Project9ca14dc2009-03-03 19:32:55 -0800221
Guang Zhu84b5bd22009-08-06 17:21:52 -0700222 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 Zavin3e824912009-05-08 18:25:58 -0700254 (*dev)->Release(dev);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800255
Dima Zavin3e824912009-05-08 18:25:58 -0700256 DBG("INFO: Found vid=%04x pid=%04x serial=%s\n", vendor, product,
257 serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800258
Dima Zavin3e824912009-05-08 18:25:58 -0700259 usb_handle* handle = CheckInterface((IOUSBInterfaceInterface**)iface,
260 vendor, product);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800261 if (handle == NULL) {
262 DBG("ERR: Could not find device interface: %08x\n", kr);
Dima Zavin3e824912009-05-08 18:25:58 -0700263 (*iface)->Release(iface);
264 continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800265 }
266
267 DBG("AndroidDeviceAdded calling register_usb_transport\n");
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700268 register_usb_transport(handle, (serial[0] ? serial : NULL), devpath, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800269
Dima Zavin3e824912009-05-08 18:25:58 -0700270 // 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 Project9ca14dc2009-03-03 19:32:55 -0800273 kr = IOServiceAddInterestNotification(notificationPort,
Dima Zavin3e824912009-05-08 18:25:58 -0700274 usbInterface,
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800275 kIOGeneralInterest,
Dima Zavin3e824912009-05-08 18:25:58 -0700276 AndroidInterfaceNotify,
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800277 handle,
278 &handle->usbNotification);
Dima Zavin3e824912009-05-08 18:25:58 -0700279
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800280 if (kIOReturnSuccess != kr) {
281 DBG("ERR: Unable to create interest notification (%08x)\n", kr);
282 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800283 }
284}
285
286static void
Dima Zavin3e824912009-05-08 18:25:58 -0700287AndroidInterfaceNotify(void *refCon, io_service_t service, natural_t messageType, void *messageArgument)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800288{
289 usb_handle *handle = (usb_handle *)refCon;
290
291 if (messageType == kIOMessageServiceIsTerminated) {
Dima Zavin3e824912009-05-08 18:25:58 -0700292 if (!handle) {
293 DBG("ERR: NULL handle\n");
294 return;
295 }
296 DBG("AndroidInterfaceNotify\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800297 IOObjectRelease(handle->usbNotification);
298 usb_kick(handle);
299 }
300}
301
Dima Zavin3e824912009-05-08 18:25:58 -0700302//* TODO: simplify this further since we only register to get ADB interface
303//* subclass+protocol events
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800304static usb_handle*
Dima Zavin3e824912009-05-08 18:25:58 -0700305CheckInterface(IOUSBInterfaceInterface **interface, UInt16 vendor, UInt16 product)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800306{
307 usb_handle* handle = NULL;
308 IOReturn kr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800309 UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol;
Dima Zavin3e824912009-05-08 18:25:58 -0700310 UInt8 endpoint;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800311
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800312
Dima Zavin3e824912009-05-08 18:25:58 -0700313 //* 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 Project9ca14dc2009-03-03 19:32:55 -0800316 if (kr != kIOReturnSuccess) {
Dima Zavin3e824912009-05-08 18:25:58 -0700317 DBG("ERR: Could not open interface: (%08x)\n", kr);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800318 return NULL;
319 }
320
Dima Zavin3e824912009-05-08 18:25:58 -0700321 //* 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 Project9ca14dc2009-03-03 19:32:55 -0800326 }
327
Dima Zavin3e824912009-05-08 18:25:58 -0700328 //* 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 Project9ca14dc2009-03-03 19:32:55 -0800374 return handle;
Dima Zavin3e824912009-05-08 18:25:58 -0700375
376err_get_pipe_props:
377 free(handle);
378err_bad_adb_interface:
379err_get_interface_class:
380err_get_num_ep:
381 (*interface)->USBInterfaceClose(interface);
382 return NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800383}
384
385
386void* RunLoopThread(void* unused)
387{
Dima Zavin3e824912009-05-08 18:25:58 -0700388 unsigned i;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800389
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 Ducrohet2ef5fc22009-05-20 17:33:53 -0700402 for (i = 0; i < vendorIdCount; i++) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800403 IOObjectRelease(notificationIterators[i]);
404 }
405 IONotificationPortDestroy(notificationPort);
406
407 DBG("RunLoopThread done\n");
408 return NULL;
409}
410
411
412static int initialized = 0;
413void usb_init()
414{
415 if (!initialized)
416 {
417 adb_thread_t tid;
418
Xavier Ducrohet2ef5fc22009-05-20 17:33:53 -0700419 notificationIterators = (io_iterator_t*)malloc(
420 vendorIdCount * sizeof(io_iterator_t));
421
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800422 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
440void usb_cleanup()
441{
442 DBG("usb_cleanup\n");
443 close_usb_devices();
444 if (currentRunLoop)
445 CFRunLoopStop(currentRunLoop);
Xavier Ducrohet2ef5fc22009-05-20 17:33:53 -0700446
447 if (notificationIterators != NULL) {
448 free(notificationIterators);
449 notificationIterators = NULL;
450 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800451}
452
453int 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
493int 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 Canal647231a2014-09-11 11:02:17 -0700516 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800517
Esteban de la Canal647231a2014-09-11 11:02:17 -0700518 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 Project9ca14dc2009-03-03 19:32:55 -0800525 return 0;
526 else {
Esteban de la Canal647231a2014-09-11 11:02:17 -0700527 DBG("ERR: usb_read failed with status %x\n", result);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800528 }
529
530 return -1;
531}
532
533int usb_close(usb_handle *handle)
534{
535 return 0;
536}
537
538void usb_kick(usb_handle *handle)
539{
540 /* release the interface */
Dima Zavin3e824912009-05-08 18:25:58 -0700541 if (!handle)
542 return;
543
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800544 if (handle->interface)
545 {
546 (*handle->interface)->USBInterfaceClose(handle->interface);
547 (*handle->interface)->Release(handle->interface);
548 handle->interface = 0;
549 }
550}