usb_osx.c revision 286d50fe34edef9b93cde6a3e2d052b2b96d071c
1/*
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
12 *    the documentation and/or other materials provided with the
13 *    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
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * 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
29#include <stdio.h>
30#include <CoreFoundation/CoreFoundation.h>
31#include <IOKit/IOKitLib.h>
32#include <IOKit/IOCFPlugIn.h>
33#include <IOKit/usb/IOUSBLib.h>
34#include <IOKit/IOMessage.h>
35#include <mach/mach_port.h>
36
37#include "usb.h"
38
39
40/*
41 * Internal helper functions and associated definitions.
42 */
43
44#if TRACE_USB
45#define WARN(x...) fprintf(stderr, x)
46#else
47#define WARN(x...)
48#endif
49
50#define ERR(x...) fprintf(stderr, "ERROR: " x)
51
52/** An open usb device */
53struct usb_handle
54{
55    int success;
56    ifc_match_func callback;
57    usb_ifc_info info;
58
59    UInt8 bulkIn;
60    UInt8 bulkOut;
61    IOUSBInterfaceInterface190 **interface;
62    unsigned int zero_mask;
63};
64
65/** Try out all the interfaces and see if there's a match. Returns 0 on
66 * success, -1 on failure. */
67static int try_interfaces(IOUSBDeviceInterface182 **dev, usb_handle *handle) {
68    IOReturn kr;
69    IOUSBFindInterfaceRequest request;
70    io_iterator_t iterator;
71    io_service_t usbInterface;
72    IOCFPlugInInterface **plugInInterface;
73    IOUSBInterfaceInterface190 **interface = NULL;
74    HRESULT result;
75    SInt32 score;
76    UInt8 interfaceNumEndpoints;
77    UInt8 endpoint;
78    UInt8 configuration;
79
80    // Placing the constant KIOUSBFindInterfaceDontCare into the following
81    // fields of the IOUSBFindInterfaceRequest structure will allow us to
82    // find all of the interfaces
83    request.bInterfaceClass = kIOUSBFindInterfaceDontCare;
84    request.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
85    request.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
86    request.bAlternateSetting = kIOUSBFindInterfaceDontCare;
87
88    // SetConfiguration will kill an existing UMS connection, so let's
89    // not do this if not necessary.
90    configuration = 0;
91    (*dev)->GetConfiguration(dev, &configuration);
92    if (configuration != 1)
93        (*dev)->SetConfiguration(dev, 1);
94
95    // Get an iterator for the interfaces on the device
96    kr = (*dev)->CreateInterfaceIterator(dev, &request, &iterator);
97
98    if (kr != 0) {
99        ERR("Couldn't create a device interface iterator: (%08x)\n", kr);
100        return -1;
101    }
102
103    while ((usbInterface = IOIteratorNext(iterator))) {
104        // Create an intermediate plugin
105        kr = IOCreatePlugInInterfaceForService(
106                usbInterface,
107                kIOUSBInterfaceUserClientTypeID,
108                kIOCFPlugInInterfaceID,
109                &plugInInterface,
110                &score);
111
112        // No longer need the usbInterface object now that we have the plugin
113        (void) IOObjectRelease(usbInterface);
114
115        if ((kr != 0) || (!plugInInterface)) {
116            WARN("Unable to create plugin (%08x)\n", kr);
117            continue;
118        }
119
120        // Now create the interface interface for the interface
121        result = (*plugInInterface)->QueryInterface(
122                plugInInterface,
123                CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID),
124                (LPVOID) &interface);
125
126        // No longer need the intermediate plugin
127        (*plugInInterface)->Release(plugInInterface);
128
129        if (result || !interface) {
130            ERR("Couldn't create interface interface: (%08x)\n",
131               (unsigned int) result);
132            // continue so we can try the next interface
133            continue;
134        }
135
136        /*
137         * Now open the interface. This will cause the pipes
138         * associated with the endpoints in the interface descriptor
139         * to be instantiated.
140         */
141
142        /*
143         * TODO: Earlier comments here indicated that it was a bad
144         * idea to just open any interface, because opening "mass
145         * storage endpoints" is bad. However, the only way to find
146         * out if an interface does bulk in or out is to open it, and
147         * the framework in this application wants to be told about
148         * bulk in / out before deciding whether it actually wants to
149         * use the interface. Maybe something needs to be done about
150         * this situation.
151         */
152
153        kr = (*interface)->USBInterfaceOpen(interface);
154
155        if (kr != 0) {
156            WARN("Could not open interface: (%08x)\n", kr);
157            (void) (*interface)->Release(interface);
158            // continue so we can try the next interface
159            continue;
160        }
161
162        // Get the number of endpoints associated with this interface.
163        kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
164
165        if (kr != 0) {
166            ERR("Unable to get number of endpoints: (%08x)\n", kr);
167            goto next_interface;
168        }
169
170        // Get interface class, subclass and protocol
171        if ((*interface)->GetInterfaceClass(interface, &handle->info.ifc_class) != 0 ||
172            (*interface)->GetInterfaceSubClass(interface, &handle->info.ifc_subclass) != 0 ||
173            (*interface)->GetInterfaceProtocol(interface, &handle->info.ifc_protocol) != 0)
174        {
175            ERR("Unable to get interface class, subclass and protocol\n");
176            goto next_interface;
177        }
178
179        handle->info.has_bulk_in = 0;
180        handle->info.has_bulk_out = 0;
181
182        // Iterate over the endpoints for this interface and see if there
183        // are any that do bulk in/out.
184        for (endpoint = 0; endpoint <= interfaceNumEndpoints; endpoint++) {
185            UInt8   transferType;
186            UInt16  maxPacketSize;
187            UInt8   interval;
188            UInt8   number;
189            UInt8   direction;
190
191            kr = (*interface)->GetPipeProperties(interface, endpoint,
192                    &direction,
193                    &number, &transferType, &maxPacketSize, &interval);
194
195            if (kr == 0) {
196                if (transferType != kUSBBulk) {
197                    continue;
198                }
199
200                if (direction == kUSBIn) {
201                    handle->info.has_bulk_in = 1;
202                    handle->bulkIn = endpoint;
203                } else if (direction == kUSBOut) {
204                    handle->info.has_bulk_out = 1;
205                    handle->bulkOut = endpoint;
206                }
207
208                if (handle->info.ifc_protocol == 0x01) {
209                    handle->zero_mask = maxPacketSize - 1;
210                }
211            } else {
212                ERR("could not get pipe properties\n");
213            }
214
215            if (handle->info.has_bulk_in && handle->info.has_bulk_out) {
216                break;
217            }
218        }
219
220        if (handle->callback(&handle->info) == 0) {
221            handle->interface = interface;
222            handle->success = 1;
223
224            /*
225             * Clear both the endpoints, because it has been observed
226             * that the Mac may otherwise (incorrectly) start out with
227             * them in bad state.
228             */
229
230            if (handle->info.has_bulk_in) {
231                kr = (*interface)->ClearPipeStallBothEnds(interface,
232                        handle->bulkIn);
233                if (kr != 0) {
234                    ERR("could not clear input pipe; result %x, ignoring...\n", kr);
235                }
236            }
237
238            if (handle->info.has_bulk_out) {
239                kr = (*interface)->ClearPipeStallBothEnds(interface,
240                        handle->bulkOut);
241                if (kr != 0) {
242                    ERR("could not clear output pipe; result %x, ignoring....\n", kr);
243                }
244            }
245
246            return 0;
247        }
248
249next_interface:
250        (*interface)->USBInterfaceClose(interface);
251        (*interface)->Release(interface);
252    }
253
254    return 0;
255}
256
257/** Try out the given device and see if there's a match. Returns 0 on
258 * success, -1 on failure.
259 */
260static int try_device(io_service_t device, usb_handle *handle) {
261    kern_return_t kr;
262    IOCFPlugInInterface **plugin = NULL;
263    IOUSBDeviceInterface182 **dev = NULL;
264    SInt32 score;
265    HRESULT result;
266    UInt8 serialIndex;
267
268    // Create an intermediate plugin.
269    kr = IOCreatePlugInInterfaceForService(device,
270            kIOUSBDeviceUserClientTypeID,
271            kIOCFPlugInInterfaceID,
272            &plugin, &score);
273
274    if ((kr != 0) || (plugin == NULL)) {
275        ERR("Unable to create a plug-in (%08x)\n", kr);
276        goto error;
277    }
278
279    // Now create the device interface.
280    result = (*plugin)->QueryInterface(plugin,
281            CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID) &dev);
282    if ((result != 0) || (dev == NULL)) {
283        ERR("Couldn't create a device interface (%08x)\n", (int) result);
284        goto error;
285    }
286
287    /*
288     * We don't need the intermediate interface after the device interface
289     * is created.
290     */
291    IODestroyPlugInInterface(plugin);
292
293    // So, we have a device, finally. Grab its vitals.
294
295    kr = (*dev)->GetDeviceVendor(dev, &handle->info.dev_vendor);
296    if (kr != 0) {
297        ERR("GetDeviceVendor");
298        goto error;
299    }
300
301    kr = (*dev)->GetDeviceProduct(dev, &handle->info.dev_product);
302    if (kr != 0) {
303        ERR("GetDeviceProduct");
304        goto error;
305    }
306
307    kr = (*dev)->GetDeviceClass(dev, &handle->info.dev_class);
308    if (kr != 0) {
309        ERR("GetDeviceClass");
310        goto error;
311    }
312
313    kr = (*dev)->GetDeviceSubClass(dev, &handle->info.dev_subclass);
314    if (kr != 0) {
315        ERR("GetDeviceSubClass");
316        goto error;
317    }
318
319    kr = (*dev)->GetDeviceProtocol(dev, &handle->info.dev_protocol);
320    if (kr != 0) {
321        ERR("GetDeviceProtocol");
322        goto error;
323    }
324
325    kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
326
327    if (serialIndex > 0) {
328        IOUSBDevRequest req;
329        UInt16  buffer[256];
330
331        req.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
332        req.bRequest = kUSBRqGetDescriptor;
333        req.wValue = (kUSBStringDesc << 8) | serialIndex;
334        req.wIndex = 0;
335        req.pData = buffer;
336        req.wLength = sizeof(buffer);
337        kr = (*dev)->DeviceRequest(dev, &req);
338
339        if (kr == kIOReturnSuccess && req.wLenDone > 0) {
340            int i, count;
341
342            // skip first word, and copy the rest to the serial string, changing shorts to bytes.
343            count = (req.wLenDone - 1) / 2;
344            for (i = 0; i < count; i++)
345              handle->info.serial_number[i] = buffer[i + 1];
346            handle->info.serial_number[i] = 0;
347        }
348    } else {
349        // device has no serial number
350        handle->info.serial_number[0] = 0;
351    }
352    handle->info.writable = 1;
353
354    if (try_interfaces(dev, handle)) {
355        goto error;
356    }
357
358    (*dev)->Release(dev);
359    return 0;
360
361    error:
362
363    if (dev != NULL) {
364        (*dev)->Release(dev);
365    }
366
367    return -1;
368}
369
370
371/** Initializes the USB system. Returns 0 on success, -1 on error. */
372static int init_usb(ifc_match_func callback, usb_handle **handle) {
373    int ret = -1;
374    CFMutableDictionaryRef matchingDict;
375    kern_return_t result;
376    io_iterator_t iterator;
377    usb_handle h;
378
379    h.success = 0;
380    h.callback = callback;
381
382    /*
383     * Create our matching dictionary to find appropriate devices.
384     * IOServiceAddMatchingNotification consumes the reference, so we
385     * do not need to release it.
386     */
387    matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
388
389    if (matchingDict == NULL) {
390        ERR("Couldn't create USB matching dictionary.\n");
391        return -1;
392    }
393
394    result = IOServiceGetMatchingServices(
395            kIOMasterPortDefault, matchingDict, &iterator);
396
397    if (result != 0) {
398        ERR("Could not create iterator.");
399        return -1;
400    }
401
402    for (;;) {
403        if (! IOIteratorIsValid(iterator)) {
404            /*
405             * Apple documentation advises resetting the iterator if
406             * it should become invalid during iteration.
407             */
408            IOIteratorReset(iterator);
409            continue;
410        }
411
412        io_service_t device = IOIteratorNext(iterator);
413
414        if (device == 0) {
415            break;
416        }
417
418        if (try_device(device, &h) != 0) {
419            IOObjectRelease(device);
420            ret = -1;
421            break;
422        }
423
424        if (h.success) {
425            *handle = calloc(1, sizeof(usb_handle));
426            memcpy(*handle, &h, sizeof(usb_handle));
427            ret = 0;
428            break;
429        }
430
431        IOObjectRelease(device);
432    }
433
434    IOObjectRelease(iterator);
435
436    return ret;
437}
438
439
440
441/*
442 * Definitions of this file's public functions.
443 */
444
445usb_handle *usb_open(ifc_match_func callback) {
446    usb_handle *handle = NULL;
447
448    if (init_usb(callback, &handle) < 0) {
449        /* Something went wrong initializing USB. */
450        return NULL;
451    }
452
453    return handle;
454}
455
456int usb_close(usb_handle *h) {
457    /* TODO: Something better here? */
458    return 0;
459}
460
461int usb_read(usb_handle *h, void *data, int len) {
462    IOReturn result;
463    UInt32 numBytes = len;
464
465    if (len == 0) {
466        return 0;
467    }
468
469    if (h == NULL) {
470        return -1;
471    }
472
473    if (h->interface == NULL) {
474        ERR("usb_read interface was null\n");
475        return -1;
476    }
477
478    if (h->bulkIn == 0) {
479        ERR("bulkIn endpoint not assigned\n");
480        return -1;
481    }
482
483    result = (*h->interface)->ReadPipe(
484            h->interface, h->bulkIn, data, &numBytes);
485
486    if (result == 0) {
487        return (int) numBytes;
488    } else {
489        ERR("usb_read failed with status %x\n", result);
490    }
491
492    return -1;
493}
494
495int usb_write(usb_handle *h, const void *data, int len) {
496    IOReturn result;
497
498    if (len == 0) {
499        return 0;
500    }
501
502    if (h == NULL) {
503        return -1;
504    }
505
506    if (h->interface == NULL) {
507        ERR("usb_write interface was null\n");
508        return -1;
509    }
510
511    if (h->bulkOut == 0) {
512        ERR("bulkOut endpoint not assigned\n");
513        return -1;
514    }
515
516#if 0
517    result = (*h->interface)->WritePipe(
518            h->interface, h->bulkOut, (void *)data, len);
519#else
520    /* Attempt to work around crashes in the USB driver that may be caused
521     * by trying to write too much data at once.  The kernel IOCopyMapper
522     * panics if a single iovmAlloc needs more than half of its mapper pages.
523     */
524    const int maxLenToSend = 1048576; // 1 MiB
525    int lenRemaining = len;
526    result = 0;
527    while (lenRemaining > 0) {
528        int lenToSend = lenRemaining > maxLenToSend
529            ? maxLenToSend : lenRemaining;
530
531        result = (*h->interface)->WritePipe(
532                h->interface, h->bulkOut, (void *)data, lenToSend);
533        if (result != 0) break;
534
535        lenRemaining -= lenToSend;
536        data = (const char*)data + lenToSend;
537    }
538#endif
539
540    #if 0
541    if ((result == 0) && (h->zero_mask)) {
542        /* we need 0-markers and our transfer */
543        if(!(len & h->zero_mask)) {
544            result = (*h->interface)->WritePipe(
545                    h->interface, h->bulkOut, (void *)data, 0);
546        }
547    }
548    #endif
549
550    if (result != 0) {
551        ERR("usb_write failed with status %x\n", result);
552        return -1;
553    }
554
555    return len;
556}
557