blob: 032ae3187de46d420caf6a13a80c9488005846e6 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughesfbcb93a2015-06-24 13:28:24 -070029#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <stdio.h>
31#include <CoreFoundation/CoreFoundation.h>
32#include <IOKit/IOKitLib.h>
33#include <IOKit/IOCFPlugIn.h>
34#include <IOKit/usb/IOUSBLib.h>
35#include <IOKit/IOMessage.h>
36#include <mach/mach_port.h>
37
David Pursell0b156632015-10-30 11:22:01 -070038#include <memory>
39
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include "usb.h"
41
42
43/*
44 * Internal helper functions and associated definitions.
45 */
46
47#if TRACE_USB
48#define WARN(x...) fprintf(stderr, x)
49#else
50#define WARN(x...)
51#endif
52
53#define ERR(x...) fprintf(stderr, "ERROR: " x)
54
55/** An open usb device */
56struct usb_handle
57{
58 int success;
59 ifc_match_func callback;
60 usb_ifc_info info;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080061
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062 UInt8 bulkIn;
63 UInt8 bulkOut;
64 IOUSBInterfaceInterface190 **interface;
65 unsigned int zero_mask;
66};
67
David Pursell0b156632015-10-30 11:22:01 -070068class OsxUsbTransport : public Transport {
69 public:
70 OsxUsbTransport(std::unique_ptr<usb_handle> handle) : handle_(std::move(handle)) {}
71 ~OsxUsbTransport() override = default;
72
73 ssize_t Read(void* data, size_t len) override;
74 ssize_t Write(const void* data, size_t len) override;
75 int Close() override;
76
77 private:
78 std::unique_ptr<usb_handle> handle_;
79
80 DISALLOW_COPY_AND_ASSIGN(OsxUsbTransport);
81};
82
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083/** Try out all the interfaces and see if there's a match. Returns 0 on
84 * success, -1 on failure. */
Jeff Brownb6406372010-05-21 13:20:47 -070085static int try_interfaces(IOUSBDeviceInterface182 **dev, usb_handle *handle) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086 IOReturn kr;
87 IOUSBFindInterfaceRequest request;
88 io_iterator_t iterator;
89 io_service_t usbInterface;
90 IOCFPlugInInterface **plugInInterface;
91 IOUSBInterfaceInterface190 **interface = NULL;
92 HRESULT result;
93 SInt32 score;
94 UInt8 interfaceNumEndpoints;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095
96 // Placing the constant KIOUSBFindInterfaceDontCare into the following
97 // fields of the IOUSBFindInterfaceRequest structure will allow us to
98 // find all of the interfaces
99 request.bInterfaceClass = kIOUSBFindInterfaceDontCare;
100 request.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
101 request.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
102 request.bAlternateSetting = kIOUSBFindInterfaceDontCare;
103
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 // Get an iterator for the interfaces on the device
105 kr = (*dev)->CreateInterfaceIterator(dev, &request, &iterator);
106
107 if (kr != 0) {
108 ERR("Couldn't create a device interface iterator: (%08x)\n", kr);
109 return -1;
110 }
111
112 while ((usbInterface = IOIteratorNext(iterator))) {
113 // Create an intermediate plugin
114 kr = IOCreatePlugInInterfaceForService(
115 usbInterface,
116 kIOUSBInterfaceUserClientTypeID,
117 kIOCFPlugInInterfaceID,
118 &plugInInterface,
119 &score);
120
121 // No longer need the usbInterface object now that we have the plugin
122 (void) IOObjectRelease(usbInterface);
123
124 if ((kr != 0) || (!plugInInterface)) {
125 WARN("Unable to create plugin (%08x)\n", kr);
126 continue;
127 }
128
129 // Now create the interface interface for the interface
130 result = (*plugInInterface)->QueryInterface(
131 plugInInterface,
132 CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID),
Elliott Hughesfbcb93a2015-06-24 13:28:24 -0700133 (LPVOID*) &interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134
135 // No longer need the intermediate plugin
136 (*plugInInterface)->Release(plugInInterface);
137
138 if (result || !interface) {
139 ERR("Couldn't create interface interface: (%08x)\n",
140 (unsigned int) result);
141 // continue so we can try the next interface
142 continue;
143 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800144
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 /*
146 * Now open the interface. This will cause the pipes
147 * associated with the endpoints in the interface descriptor
148 * to be instantiated.
149 */
150
151 /*
152 * TODO: Earlier comments here indicated that it was a bad
153 * idea to just open any interface, because opening "mass
154 * storage endpoints" is bad. However, the only way to find
155 * out if an interface does bulk in or out is to open it, and
156 * the framework in this application wants to be told about
157 * bulk in / out before deciding whether it actually wants to
158 * use the interface. Maybe something needs to be done about
159 * this situation.
160 */
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800161
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 kr = (*interface)->USBInterfaceOpen(interface);
163
164 if (kr != 0) {
165 WARN("Could not open interface: (%08x)\n", kr);
166 (void) (*interface)->Release(interface);
167 // continue so we can try the next interface
168 continue;
169 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800170
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 // Get the number of endpoints associated with this interface.
172 kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
173
174 if (kr != 0) {
175 ERR("Unable to get number of endpoints: (%08x)\n", kr);
176 goto next_interface;
177 }
178
179 // Get interface class, subclass and protocol
180 if ((*interface)->GetInterfaceClass(interface, &handle->info.ifc_class) != 0 ||
181 (*interface)->GetInterfaceSubClass(interface, &handle->info.ifc_subclass) != 0 ||
182 (*interface)->GetInterfaceProtocol(interface, &handle->info.ifc_protocol) != 0)
183 {
184 ERR("Unable to get interface class, subclass and protocol\n");
185 goto next_interface;
186 }
187
188 handle->info.has_bulk_in = 0;
189 handle->info.has_bulk_out = 0;
190
191 // Iterate over the endpoints for this interface and see if there
192 // are any that do bulk in/out.
Elliott Hughes2d4f8522015-08-13 15:01:18 -0700193 for (UInt8 endpoint = 1; endpoint <= interfaceNumEndpoints; endpoint++) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 UInt8 transferType;
195 UInt16 maxPacketSize;
196 UInt8 interval;
197 UInt8 number;
198 UInt8 direction;
199
200 kr = (*interface)->GetPipeProperties(interface, endpoint,
201 &direction,
202 &number, &transferType, &maxPacketSize, &interval);
203
204 if (kr == 0) {
205 if (transferType != kUSBBulk) {
206 continue;
207 }
208
209 if (direction == kUSBIn) {
210 handle->info.has_bulk_in = 1;
211 handle->bulkIn = endpoint;
212 } else if (direction == kUSBOut) {
213 handle->info.has_bulk_out = 1;
214 handle->bulkOut = endpoint;
215 }
216
217 if (handle->info.ifc_protocol == 0x01) {
218 handle->zero_mask = maxPacketSize - 1;
219 }
220 } else {
Elliott Hughes2ae8d2e2015-08-07 10:49:36 -0700221 ERR("could not get pipe properties for endpoint %u (%08x)\n", endpoint, kr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 }
223
224 if (handle->info.has_bulk_in && handle->info.has_bulk_out) {
225 break;
226 }
227 }
228
229 if (handle->callback(&handle->info) == 0) {
230 handle->interface = interface;
231 handle->success = 1;
232
233 /*
234 * Clear both the endpoints, because it has been observed
235 * that the Mac may otherwise (incorrectly) start out with
236 * them in bad state.
237 */
238
239 if (handle->info.has_bulk_in) {
240 kr = (*interface)->ClearPipeStallBothEnds(interface,
241 handle->bulkIn);
242 if (kr != 0) {
Nick Pelly286d50f2010-07-22 10:59:55 -0700243 ERR("could not clear input pipe; result %x, ignoring...\n", kr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 }
245 }
246
247 if (handle->info.has_bulk_out) {
248 kr = (*interface)->ClearPipeStallBothEnds(interface,
249 handle->bulkOut);
250 if (kr != 0) {
Nick Pelly286d50f2010-07-22 10:59:55 -0700251 ERR("could not clear output pipe; result %x, ignoring....\n", kr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 }
253 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800254
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 return 0;
256 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800257
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258next_interface:
259 (*interface)->USBInterfaceClose(interface);
260 (*interface)->Release(interface);
261 }
262
263 return 0;
264}
265
266/** Try out the given device and see if there's a match. Returns 0 on
267 * success, -1 on failure.
268 */
269static int try_device(io_service_t device, usb_handle *handle) {
270 kern_return_t kr;
271 IOCFPlugInInterface **plugin = NULL;
272 IOUSBDeviceInterface182 **dev = NULL;
273 SInt32 score;
274 HRESULT result;
275 UInt8 serialIndex;
Scott Anderson13081c62012-04-06 12:39:30 -0700276 UInt32 locationId;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277
278 // Create an intermediate plugin.
279 kr = IOCreatePlugInInterfaceForService(device,
280 kIOUSBDeviceUserClientTypeID,
281 kIOCFPlugInInterfaceID,
282 &plugin, &score);
283
284 if ((kr != 0) || (plugin == NULL)) {
285 ERR("Unable to create a plug-in (%08x)\n", kr);
286 goto error;
287 }
288
289 // Now create the device interface.
290 result = (*plugin)->QueryInterface(plugin,
Elliott Hughesfbcb93a2015-06-24 13:28:24 -0700291 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID*) &dev);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 if ((result != 0) || (dev == NULL)) {
293 ERR("Couldn't create a device interface (%08x)\n", (int) result);
294 goto error;
295 }
296
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800297 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 * We don't need the intermediate interface after the device interface
299 * is created.
300 */
301 IODestroyPlugInInterface(plugin);
302
303 // So, we have a device, finally. Grab its vitals.
304
Matt Reimer81c24f62014-09-04 16:09:49 -0700305
306 kr = (*dev)->USBDeviceOpen(dev);
307 if (kr != 0) {
308 WARN("USBDeviceOpen");
309 goto out;
310 }
311
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 kr = (*dev)->GetDeviceVendor(dev, &handle->info.dev_vendor);
313 if (kr != 0) {
314 ERR("GetDeviceVendor");
315 goto error;
316 }
317
318 kr = (*dev)->GetDeviceProduct(dev, &handle->info.dev_product);
319 if (kr != 0) {
320 ERR("GetDeviceProduct");
321 goto error;
322 }
323
324 kr = (*dev)->GetDeviceClass(dev, &handle->info.dev_class);
325 if (kr != 0) {
326 ERR("GetDeviceClass");
327 goto error;
328 }
329
330 kr = (*dev)->GetDeviceSubClass(dev, &handle->info.dev_subclass);
331 if (kr != 0) {
332 ERR("GetDeviceSubClass");
333 goto error;
334 }
335
336 kr = (*dev)->GetDeviceProtocol(dev, &handle->info.dev_protocol);
337 if (kr != 0) {
338 ERR("GetDeviceProtocol");
339 goto error;
340 }
341
Scott Anderson13081c62012-04-06 12:39:30 -0700342 kr = (*dev)->GetLocationID(dev, &locationId);
343 if (kr != 0) {
344 ERR("GetLocationId");
345 goto error;
346 }
Ying Wang42a809b2014-08-14 15:50:13 -0700347 snprintf(handle->info.device_path, sizeof(handle->info.device_path),
348 "usb:%" PRIu32 "X", (unsigned int)locationId);
Scott Anderson13081c62012-04-06 12:39:30 -0700349
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
351
352 if (serialIndex > 0) {
353 IOUSBDevRequest req;
354 UInt16 buffer[256];
355
356 req.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
357 req.bRequest = kUSBRqGetDescriptor;
358 req.wValue = (kUSBStringDesc << 8) | serialIndex;
mgrossc8406532011-07-07 17:08:10 -0700359 //language ID (en-us) for serial number string
360 req.wIndex = 0x0409;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 req.pData = buffer;
362 req.wLength = sizeof(buffer);
363 kr = (*dev)->DeviceRequest(dev, &req);
364
365 if (kr == kIOReturnSuccess && req.wLenDone > 0) {
366 int i, count;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800367
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368 // skip first word, and copy the rest to the serial string, changing shorts to bytes.
369 count = (req.wLenDone - 1) / 2;
370 for (i = 0; i < count; i++)
371 handle->info.serial_number[i] = buffer[i + 1];
372 handle->info.serial_number[i] = 0;
373 }
374 } else {
375 // device has no serial number
376 handle->info.serial_number[0] = 0;
377 }
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700378 handle->info.writable = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379
380 if (try_interfaces(dev, handle)) {
381 goto error;
382 }
383
Matt Reimer81c24f62014-09-04 16:09:49 -0700384 out:
385
386 (*dev)->USBDeviceClose(dev);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 (*dev)->Release(dev);
388 return 0;
389
390 error:
391
392 if (dev != NULL) {
Matt Reimer81c24f62014-09-04 16:09:49 -0700393 (*dev)->USBDeviceClose(dev);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 (*dev)->Release(dev);
395 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800396
397 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398}
399
400
401/** Initializes the USB system. Returns 0 on success, -1 on error. */
David Pursell0b156632015-10-30 11:22:01 -0700402static int init_usb(ifc_match_func callback, std::unique_ptr<usb_handle>* handle) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403 int ret = -1;
404 CFMutableDictionaryRef matchingDict;
405 kern_return_t result;
406 io_iterator_t iterator;
407 usb_handle h;
408
409 h.success = 0;
410 h.callback = callback;
411
412 /*
413 * Create our matching dictionary to find appropriate devices.
414 * IOServiceAddMatchingNotification consumes the reference, so we
415 * do not need to release it.
416 */
417 matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
418
419 if (matchingDict == NULL) {
420 ERR("Couldn't create USB matching dictionary.\n");
421 return -1;
422 }
423
424 result = IOServiceGetMatchingServices(
425 kIOMasterPortDefault, matchingDict, &iterator);
426
427 if (result != 0) {
428 ERR("Could not create iterator.");
429 return -1;
430 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800431
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432 for (;;) {
433 if (! IOIteratorIsValid(iterator)) {
434 /*
435 * Apple documentation advises resetting the iterator if
436 * it should become invalid during iteration.
437 */
438 IOIteratorReset(iterator);
439 continue;
440 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800441
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 io_service_t device = IOIteratorNext(iterator);
443
444 if (device == 0) {
445 break;
446 }
447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 if (try_device(device, &h) != 0) {
449 IOObjectRelease(device);
450 ret = -1;
451 break;
452 }
453
454 if (h.success) {
David Pursell0b156632015-10-30 11:22:01 -0700455 handle->reset(new usb_handle);
456 memcpy(handle->get(), &h, sizeof(usb_handle));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 ret = 0;
458 break;
459 }
460
461 IOObjectRelease(device);
462 }
463
464 IOObjectRelease(iterator);
465
466 return ret;
467}
468
469
470
471/*
472 * Definitions of this file's public functions.
473 */
474
David Pursell0b156632015-10-30 11:22:01 -0700475Transport* usb_open(ifc_match_func callback) {
476 std::unique_ptr<usb_handle> handle;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477
478 if (init_usb(callback, &handle) < 0) {
479 /* Something went wrong initializing USB. */
David Pursell0b156632015-10-30 11:22:01 -0700480 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 }
482
David Pursell0b156632015-10-30 11:22:01 -0700483 return new OsxUsbTransport(std::move(handle));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484}
485
David Pursell0b156632015-10-30 11:22:01 -0700486int OsxUsbTransport::Close() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 /* TODO: Something better here? */
488 return 0;
489}
490
David Pursell0b156632015-10-30 11:22:01 -0700491ssize_t OsxUsbTransport::Read(void* data, size_t len) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 IOReturn result;
493 UInt32 numBytes = len;
494
495 if (len == 0) {
496 return 0;
497 }
498
David Pursell0b156632015-10-30 11:22:01 -0700499 if (handle_ == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 return -1;
501 }
502
David Pursell0b156632015-10-30 11:22:01 -0700503 if (handle_->interface == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 ERR("usb_read interface was null\n");
505 return -1;
506 }
507
David Pursell0b156632015-10-30 11:22:01 -0700508 if (handle_->bulkIn == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 ERR("bulkIn endpoint not assigned\n");
510 return -1;
511 }
512
David Pursell0b156632015-10-30 11:22:01 -0700513 result = (*handle_->interface)->ReadPipe(handle_->interface, handle_->bulkIn, data, &numBytes);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514
515 if (result == 0) {
516 return (int) numBytes;
517 } else {
518 ERR("usb_read failed with status %x\n", result);
519 }
520
521 return -1;
522}
523
David Pursell0b156632015-10-30 11:22:01 -0700524ssize_t OsxUsbTransport::Write(const void* data, size_t len) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 IOReturn result;
526
527 if (len == 0) {
528 return 0;
529 }
530
David Pursell0b156632015-10-30 11:22:01 -0700531 if (handle_ == NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532 return -1;
533 }
534
David Pursell0b156632015-10-30 11:22:01 -0700535 if (handle_->interface == NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536 ERR("usb_write interface was null\n");
537 return -1;
538 }
539
David Pursell0b156632015-10-30 11:22:01 -0700540 if (handle_->bulkOut == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541 ERR("bulkOut endpoint not assigned\n");
542 return -1;
543 }
544
Jeff Brownb6406372010-05-21 13:20:47 -0700545#if 0
David Pursell0b156632015-10-30 11:22:01 -0700546 result = (*handle_->interface)->WritePipe(
547 handle_->interface, handle_->bulkOut, (void *)data, len);
Jeff Brownb6406372010-05-21 13:20:47 -0700548#else
549 /* Attempt to work around crashes in the USB driver that may be caused
550 * by trying to write too much data at once. The kernel IOCopyMapper
551 * panics if a single iovmAlloc needs more than half of its mapper pages.
552 */
553 const int maxLenToSend = 1048576; // 1 MiB
554 int lenRemaining = len;
555 result = 0;
556 while (lenRemaining > 0) {
557 int lenToSend = lenRemaining > maxLenToSend
558 ? maxLenToSend : lenRemaining;
559
David Pursell0b156632015-10-30 11:22:01 -0700560 result = (*handle_->interface)->WritePipe(
561 handle_->interface, handle_->bulkOut, (void *)data, lenToSend);
Jeff Brownb6406372010-05-21 13:20:47 -0700562 if (result != 0) break;
563
564 lenRemaining -= lenToSend;
565 data = (const char*)data + lenToSend;
566 }
567#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568
569 #if 0
David Pursell0b156632015-10-30 11:22:01 -0700570 if ((result == 0) && (handle_->zero_mask)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800571 /* we need 0-markers and our transfer */
David Pursell0b156632015-10-30 11:22:01 -0700572 if(!(len & handle_->zero_mask)) {
573 result = (*handle_->interface)->WritePipe(
574 handle_->interface, handle_->bulkOut, (void *)data, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575 }
576 }
577 #endif
578
579 if (result != 0) {
580 ERR("usb_write failed with status %x\n", result);
581 return -1;
582 }
583
584 return len;
585}