blob: af651309349e0e89d8e486a6841e48d390af83fd [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
Dan Albertdb6fe642015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_USB
18
19#include "sysdeps.h"
20
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080021#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 Albert25896062015-04-16 19:20:40 -070029#include <inttypes.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080030#include <stdio.h>
31
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080032#include "adb.h"
Dan Albertbd3d4482015-02-25 10:26:17 -080033#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080034
35#define DBG D
36
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080037static IONotificationPortRef notificationPort = 0;
Al Sutton178bae82014-11-21 12:21:12 +000038static io_iterator_t notificationIterator;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080039
40struct usb_handle
41{
42 UInt8 bulkIn;
43 UInt8 bulkOut;
44 IOUSBInterfaceInterface **interface;
45 io_object_t usbNotification;
46 unsigned int zero_mask;
47};
48
49static CFRunLoopRef currentRunLoop = 0;
50static pthread_mutex_t start_lock;
51static pthread_cond_t start_cond;
52
53
Dima Zavin3e824912009-05-08 18:25:58 -070054static void AndroidInterfaceAdded(void *refCon, io_iterator_t iterator);
55static void AndroidInterfaceNotify(void *refCon, io_iterator_t iterator,
56 natural_t messageType,
57 void *messageArgument);
58static usb_handle* CheckInterface(IOUSBInterfaceInterface **iface,
59 UInt16 vendor, UInt16 product);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080060
61static int
62InitUSB()
63{
64 CFMutableDictionaryRef matchingDict;
65 CFRunLoopSourceRef runLoopSource;
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
Al Sutton178bae82014-11-21 12:21:12 +000073 //* Create our matching dictionary to find the Android device's
74 //* adb interface
75 //* IOServiceAddMatchingNotification consumes the reference, so we do
76 //* not need to release this
77 matchingDict = IOServiceMatching(kIOUSBInterfaceClassName);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080078
Al Sutton178bae82014-11-21 12:21:12 +000079 if (!matchingDict) {
80 DBG("ERR: Couldn't create USB matching dictionary.\n");
81 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080082 }
83
Al Sutton178bae82014-11-21 12:21:12 +000084 //* We have to get notifications for all potential candidates and test them
85 //* at connection time because the matching rules don't allow for a
86 //* USB interface class of 0xff for class+subclass+protocol matches
87 //* See https://developer.apple.com/library/mac/qa/qa1076/_index.html
88 IOServiceAddMatchingNotification(
89 notificationPort,
90 kIOFirstMatchNotification,
91 matchingDict,
92 AndroidInterfaceAdded,
93 NULL,
94 &notificationIterator);
95
96 //* Iterate over set of matching interfaces to access already-present
97 //* devices and to arm the notification
98 AndroidInterfaceAdded(NULL, notificationIterator);
99
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800100 return 0;
101}
102
103static void
Dima Zavin3e824912009-05-08 18:25:58 -0700104AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800105{
106 kern_return_t kr;
107 io_service_t usbDevice;
Dima Zavin3e824912009-05-08 18:25:58 -0700108 io_service_t usbInterface;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800109 IOCFPlugInInterface **plugInInterface = NULL;
Dima Zavin3e824912009-05-08 18:25:58 -0700110 IOUSBInterfaceInterface220 **iface = NULL;
111 IOUSBDeviceInterface197 **dev = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800112 HRESULT result;
113 SInt32 score;
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700114 UInt32 locationId;
Dan Albert25896062015-04-16 19:20:40 -0700115 UInt8 if_class, subclass, protocol;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800116 UInt16 vendor;
117 UInt16 product;
118 UInt8 serialIndex;
119 char serial[256];
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700120 char devpathBuf[64];
121 char *devpath = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800122
Dima Zavin3e824912009-05-08 18:25:58 -0700123 while ((usbInterface = IOIteratorNext(iterator))) {
124 //* Create an intermediate interface plugin
125 kr = IOCreatePlugInInterfaceForService(usbInterface,
126 kIOUSBInterfaceUserClientTypeID,
127 kIOCFPlugInInterfaceID,
128 &plugInInterface, &score);
129 IOObjectRelease(usbInterface);
130 if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
131 DBG("ERR: Unable to create an interface plug-in (%08x)\n", kr);
132 continue;
133 }
134
135 //* This gets us the interface object
Dan Albert25896062015-04-16 19:20:40 -0700136 result = (*plugInInterface)->QueryInterface(
137 plugInInterface,
138 CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID*)&iface);
Dima Zavin3e824912009-05-08 18:25:58 -0700139 //* We only needed the plugin to get the interface, so discard it
140 (*plugInInterface)->Release(plugInInterface);
141 if (result || !iface) {
142 DBG("ERR: Couldn't query the interface (%08x)\n", (int) result);
143 continue;
144 }
145
Dan Albert25896062015-04-16 19:20:40 -0700146 kr = (*iface)->GetInterfaceClass(iface, &if_class);
Al Sutton178bae82014-11-21 12:21:12 +0000147 kr = (*iface)->GetInterfaceSubClass(iface, &subclass);
148 kr = (*iface)->GetInterfaceProtocol(iface, &protocol);
Dan Albert25896062015-04-16 19:20:40 -0700149 if(if_class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) {
Al Sutton178bae82014-11-21 12:21:12 +0000150 // Ignore non-ADB devices.
Dan Albert25896062015-04-16 19:20:40 -0700151 DBG("Ignoring interface with incorrect class/subclass/protocol - %d, %d, %d\n", if_class, subclass, protocol);
Al Sutton178bae82014-11-21 12:21:12 +0000152 (*iface)->Release(iface);
153 continue;
154 }
155
Dima Zavin3e824912009-05-08 18:25:58 -0700156 //* this gets us an ioservice, with which we will find the actual
157 //* device; after getting a plugin, and querying the interface, of
158 //* course.
159 //* Gotta love OS X
160 kr = (*iface)->GetDevice(iface, &usbDevice);
161 if (kIOReturnSuccess != kr || !usbDevice) {
162 DBG("ERR: Couldn't grab device from interface (%08x)\n", kr);
163 continue;
164 }
165
166 plugInInterface = NULL;
167 score = 0;
168 //* create an intermediate device plugin
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800169 kr = IOCreatePlugInInterfaceForService(usbDevice,
170 kIOUSBDeviceUserClientTypeID,
171 kIOCFPlugInInterfaceID,
172 &plugInInterface, &score);
Dima Zavin3e824912009-05-08 18:25:58 -0700173 //* only needed this to find the plugin
174 (void)IOObjectRelease(usbDevice);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800175 if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
Dima Zavin3e824912009-05-08 18:25:58 -0700176 DBG("ERR: Unable to create a device plug-in (%08x)\n", kr);
177 continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800178 }
179
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800180 result = (*plugInInterface)->QueryInterface(plugInInterface,
Dan Albert25896062015-04-16 19:20:40 -0700181 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID*)&dev);
Dima Zavin3e824912009-05-08 18:25:58 -0700182 //* only needed this to query the plugin
183 (*plugInInterface)->Release(plugInInterface);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800184 if (result || !dev) {
Dima Zavin3e824912009-05-08 18:25:58 -0700185 DBG("ERR: Couldn't create a device interface (%08x)\n",
186 (int) result);
187 continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800188 }
189
Dima Zavin3e824912009-05-08 18:25:58 -0700190 //* Now after all that, we actually have a ref to the device and
191 //* the interface that matched our criteria
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800192 kr = (*dev)->GetDeviceVendor(dev, &vendor);
193 kr = (*dev)->GetDeviceProduct(dev, &product);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700194 kr = (*dev)->GetLocationID(dev, &locationId);
195 if (kr == 0) {
Ying Wangf6d4f9a2014-08-14 15:50:13 -0700196 snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X",
197 (unsigned int)locationId);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700198 devpath = devpathBuf;
199 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800200 kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
201
Guang Zhu84b5bd22009-08-06 17:21:52 -0700202 if (serialIndex > 0) {
203 IOUSBDevRequest req;
204 UInt16 buffer[256];
205 UInt16 languages[128];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800206
Guang Zhu84b5bd22009-08-06 17:21:52 -0700207 memset(languages, 0, sizeof(languages));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800208
Guang Zhu84b5bd22009-08-06 17:21:52 -0700209 req.bmRequestType =
210 USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
211 req.bRequest = kUSBRqGetDescriptor;
212 req.wValue = (kUSBStringDesc << 8) | 0;
213 req.wIndex = 0;
214 req.pData = languages;
215 req.wLength = sizeof(languages);
216 kr = (*dev)->DeviceRequest(dev, &req);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800217
Guang Zhu84b5bd22009-08-06 17:21:52 -0700218 if (kr == kIOReturnSuccess && req.wLenDone > 0) {
219
220 int langCount = (req.wLenDone - 2) / 2, lang;
221
222 for (lang = 1; lang <= langCount; lang++) {
223
224 memset(buffer, 0, sizeof(buffer));
225 memset(&req, 0, sizeof(req));
226
227 req.bmRequestType =
228 USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
229 req.bRequest = kUSBRqGetDescriptor;
230 req.wValue = (kUSBStringDesc << 8) | serialIndex;
231 req.wIndex = languages[lang];
232 req.pData = buffer;
233 req.wLength = sizeof(buffer);
234 kr = (*dev)->DeviceRequest(dev, &req);
235
236 if (kr == kIOReturnSuccess && req.wLenDone > 0) {
237 int i, count;
238
239 // skip first word, and copy the rest to the serial string,
240 // changing shorts to bytes.
241 count = (req.wLenDone - 1) / 2;
242 for (i = 0; i < count; i++)
243 serial[i] = buffer[i + 1];
244 serial[i] = 0;
245 break;
246 }
247 }
248 }
249 }
Dima Zavin3e824912009-05-08 18:25:58 -0700250 (*dev)->Release(dev);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800251
Dima Zavin3e824912009-05-08 18:25:58 -0700252 DBG("INFO: Found vid=%04x pid=%04x serial=%s\n", vendor, product,
253 serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800254
Dima Zavin3e824912009-05-08 18:25:58 -0700255 usb_handle* handle = CheckInterface((IOUSBInterfaceInterface**)iface,
256 vendor, product);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800257 if (handle == NULL) {
258 DBG("ERR: Could not find device interface: %08x\n", kr);
Dima Zavin3e824912009-05-08 18:25:58 -0700259 (*iface)->Release(iface);
260 continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800261 }
262
263 DBG("AndroidDeviceAdded calling register_usb_transport\n");
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700264 register_usb_transport(handle, (serial[0] ? serial : NULL), devpath, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800265
Dima Zavin3e824912009-05-08 18:25:58 -0700266 // Register for an interest notification of this device being removed.
267 // Pass the reference to our private data as the refCon for the
268 // notification.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800269 kr = IOServiceAddInterestNotification(notificationPort,
Dima Zavin3e824912009-05-08 18:25:58 -0700270 usbInterface,
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800271 kIOGeneralInterest,
Dima Zavin3e824912009-05-08 18:25:58 -0700272 AndroidInterfaceNotify,
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800273 handle,
274 &handle->usbNotification);
Dima Zavin3e824912009-05-08 18:25:58 -0700275
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800276 if (kIOReturnSuccess != kr) {
277 DBG("ERR: Unable to create interest notification (%08x)\n", kr);
278 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800279 }
280}
281
282static void
Dima Zavin3e824912009-05-08 18:25:58 -0700283AndroidInterfaceNotify(void *refCon, io_service_t service, natural_t messageType, void *messageArgument)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800284{
285 usb_handle *handle = (usb_handle *)refCon;
286
287 if (messageType == kIOMessageServiceIsTerminated) {
Dima Zavin3e824912009-05-08 18:25:58 -0700288 if (!handle) {
289 DBG("ERR: NULL handle\n");
290 return;
291 }
292 DBG("AndroidInterfaceNotify\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800293 IOObjectRelease(handle->usbNotification);
294 usb_kick(handle);
295 }
296}
297
Dima Zavin3e824912009-05-08 18:25:58 -0700298//* TODO: simplify this further since we only register to get ADB interface
299//* subclass+protocol events
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800300static usb_handle*
Dima Zavin3e824912009-05-08 18:25:58 -0700301CheckInterface(IOUSBInterfaceInterface **interface, UInt16 vendor, UInt16 product)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800302{
303 usb_handle* handle = NULL;
304 IOReturn kr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800305 UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol;
Dima Zavin3e824912009-05-08 18:25:58 -0700306 UInt8 endpoint;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800307
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800308
Dima Zavin3e824912009-05-08 18:25:58 -0700309 //* Now open the interface. This will cause the pipes associated with
310 //* the endpoints in the interface descriptor to be instantiated
311 kr = (*interface)->USBInterfaceOpen(interface);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800312 if (kr != kIOReturnSuccess) {
Dima Zavin3e824912009-05-08 18:25:58 -0700313 DBG("ERR: Could not open interface: (%08x)\n", kr);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800314 return NULL;
315 }
316
Dima Zavin3e824912009-05-08 18:25:58 -0700317 //* Get the number of endpoints associated with this interface
318 kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
319 if (kr != kIOReturnSuccess) {
320 DBG("ERR: Unable to get number of endpoints: (%08x)\n", kr);
321 goto err_get_num_ep;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800322 }
323
Dima Zavin3e824912009-05-08 18:25:58 -0700324 //* Get interface class, subclass and protocol
325 if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess ||
326 (*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess ||
327 (*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess) {
328 DBG("ERR: Unable to get interface class, subclass and protocol\n");
329 goto err_get_interface_class;
330 }
331
332 //* check to make sure interface class, subclass and protocol match ADB
333 //* avoid opening mass storage endpoints
334 if (!is_adb_interface(vendor, product, interfaceClass,
335 interfaceSubClass, interfaceProtocol))
336 goto err_bad_adb_interface;
337
Dan Albert25896062015-04-16 19:20:40 -0700338 handle = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesd0269c92015-04-21 19:39:52 -0700339 if (handle == nullptr) goto err_bad_adb_interface;
Dima Zavin3e824912009-05-08 18:25:58 -0700340
341 //* Iterate over the endpoints for this interface and find the first
342 //* bulk in/out pipes available. These will be our read/write pipes.
343 for (endpoint = 0; endpoint <= interfaceNumEndpoints; endpoint++) {
344 UInt8 transferType;
345 UInt16 maxPacketSize;
346 UInt8 interval;
347 UInt8 number;
348 UInt8 direction;
349
350 kr = (*interface)->GetPipeProperties(interface, endpoint, &direction,
351 &number, &transferType, &maxPacketSize, &interval);
352
353 if (kIOReturnSuccess == kr) {
354 if (kUSBBulk != transferType)
355 continue;
356
357 if (kUSBIn == direction)
358 handle->bulkIn = endpoint;
359
360 if (kUSBOut == direction)
361 handle->bulkOut = endpoint;
362
363 handle->zero_mask = maxPacketSize - 1;
364 } else {
365 DBG("ERR: FindDeviceInterface - could not get pipe properties\n");
366 goto err_get_pipe_props;
367 }
368 }
369
370 handle->interface = interface;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800371 return handle;
Dima Zavin3e824912009-05-08 18:25:58 -0700372
373err_get_pipe_props:
374 free(handle);
375err_bad_adb_interface:
376err_get_interface_class:
377err_get_num_ep:
378 (*interface)->USBInterfaceClose(interface);
379 return NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800380}
381
382
383void* RunLoopThread(void* unused)
384{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800385 InitUSB();
386
387 currentRunLoop = CFRunLoopGetCurrent();
388
389 // Signal the parent that we are running
390 adb_mutex_lock(&start_lock);
391 adb_cond_signal(&start_cond);
392 adb_mutex_unlock(&start_lock);
393
394 CFRunLoopRun();
395 currentRunLoop = 0;
396
Al Sutton178bae82014-11-21 12:21:12 +0000397 IOObjectRelease(notificationIterator);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800398 IONotificationPortDestroy(notificationPort);
399
400 DBG("RunLoopThread done\n");
Elliott Hughesf2517142015-05-05 13:41:21 -0700401 return NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800402}
403
Dan Albert53a37442015-05-08 16:13:53 -0700404static void usb_cleanup() {
405 DBG("usb_cleanup\n");
406 close_usb_devices();
407 if (currentRunLoop)
408 CFRunLoopStop(currentRunLoop);
409}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800410
Elliott Hughesf2517142015-05-05 13:41:21 -0700411void usb_init() {
Dan Albert53a37442015-05-08 16:13:53 -0700412 static bool initialized = false;
413 if (!initialized) {
414 atexit(usb_cleanup);
415
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800416 adb_mutex_init(&start_lock, NULL);
417 adb_cond_init(&start_cond, NULL);
418
Elliott Hughesf2517142015-05-05 13:41:21 -0700419 if (!adb_thread_create(RunLoopThread, nullptr)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800420 fatal_errno("cannot create input thread");
Elliott Hughesf2517142015-05-05 13:41:21 -0700421 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800422
423 // Wait for initialization to finish
424 adb_mutex_lock(&start_lock);
425 adb_cond_wait(&start_cond, &start_lock);
426 adb_mutex_unlock(&start_lock);
427
428 adb_mutex_destroy(&start_lock);
429 adb_cond_destroy(&start_cond);
430
Dan Albert53a37442015-05-08 16:13:53 -0700431 initialized = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800432 }
433}
434
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800435int usb_write(usb_handle *handle, const void *buf, int len)
436{
437 IOReturn result;
438
439 if (!len)
440 return 0;
441
442 if (!handle)
443 return -1;
444
445 if (NULL == handle->interface) {
446 DBG("ERR: usb_write interface was null\n");
447 return -1;
448 }
449
450 if (0 == handle->bulkOut) {
451 DBG("ERR: bulkOut endpoint not assigned\n");
452 return -1;
453 }
454
455 result =
456 (*handle->interface)->WritePipe(
457 handle->interface, handle->bulkOut, (void *)buf, len);
458
459 if ((result == 0) && (handle->zero_mask)) {
460 /* we need 0-markers and our transfer */
461 if(!(len & handle->zero_mask)) {
462 result =
463 (*handle->interface)->WritePipe(
464 handle->interface, handle->bulkOut, (void *)buf, 0);
465 }
466 }
467
468 if (0 == result)
469 return 0;
470
471 DBG("ERR: usb_write failed with status %d\n", result);
472 return -1;
473}
474
475int usb_read(usb_handle *handle, void *buf, int len)
476{
477 IOReturn result;
478 UInt32 numBytes = len;
479
480 if (!len) {
481 return 0;
482 }
483
484 if (!handle) {
485 return -1;
486 }
487
488 if (NULL == handle->interface) {
489 DBG("ERR: usb_read interface was null\n");
490 return -1;
491 }
492
493 if (0 == handle->bulkIn) {
494 DBG("ERR: bulkIn endpoint not assigned\n");
495 return -1;
496 }
497
Esteban de la Canal647231a2014-09-11 11:02:17 -0700498 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800499
Esteban de la Canal647231a2014-09-11 11:02:17 -0700500 if (kIOUSBPipeStalled == result) {
501 DBG(" Pipe stalled, clearing stall.\n");
502 (*handle->interface)->ClearPipeStall(handle->interface, handle->bulkIn);
503 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
504 }
505
506 if (kIOReturnSuccess == result)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800507 return 0;
508 else {
Esteban de la Canal647231a2014-09-11 11:02:17 -0700509 DBG("ERR: usb_read failed with status %x\n", result);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800510 }
511
512 return -1;
513}
514
515int usb_close(usb_handle *handle)
516{
517 return 0;
518}
519
520void usb_kick(usb_handle *handle)
521{
522 /* release the interface */
Dima Zavin3e824912009-05-08 18:25:58 -0700523 if (!handle)
524 return;
525
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800526 if (handle->interface)
527 {
528 (*handle->interface)->USBInterfaceClose(handle->interface);
529 (*handle->interface)->Release(handle->interface);
530 handle->interface = 0;
531 }
532}