2 * darwin backend for libusbx 1.0
3 * Copyright © 2008-2011 Nathan Hjelm <hjelmn@users.sourceforge.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/ioctl.h>
31 #include <sys/types.h>
33 #include <libkern/OSAtomic.h>
35 #include <mach/clock.h>
36 #include <mach/clock_types.h>
37 #include <mach/mach_host.h>
38 #include <mach/mach_port.h>
40 #include <AvailabilityMacros.h>
41 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
42 #include <objc/objc-auto.h>
45 #include <IOKit/IOCFBundle.h>
46 #include <IOKit/usb/IOUSBLib.h>
47 #include <IOKit/IOCFPlugIn.h>
49 #include "darwin_usb.h"
51 /* async event thread */
52 static pthread_mutex_t libusb_darwin_at_mutex
;
53 static pthread_cond_t libusb_darwin_at_cond
;
55 static clock_serv_t clock_realtime
;
56 static clock_serv_t clock_monotonic
;
58 static CFRunLoopRef libusb_darwin_acfl
= NULL
; /* async cf loop */
59 static volatile int32_t initCount
= 0;
61 /* async event thread */
62 static pthread_t libusb_darwin_at
;
64 static int darwin_get_config_descriptor(struct libusb_device
*dev
, uint8_t config_index
, unsigned char *buffer
, size_t len
, int *host_endian
);
65 static int darwin_claim_interface(struct libusb_device_handle
*dev_handle
, int iface
);
66 static int darwin_release_interface(struct libusb_device_handle
*dev_handle
, int iface
);
67 static int darwin_reset_device(struct libusb_device_handle
*dev_handle
);
68 static void darwin_async_io_callback (void *refcon
, IOReturn result
, void *arg0
);
70 static const char *darwin_error_str (int result
) {
72 case kIOReturnSuccess
:
74 case kIOReturnNotOpen
:
75 return "device not opened for exclusive access";
76 case kIOReturnNoDevice
:
77 return "no connection to an IOService";
78 case kIOUSBNoAsyncPortErr
:
79 return "no async port has been opened for interface";
80 case kIOReturnExclusiveAccess
:
81 return "another process has device opened for exclusive access";
82 case kIOUSBPipeStalled
:
83 return "pipe is stalled";
85 return "could not establish a connection to the Darwin kernel";
86 case kIOUSBTransactionTimeout
:
87 return "transaction timed out";
88 case kIOReturnBadArgument
:
89 return "invalid argument";
90 case kIOReturnAborted
:
91 return "transaction aborted";
92 case kIOReturnNotResponding
:
93 return "device not responding";
94 case kIOReturnOverrun
:
95 return "data overrun";
96 case kIOReturnCannotWire
:
97 return "physical memory can not be wired down";
99 return "unknown error";
103 static int darwin_to_libusb (int result
) {
105 case kIOReturnUnderrun
:
106 case kIOReturnSuccess
:
107 return LIBUSB_SUCCESS
;
108 case kIOReturnNotOpen
:
109 case kIOReturnNoDevice
:
110 return LIBUSB_ERROR_NO_DEVICE
;
111 case kIOReturnExclusiveAccess
:
112 return LIBUSB_ERROR_ACCESS
;
113 case kIOUSBPipeStalled
:
114 return LIBUSB_ERROR_PIPE
;
115 case kIOReturnBadArgument
:
116 return LIBUSB_ERROR_INVALID_PARAM
;
117 case kIOUSBTransactionTimeout
:
118 return LIBUSB_ERROR_TIMEOUT
;
119 case kIOReturnNotResponding
:
120 case kIOReturnAborted
:
122 case kIOUSBNoAsyncPortErr
:
124 return LIBUSB_ERROR_OTHER
;
129 static int ep_to_pipeRef(struct libusb_device_handle
*dev_handle
, uint8_t ep
, uint8_t *pipep
, uint8_t *ifcp
) {
130 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)dev_handle
->os_priv
;
132 /* current interface */
133 struct darwin_interface
*cInterface
;
137 usbi_info (HANDLE_CTX(dev_handle
), "converting ep address 0x%02x to pipeRef and interface", ep
);
139 for (iface
= 0 ; iface
< USB_MAXINTERFACES
; iface
++) {
140 cInterface
= &priv
->interfaces
[iface
];
142 if (dev_handle
->claimed_interfaces
& (1 << iface
)) {
143 for (i
= 0 ; i
< cInterface
->num_endpoints
; i
++) {
144 if (cInterface
->endpoint_addrs
[i
] == ep
) {
147 usbi_info (HANDLE_CTX(dev_handle
), "pipe %d on interface %d matches", *pipep
, *ifcp
);
154 /* No pipe found with the correct endpoint address */
155 usbi_warn (HANDLE_CTX(dev_handle
), "no pipeRef found with endpoint address 0x%02x.", ep
);
160 static int usb_setup_device_iterator (io_iterator_t
*deviceIterator
, long location
) {
161 CFMutableDictionaryRef matchingDict
= IOServiceMatching(kIOUSBDeviceClassName
);
164 return kIOReturnError
;
167 CFMutableDictionaryRef propertyMatchDict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
168 &kCFTypeDictionaryKeyCallBacks
,
169 &kCFTypeDictionaryValueCallBacks
);
171 if (propertyMatchDict
) {
172 CFTypeRef locationCF
= CFNumberCreate (NULL
, kCFNumberLongType
, &location
);
174 CFDictionarySetValue (propertyMatchDict
, CFSTR(kUSBDevicePropertyLocationID
), locationCF
);
175 /* release our reference to the CFNumber (CFDictionarySetValue retains it) */
176 CFRelease (locationCF
);
178 CFDictionarySetValue (matchingDict
, CFSTR(kIOPropertyMatchKey
), propertyMatchDict
);
179 /* release out reference to the CFMutableDictionaryRef (CFDictionarySetValue retains it) */
180 CFRelease (propertyMatchDict
);
182 /* else we can still proceed as long as the caller accounts for the possibility of other devices in the iterator */
185 return IOServiceGetMatchingServices(kIOMasterPortDefault
, matchingDict
, deviceIterator
);
188 static int get_ioregistry_value_number (io_service_t service
, CFStringRef property
, CFNumberType type
, void *p
) {
189 CFTypeRef cfNumber
= IORegistryEntryCreateCFProperty (service
, property
, kCFAllocatorDefault
, 0);
193 if (CFGetTypeID(cfNumber
) == CFNumberGetTypeID()) {
194 ret
= CFNumberGetValue(cfNumber
, type
, p
);
197 CFRelease (cfNumber
);
203 static usb_device_t
**usb_get_next_device (io_iterator_t deviceIterator
, UInt32
*locationp
, UInt8
*portp
, UInt32
*parent_locationp
) {
204 io_cf_plugin_ref_t
*plugInInterface
= NULL
;
205 usb_device_t
**device
;
206 io_service_t usbDevice
, parent
;
207 kern_return_t result
;
210 if (!IOIteratorIsValid (deviceIterator
))
214 while ((usbDevice
= IOIteratorNext(deviceIterator
))) {
215 result
= IOCreatePlugInInterfaceForService(usbDevice
, kIOUSBDeviceUserClientTypeID
,
216 kIOCFPlugInInterfaceID
, &plugInInterface
,
219 /* we are done with the usb_device_t */
220 (void)IOObjectRelease(usbDevice
);
224 (void) get_ioregistry_value_number (usbDevice
, CFSTR("PortNum"), kCFNumberSInt8Type
, portp
);
227 if (parent_locationp
) {
228 *parent_locationp
= 0;
230 result
= IORegistryEntryGetParentEntry (usbDevice
, kIOUSBPlane
, &parent
);
232 if (kIOReturnSuccess
== result
) {
233 (void) get_ioregistry_value_number (parent
, CFSTR("locationID"), kCFNumberLongType
, parent_locationp
);
237 if (kIOReturnSuccess
== result
&& plugInInterface
)
240 usbi_dbg ("libusb/darwin.c usb_get_next_device: could not set up plugin for service: %s\n", darwin_error_str (result
));
246 (void)(*plugInInterface
)->QueryInterface(plugInInterface
, CFUUIDGetUUIDBytes(DeviceInterfaceID
),
249 (*plugInInterface
)->Stop(plugInInterface
);
250 IODestroyPlugInInterface (plugInInterface
);
252 /* get the location from the device */
254 (*(device
))->GetLocationID(device
, locationp
);
259 static kern_return_t
darwin_get_device (uint32_t dev_location
, usb_device_t
***darwin_device
) {
260 kern_return_t kresult
;
262 io_iterator_t deviceIterator
;
264 kresult
= usb_setup_device_iterator (&deviceIterator
, dev_location
);
268 /* This port of libusb uses locations to keep track of devices. */
269 while ((*darwin_device
= usb_get_next_device (deviceIterator
, &location
, NULL
, NULL
)) != NULL
) {
270 if (location
== dev_location
)
273 (**darwin_device
)->Release(*darwin_device
);
276 IOObjectRelease (deviceIterator
);
278 if (!(*darwin_device
))
279 return kIOReturnNoDevice
;
281 return kIOReturnSuccess
;
286 static void darwin_devices_detached (void *ptr
, io_iterator_t rem_devices
) {
287 struct libusb_context
*ctx
= (struct libusb_context
*)ptr
;
288 struct libusb_device_handle
*handle
;
289 struct darwin_device_priv
*dpriv
;
290 struct darwin_device_handle_priv
*priv
;
295 CFTypeRef locationCF
;
298 usbi_info (ctx
, "a device has been detached");
300 while ((device
= IOIteratorNext (rem_devices
)) != 0) {
301 /* get the location from the i/o registry */
302 locationCF
= IORegistryEntryCreateCFProperty (device
, CFSTR(kUSBDevicePropertyLocationID
), kCFAllocatorDefault
, 0);
304 IOObjectRelease (device
);
309 locationValid
= CFGetTypeID(locationCF
) == CFNumberGetTypeID() &&
310 CFNumberGetValue(locationCF
, kCFNumberLongType
, &location
);
312 CFRelease (locationCF
);
317 usbi_mutex_lock(&ctx
->open_devs_lock
);
318 list_for_each_entry(handle
, &ctx
->open_devs
, list
, struct libusb_device_handle
) {
319 dpriv
= (struct darwin_device_priv
*)handle
->dev
->os_priv
;
321 /* the device may have been opened several times. write to each handle's event descriptor */
322 if (dpriv
->location
== location
&& handle
->os_priv
) {
323 priv
= (struct darwin_device_handle_priv
*)handle
->os_priv
;
325 message
= MESSAGE_DEVICE_GONE
;
326 write (priv
->fds
[1], &message
, sizeof (message
));
330 usbi_mutex_unlock(&ctx
->open_devs_lock
);
334 static void darwin_clear_iterator (io_iterator_t iter
) {
337 while ((device
= IOIteratorNext (iter
)) != 0)
338 IOObjectRelease (device
);
341 static void *event_thread_main (void *arg0
) {
343 struct libusb_context
*ctx
= (struct libusb_context
*)arg0
;
344 CFRunLoopRef runloop
;
346 /* Set this thread's name, so it can be seen in the debugger
347 and crash reports. */
348 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
349 pthread_setname_np ("org.libusb.device-detach");
352 /* Tell the Objective-C garbage collector about this thread.
353 This is required because, unlike NSThreads, pthreads are
354 not automatically registered. Although we don't use
355 Objective-C, we use CoreFoundation, which does. */
356 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
357 objc_registerThreadWithCollector();
360 /* hotplug (device removal) source */
361 CFRunLoopSourceRef libusb_notification_cfsource
;
362 io_notification_port_t libusb_notification_port
;
363 io_iterator_t libusb_rem_device_iterator
;
365 usbi_info (ctx
, "creating hotplug event source");
367 runloop
= CFRunLoopGetCurrent ();
370 /* add the notification port to the run loop */
371 libusb_notification_port
= IONotificationPortCreate (kIOMasterPortDefault
);
372 libusb_notification_cfsource
= IONotificationPortGetRunLoopSource (libusb_notification_port
);
373 CFRunLoopAddSource(runloop
, libusb_notification_cfsource
, kCFRunLoopDefaultMode
);
375 /* create notifications for removed devices */
376 kresult
= IOServiceAddMatchingNotification (libusb_notification_port
, kIOTerminatedNotification
,
377 IOServiceMatching(kIOUSBDeviceClassName
),
378 (IOServiceMatchingCallback
)darwin_devices_detached
,
379 (void *)ctx
, &libusb_rem_device_iterator
);
381 if (kresult
!= kIOReturnSuccess
) {
382 usbi_err (ctx
, "could not add hotplug event source: %s", darwin_error_str (kresult
));
388 darwin_clear_iterator (libusb_rem_device_iterator
);
390 usbi_info (ctx
, "thread ready to receive events");
392 /* signal the main thread that the async runloop has been created. */
393 pthread_mutex_lock (&libusb_darwin_at_mutex
);
394 libusb_darwin_acfl
= runloop
;
395 pthread_cond_signal (&libusb_darwin_at_cond
);
396 pthread_mutex_unlock (&libusb_darwin_at_mutex
);
398 /* run the runloop */
401 usbi_info (ctx
, "thread exiting");
403 /* delete notification port */
404 IONotificationPortDestroy (libusb_notification_port
);
405 IOObjectRelease (libusb_rem_device_iterator
);
409 libusb_darwin_acfl
= NULL
;
414 static int darwin_init(struct libusb_context
*ctx
) {
415 host_name_port_t host_self
;
417 if (OSAtomicIncrement32Barrier(&initCount
) == 1) {
418 /* create the clocks that will be used */
420 host_self
= mach_host_self();
421 host_get_clock_service(host_self
, CALENDAR_CLOCK
, &clock_realtime
);
422 host_get_clock_service(host_self
, SYSTEM_CLOCK
, &clock_monotonic
);
423 mach_port_deallocate(mach_task_self(), host_self
);
425 pthread_mutex_init (&libusb_darwin_at_mutex
, NULL
);
426 pthread_cond_init (&libusb_darwin_at_cond
, NULL
);
428 pthread_create (&libusb_darwin_at
, NULL
, event_thread_main
, (void *)ctx
);
430 pthread_mutex_lock (&libusb_darwin_at_mutex
);
431 while (!libusb_darwin_acfl
)
432 pthread_cond_wait (&libusb_darwin_at_cond
, &libusb_darwin_at_mutex
);
433 pthread_mutex_unlock (&libusb_darwin_at_mutex
);
439 static void darwin_exit (void) {
440 if (OSAtomicDecrement32Barrier(&initCount
) == 0) {
441 mach_port_deallocate(mach_task_self(), clock_realtime
);
442 mach_port_deallocate(mach_task_self(), clock_monotonic
);
444 /* stop the async runloop and wait for the thread to terminate. */
445 CFRunLoopStop (libusb_darwin_acfl
);
446 pthread_join (libusb_darwin_at
, NULL
);
450 static int darwin_get_device_descriptor(struct libusb_device
*dev
, unsigned char *buffer
, int *host_endian
) {
451 struct darwin_device_priv
*priv
= (struct darwin_device_priv
*)dev
->os_priv
;
453 /* return cached copy */
454 memmove (buffer
, &(priv
->dev_descriptor
), DEVICE_DESC_LENGTH
);
461 static int get_configuration_index (struct libusb_device
*dev
, int config_value
) {
462 struct darwin_device_priv
*priv
= (struct darwin_device_priv
*)dev
->os_priv
;
464 IOUSBConfigurationDescriptorPtr desc
;
467 /* is there a simpler way to determine the index? */
468 kresult
= (*(priv
->device
))->GetNumberOfConfigurations (priv
->device
, &numConfig
);
469 if (kresult
!= kIOReturnSuccess
)
470 return darwin_to_libusb (kresult
);
472 for (i
= 0 ; i
< numConfig
; i
++) {
473 (*(priv
->device
))->GetConfigurationDescriptorPtr (priv
->device
, i
, &desc
);
475 if (desc
->bConfigurationValue
== config_value
)
479 /* configuration not found */
480 return LIBUSB_ERROR_OTHER
;
483 static int darwin_get_active_config_descriptor(struct libusb_device
*dev
, unsigned char *buffer
, size_t len
, int *host_endian
) {
484 struct darwin_device_priv
*priv
= (struct darwin_device_priv
*)dev
->os_priv
;
487 if (0 == priv
->active_config
)
488 return LIBUSB_ERROR_INVALID_PARAM
;
490 config_index
= get_configuration_index (dev
, priv
->active_config
);
491 if (config_index
< 0)
494 return darwin_get_config_descriptor (dev
, config_index
, buffer
, len
, host_endian
);
497 static int darwin_get_config_descriptor(struct libusb_device
*dev
, uint8_t config_index
, unsigned char *buffer
, size_t len
, int *host_endian
) {
498 struct darwin_device_priv
*priv
= (struct darwin_device_priv
*)dev
->os_priv
;
499 IOUSBConfigurationDescriptorPtr desc
;
501 usb_device_t
**device
= NULL
;
504 return LIBUSB_ERROR_OTHER
;
507 kresult
= darwin_get_device (priv
->location
, &device
);
508 if (kresult
|| !device
) {
509 usbi_err (DEVICE_CTX (dev
), "could not find device: %s", darwin_error_str (kresult
));
511 return darwin_to_libusb (kresult
);
514 /* don't have to open the device to get a config descriptor */
516 device
= priv
->device
;
518 kresult
= (*device
)->GetConfigurationDescriptorPtr (device
, config_index
, &desc
);
519 if (kresult
== kIOReturnSuccess
) {
520 /* copy descriptor */
521 if (libusb_le16_to_cpu(desc
->wTotalLength
) < len
)
522 len
= libusb_le16_to_cpu(desc
->wTotalLength
);
524 memmove (buffer
, desc
, len
);
526 /* GetConfigurationDescriptorPtr returns the descriptor in USB bus order */
531 (*device
)->Release (device
);
533 return darwin_to_libusb (kresult
);
536 /* check whether the os has configured the device */
537 static int darwin_check_configuration (struct libusb_context
*ctx
, struct libusb_device
*dev
, usb_device_t
**darwin_device
) {
538 struct darwin_device_priv
*priv
= (struct darwin_device_priv
*)dev
->os_priv
;
540 IOUSBConfigurationDescriptorPtr configDesc
;
541 IOUSBFindInterfaceRequest request
;
542 kern_return_t kresult
;
543 io_iterator_t interface_iterator
;
544 io_service_t firstInterface
;
546 if (priv
->dev_descriptor
.bNumConfigurations
< 1) {
547 usbi_err (ctx
, "device has no configurations");
548 return LIBUSB_ERROR_OTHER
; /* no configurations at this speed so we can't use it */
551 /* find the first configuration */
552 kresult
= (*darwin_device
)->GetConfigurationDescriptorPtr (darwin_device
, 0, &configDesc
);
553 priv
->first_config
= (kIOReturnSuccess
== kresult
) ? configDesc
->bConfigurationValue
: 1;
555 /* check if the device is already configured. there is probably a better way than iterating over the
556 to accomplish this (the trick is we need to avoid a call to GetConfigurations since buggy devices
557 might lock up on the device request) */
559 /* Setup the Interface Request */
560 request
.bInterfaceClass
= kIOUSBFindInterfaceDontCare
;
561 request
.bInterfaceSubClass
= kIOUSBFindInterfaceDontCare
;
562 request
.bInterfaceProtocol
= kIOUSBFindInterfaceDontCare
;
563 request
.bAlternateSetting
= kIOUSBFindInterfaceDontCare
;
565 kresult
= (*(darwin_device
))->CreateInterfaceIterator(darwin_device
, &request
, &interface_iterator
);
567 return darwin_to_libusb (kresult
);
570 firstInterface
= IOIteratorNext(interface_iterator
);
572 /* done with the interface iterator */
573 IOObjectRelease(interface_iterator
);
575 if (firstInterface
) {
576 IOObjectRelease (firstInterface
);
578 /* device is configured */
579 if (priv
->dev_descriptor
.bNumConfigurations
== 1)
580 /* to avoid problems with some devices get the configurations value from the configuration descriptor */
581 priv
->active_config
= priv
->first_config
;
583 /* devices with more than one configuration should work with GetConfiguration */
584 (*darwin_device
)->GetConfiguration (darwin_device
, &priv
->active_config
);
587 priv
->active_config
= 0;
589 usbi_info (ctx
, "active config: %u, first config: %u", priv
->active_config
, priv
->first_config
);
594 static int darwin_cache_device_descriptor (struct libusb_context
*ctx
, struct libusb_device
*dev
, usb_device_t
**device
) {
595 struct darwin_device_priv
*priv
;
596 int retries
= 5, delay
= 30000;
597 int unsuspended
= 0, try_unsuspend
= 1, try_reconfigure
= 1;
602 UInt16 idProduct
, idVendor
;
604 (*device
)->GetDeviceClass (device
, &bDeviceClass
);
605 (*device
)->GetDeviceProduct (device
, &idProduct
);
606 (*device
)->GetDeviceVendor (device
, &idVendor
);
608 priv
= (struct darwin_device_priv
*)dev
->os_priv
;
610 /* try to open the device (we can usually continue even if this fails) */
611 is_open
= ((*device
)->USBDeviceOpenSeize(device
) == kIOReturnSuccess
);
613 /**** retrieve device descriptor ****/
615 /* Set up request for device descriptor */
616 memset (&(priv
->dev_descriptor
), 0, sizeof(IOUSBDeviceDescriptor
));
617 req
.bmRequestType
= USBmakebmRequestType(kUSBIn
, kUSBStandard
, kUSBDevice
);
618 req
.bRequest
= kUSBRqGetDescriptor
;
619 req
.wValue
= kUSBDeviceDesc
<< 8;
621 req
.wLength
= sizeof(priv
->dev_descriptor
);
622 req
.pData
= &(priv
->dev_descriptor
);
624 /* according to Apple's documentation the device must be open for DeviceRequest but we may not be able to open some
625 * devices and Apple's USB Prober doesn't bother to open the device before issuing a descriptor request. Still,
626 * to follow the spec as closely as possible, try opening the device */
628 ret
= (*(device
))->DeviceRequest (device
, &req
);
630 if (kIOReturnOverrun
== ret
&& kUSBDeviceDesc
== priv
->dev_descriptor
.bDescriptorType
)
631 /* received an overrun error but we still received a device descriptor */
632 ret
= kIOReturnSuccess
;
634 if (kIOReturnSuccess
== ret
&& (0 == priv
->dev_descriptor
.bNumConfigurations
||
635 0 == priv
->dev_descriptor
.bcdUSB
)) {
636 /* work around for incorrectly configured devices */
637 if (try_reconfigure
&& is_open
) {
638 usbi_dbg("descriptor appears to be invalid. resetting configuration before trying again...");
640 /* set the first configuration */
641 (*device
)->SetConfiguration(device
, 1);
643 /* don't try to reconfigure again */
647 ret
= kIOUSBPipeStalled
;
650 if (kIOReturnSuccess
!= ret
&& is_open
&& try_unsuspend
) {
651 /* device may be suspended. unsuspend it and try again */
652 #if DeviceVersion >= 320
655 /* IOUSBFamily 320+ provides a way to detect device suspension but earlier versions do not */
656 (void)(*device
)->GetUSBDeviceInformation (device
, &info
);
658 try_unsuspend
= info
& (1 << kUSBInformationDeviceIsSuspendedBit
);
662 /* resume the device */
663 ret2
= (*device
)->USBDeviceSuspend (device
, 0);
664 if (kIOReturnSuccess
!= ret2
) {
665 /* prevent log spew from poorly behaving devices. this indicates the
666 os actually had trouble communicating with the device */
667 usbi_dbg("could not retrieve device descriptor. failed to unsuspend: %s",darwin_error_str(ret2
));
675 if (kIOReturnSuccess
!= ret
) {
676 usbi_dbg("kernel responded with code: 0x%08x. sleeping for %d ms before trying again", ret
, delay
/1000);
677 /* sleep for a little while before trying again */
680 } while (kIOReturnSuccess
!= ret
&& retries
--);
683 /* resuspend the device */
684 (void)(*device
)->USBDeviceSuspend (device
, 1);
687 (void) (*device
)->USBDeviceClose (device
);
689 if (ret
!= kIOReturnSuccess
) {
690 /* a debug message was already printed out for this error */
691 if (LIBUSB_CLASS_HUB
== bDeviceClass
)
692 usbi_dbg ("could not retrieve device descriptor %.4x:%.4x: %s. skipping device", idVendor
, idProduct
, darwin_error_str (ret
));
694 usbi_warn (ctx
, "could not retrieve device descriptor %.4x:%.4x: %s. skipping device", idVendor
, idProduct
, darwin_error_str (ret
));
699 usbi_dbg ("device descriptor:");
700 usbi_dbg (" bDescriptorType: 0x%02x", priv
->dev_descriptor
.bDescriptorType
);
701 usbi_dbg (" bcdUSB: 0x%04x", priv
->dev_descriptor
.bcdUSB
);
702 usbi_dbg (" bDeviceClass: 0x%02x", priv
->dev_descriptor
.bDeviceClass
);
703 usbi_dbg (" bDeviceSubClass: 0x%02x", priv
->dev_descriptor
.bDeviceSubClass
);
704 usbi_dbg (" bDeviceProtocol: 0x%02x", priv
->dev_descriptor
.bDeviceProtocol
);
705 usbi_dbg (" bMaxPacketSize0: 0x%02x", priv
->dev_descriptor
.bMaxPacketSize0
);
706 usbi_dbg (" idVendor: 0x%04x", priv
->dev_descriptor
.idVendor
);
707 usbi_dbg (" idProduct: 0x%04x", priv
->dev_descriptor
.idProduct
);
708 usbi_dbg (" bcdDevice: 0x%04x", priv
->dev_descriptor
.bcdDevice
);
709 usbi_dbg (" iManufacturer: 0x%02x", priv
->dev_descriptor
.iManufacturer
);
710 usbi_dbg (" iProduct: 0x%02x", priv
->dev_descriptor
.iProduct
);
711 usbi_dbg (" iSerialNumber: 0x%02x", priv
->dev_descriptor
.iSerialNumber
);
712 usbi_dbg (" bNumConfigurations: 0x%02x", priv
->dev_descriptor
.bNumConfigurations
);
714 /* catch buggy hubs (which appear to be virtual). Apple's own USB prober has problems with these devices. */
715 if (libusb_le16_to_cpu (priv
->dev_descriptor
.idProduct
) != idProduct
) {
716 /* not a valid device */
717 usbi_warn (ctx
, "idProduct from iokit (%04x) does not match idProduct in descriptor (%04x). skipping device",
718 idProduct
, libusb_le16_to_cpu (priv
->dev_descriptor
.idProduct
));
725 static int process_new_device (struct libusb_context
*ctx
, usb_device_t
**device
, UInt32 locationID
,
726 UInt32 parent_location
, UInt8 port
, struct discovered_devs
**_discdevs
,
727 struct libusb_device
**last_dev
) {
728 struct darwin_device_priv
*priv
;
729 struct libusb_device
*dev
, *parent
= NULL
;
730 struct discovered_devs
*discdevs
;
733 int ret
= 0, need_unref
= 0;
736 dev
= usbi_get_device_by_session_id(ctx
, locationID
);
738 usbi_info (ctx
, "allocating new device for location 0x%08x", locationID
);
739 dev
= usbi_alloc_device(ctx
, locationID
);
742 usbi_info (ctx
, "using existing device for location 0x%08x", locationID
);
745 ret
= LIBUSB_ERROR_NO_MEM
;
749 priv
= (struct darwin_device_priv
*)dev
->os_priv
;
751 (*device
)->GetDeviceAddress (device
, (USBDeviceAddress
*)&address
);
753 ret
= darwin_cache_device_descriptor (ctx
, dev
, device
);
757 /* check current active configuration (and cache the first configuration value-- which may be used by claim_interface) */
758 ret
= darwin_check_configuration (ctx
, dev
, device
);
762 /* the device iterator provides devices in increasing order of location. given this property
763 * we can use the last device to find the parent. */
764 for (parent
= *last_dev
; parent
; parent
= parent
->parent_dev
) {
765 struct darwin_device_priv
*parent_priv
= (struct darwin_device_priv
*) parent
->os_priv
;
767 if (parent_priv
->location
== parent_location
) {
772 dev
->parent_dev
= parent
;
774 dev
->port_number
= port
;
775 dev
->bus_number
= locationID
>> 24;
776 dev
->device_address
= address
;
778 (*device
)->GetDeviceSpeed (device
, &devSpeed
);
781 case kUSBDeviceSpeedLow
: dev
->speed
= LIBUSB_SPEED_LOW
; break;
782 case kUSBDeviceSpeedFull
: dev
->speed
= LIBUSB_SPEED_FULL
; break;
783 case kUSBDeviceSpeedHigh
: dev
->speed
= LIBUSB_SPEED_HIGH
; break;
785 usbi_warn (ctx
, "Got unknown device speed %d", devSpeed
);
788 /* save our location, we'll need this later */
789 priv
->location
= locationID
;
790 snprintf(priv
->sys_path
, 20, "%03i-%04x-%04x-%02x-%02x", address
, priv
->dev_descriptor
.idVendor
, priv
->dev_descriptor
.idProduct
,
791 priv
->dev_descriptor
.bDeviceClass
, priv
->dev_descriptor
.bDeviceSubClass
);
793 ret
= usbi_sanitize_device (dev
);
797 /* append the device to the list of discovered devices */
798 discdevs
= discovered_devs_append(*_discdevs
, dev
);
800 ret
= LIBUSB_ERROR_NO_MEM
;
804 *_discdevs
= discdevs
;
807 usbi_info (ctx
, "found device with address %d port = %d parent = %p at %p", dev
->device_address
,
808 dev
->port_number
, priv
->sys_path
, (void *) parent
);
812 libusb_unref_device(dev
);
817 static int darwin_get_device_list(struct libusb_context
*ctx
, struct discovered_devs
**_discdevs
) {
818 io_iterator_t deviceIterator
;
819 usb_device_t
**device
;
820 kern_return_t kresult
;
821 UInt32 location
, parent_location
;
823 struct libusb_device
*last_dev
= NULL
;
825 kresult
= usb_setup_device_iterator (&deviceIterator
, 0);
826 if (kresult
!= kIOReturnSuccess
)
827 return darwin_to_libusb (kresult
);
829 while ((device
= usb_get_next_device (deviceIterator
, &location
, &port
, &parent_location
)) != NULL
) {
830 (void) process_new_device (ctx
, device
, location
, parent_location
, port
, _discdevs
, &last_dev
);
832 (*(device
))->Release(device
);
835 IOObjectRelease(deviceIterator
);
840 static int darwin_open (struct libusb_device_handle
*dev_handle
) {
841 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)dev_handle
->os_priv
;
842 struct darwin_device_priv
*dpriv
= (struct darwin_device_priv
*)dev_handle
->dev
->os_priv
;
843 usb_device_t
**darwin_device
;
846 if (0 == dpriv
->open_count
) {
847 kresult
= darwin_get_device (dpriv
->location
, &darwin_device
);
849 usbi_err (HANDLE_CTX (dev_handle
), "could not find device: %s", darwin_error_str (kresult
));
850 return darwin_to_libusb (kresult
);
853 dpriv
->device
= darwin_device
;
855 /* try to open the device */
856 kresult
= (*(dpriv
->device
))->USBDeviceOpenSeize (dpriv
->device
);
858 if (kresult
!= kIOReturnSuccess
) {
859 usbi_err (HANDLE_CTX (dev_handle
), "USBDeviceOpen: %s", darwin_error_str(kresult
));
862 case kIOReturnExclusiveAccess
:
863 /* it is possible to perform some actions on a device that is not open so do not return an error */
868 (*(dpriv
->device
))->Release (dpriv
->device
);
869 dpriv
->device
= NULL
;
870 return darwin_to_libusb (kresult
);
873 /* create async event source */
874 kresult
= (*(dpriv
->device
))->CreateDeviceAsyncEventSource (dpriv
->device
, &priv
->cfSource
);
875 if (kresult
!= kIOReturnSuccess
) {
876 usbi_err (HANDLE_CTX (dev_handle
), "CreateDeviceAsyncEventSource: %s", darwin_error_str(kresult
));
878 (*(dpriv
->device
))->USBDeviceClose (dpriv
->device
);
879 (*(dpriv
->device
))->Release (dpriv
->device
);
881 dpriv
->device
= NULL
;
882 return darwin_to_libusb (kresult
);
887 CFRetain (libusb_darwin_acfl
);
889 /* add the cfSource to the aync run loop */
890 CFRunLoopAddSource(libusb_darwin_acfl
, priv
->cfSource
, kCFRunLoopCommonModes
);
894 /* device opened successfully */
897 /* create a file descriptor for notifications */
900 /* set the pipe to be non-blocking */
901 fcntl (priv
->fds
[1], F_SETFD
, O_NONBLOCK
);
903 usbi_add_pollfd(HANDLE_CTX(dev_handle
), priv
->fds
[0], POLLIN
);
905 usbi_info (HANDLE_CTX (dev_handle
), "device open for access");
910 static void darwin_close (struct libusb_device_handle
*dev_handle
) {
911 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)dev_handle
->os_priv
;
912 struct darwin_device_priv
*dpriv
= (struct darwin_device_priv
*)dev_handle
->dev
->os_priv
;
916 if (dpriv
->open_count
== 0) {
917 /* something is probably very wrong if this is the case */
918 usbi_err (HANDLE_CTX (dev_handle
), "Close called on a device that was not open!\n");
924 /* make sure all interfaces are released */
925 for (i
= 0 ; i
< USB_MAXINTERFACES
; i
++)
926 if (dev_handle
->claimed_interfaces
& (1 << i
))
927 libusb_release_interface (dev_handle
, i
);
929 if (0 == dpriv
->open_count
) {
931 /* delete the device's async event source */
932 if (priv
->cfSource
) {
933 CFRunLoopRemoveSource (libusb_darwin_acfl
, priv
->cfSource
, kCFRunLoopDefaultMode
);
934 CFRelease (priv
->cfSource
);
937 /* close the device */
938 kresult
= (*(dpriv
->device
))->USBDeviceClose(dpriv
->device
);
940 /* Log the fact that we had a problem closing the file, however failing a
941 * close isn't really an error, so return success anyway */
942 usbi_err (HANDLE_CTX (dev_handle
), "USBDeviceClose: %s", darwin_error_str(kresult
));
946 kresult
= (*(dpriv
->device
))->Release(dpriv
->device
);
948 /* Log the fact that we had a problem closing the file, however failing a
949 * close isn't really an error, so return success anyway */
950 usbi_err (HANDLE_CTX (dev_handle
), "Release: %s", darwin_error_str(kresult
));
953 dpriv
->device
= NULL
;
956 /* file descriptors are maintained per-instance */
957 usbi_remove_pollfd (HANDLE_CTX (dev_handle
), priv
->fds
[0]);
958 close (priv
->fds
[1]);
959 close (priv
->fds
[0]);
961 priv
->fds
[0] = priv
->fds
[1] = -1;
964 static int darwin_get_configuration(struct libusb_device_handle
*dev_handle
, int *config
) {
965 struct darwin_device_priv
*dpriv
= (struct darwin_device_priv
*)dev_handle
->dev
->os_priv
;
967 *config
= (int) dpriv
->active_config
;
972 static int darwin_set_configuration(struct libusb_device_handle
*dev_handle
, int config
) {
973 struct darwin_device_priv
*dpriv
= (struct darwin_device_priv
*)dev_handle
->dev
->os_priv
;
977 /* Setting configuration will invalidate the interface, so we need
978 to reclaim it. First, dispose of existing interfaces, if any. */
979 for (i
= 0 ; i
< USB_MAXINTERFACES
; i
++)
980 if (dev_handle
->claimed_interfaces
& (1 << i
))
981 darwin_release_interface (dev_handle
, i
);
983 kresult
= (*(dpriv
->device
))->SetConfiguration (dpriv
->device
, config
);
984 if (kresult
!= kIOReturnSuccess
)
985 return darwin_to_libusb (kresult
);
987 /* Reclaim any interfaces. */
988 for (i
= 0 ; i
< USB_MAXINTERFACES
; i
++)
989 if (dev_handle
->claimed_interfaces
& (1 << i
))
990 darwin_claim_interface (dev_handle
, i
);
992 dpriv
->active_config
= config
;
997 static int darwin_get_interface (usb_device_t
**darwin_device
, uint8_t ifc
, io_service_t
*usbInterfacep
) {
998 IOUSBFindInterfaceRequest request
;
999 uint8_t current_interface
;
1000 kern_return_t kresult
;
1001 io_iterator_t interface_iterator
;
1003 *usbInterfacep
= IO_OBJECT_NULL
;
1005 /* Setup the Interface Request */
1006 request
.bInterfaceClass
= kIOUSBFindInterfaceDontCare
;
1007 request
.bInterfaceSubClass
= kIOUSBFindInterfaceDontCare
;
1008 request
.bInterfaceProtocol
= kIOUSBFindInterfaceDontCare
;
1009 request
.bAlternateSetting
= kIOUSBFindInterfaceDontCare
;
1011 kresult
= (*(darwin_device
))->CreateInterfaceIterator(darwin_device
, &request
, &interface_iterator
);
1015 for ( current_interface
= 0 ; current_interface
<= ifc
; current_interface
++ ) {
1016 *usbInterfacep
= IOIteratorNext(interface_iterator
);
1017 if (current_interface
!= ifc
)
1018 (void) IOObjectRelease (*usbInterfacep
);
1021 /* done with the interface iterator */
1022 IOObjectRelease(interface_iterator
);
1027 static int get_endpoints (struct libusb_device_handle
*dev_handle
, int iface
) {
1028 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)dev_handle
->os_priv
;
1030 /* current interface */
1031 struct darwin_interface
*cInterface
= &priv
->interfaces
[iface
];
1033 kern_return_t kresult
;
1035 u_int8_t numep
, direction
, number
;
1036 u_int8_t dont_care1
, dont_care3
;
1037 u_int16_t dont_care2
;
1040 usbi_info (HANDLE_CTX (dev_handle
), "building table of endpoints.");
1042 /* retrieve the total number of endpoints on this interface */
1043 kresult
= (*(cInterface
->interface
))->GetNumEndpoints(cInterface
->interface
, &numep
);
1045 usbi_err (HANDLE_CTX (dev_handle
), "can't get number of endpoints for interface: %s", darwin_error_str(kresult
));
1046 return darwin_to_libusb (kresult
);
1049 /* iterate through pipe references */
1050 for (i
= 1 ; i
<= numep
; i
++) {
1051 kresult
= (*(cInterface
->interface
))->GetPipeProperties(cInterface
->interface
, i
, &direction
, &number
, &dont_care1
,
1052 &dont_care2
, &dont_care3
);
1054 if (kresult
!= kIOReturnSuccess
) {
1055 usbi_err (HANDLE_CTX (dev_handle
), "error getting pipe information for pipe %d: %s", i
, darwin_error_str(kresult
));
1057 return darwin_to_libusb (kresult
);
1060 usbi_info (HANDLE_CTX (dev_handle
), "interface: %i pipe %i: dir: %i number: %i", iface
, i
, direction
, number
);
1062 cInterface
->endpoint_addrs
[i
- 1] = ((direction
<< 7 & LIBUSB_ENDPOINT_DIR_MASK
) | (number
& LIBUSB_ENDPOINT_ADDRESS_MASK
));
1065 cInterface
->num_endpoints
= numep
;
1070 static int darwin_claim_interface(struct libusb_device_handle
*dev_handle
, int iface
) {
1071 struct darwin_device_priv
*dpriv
= (struct darwin_device_priv
*)dev_handle
->dev
->os_priv
;
1072 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)dev_handle
->os_priv
;
1073 io_service_t usbInterface
= IO_OBJECT_NULL
;
1075 IOCFPlugInInterface
**plugInInterface
= NULL
;
1078 /* current interface */
1079 struct darwin_interface
*cInterface
= &priv
->interfaces
[iface
];
1081 kresult
= darwin_get_interface (dpriv
->device
, iface
, &usbInterface
);
1082 if (kresult
!= kIOReturnSuccess
)
1083 return darwin_to_libusb (kresult
);
1085 /* make sure we have an interface */
1086 if (!usbInterface
&& dpriv
->first_config
!= 0) {
1087 usbi_info (HANDLE_CTX (dev_handle
), "no interface found; setting configuration: %d", dpriv
->first_config
);
1089 /* set the configuration */
1090 kresult
= darwin_set_configuration (dev_handle
, dpriv
->first_config
);
1091 if (kresult
!= LIBUSB_SUCCESS
) {
1092 usbi_err (HANDLE_CTX (dev_handle
), "could not set configuration");
1096 kresult
= darwin_get_interface (dpriv
->device
, iface
, &usbInterface
);
1098 usbi_err (HANDLE_CTX (dev_handle
), "darwin_get_interface: %s", darwin_error_str(kresult
));
1099 return darwin_to_libusb (kresult
);
1103 if (!usbInterface
) {
1104 usbi_err (HANDLE_CTX (dev_handle
), "interface not found");
1105 return LIBUSB_ERROR_NOT_FOUND
;
1108 /* get an interface to the device's interface */
1109 kresult
= IOCreatePlugInInterfaceForService (usbInterface
, kIOUSBInterfaceUserClientTypeID
,
1110 kIOCFPlugInInterfaceID
, &plugInInterface
, &score
);
1112 /* ignore release error */
1113 (void)IOObjectRelease (usbInterface
);
1116 usbi_err (HANDLE_CTX (dev_handle
), "IOCreatePlugInInterfaceForService: %s", darwin_error_str(kresult
));
1117 return darwin_to_libusb (kresult
);
1120 if (!plugInInterface
) {
1121 usbi_err (HANDLE_CTX (dev_handle
), "plugin interface not found");
1122 return LIBUSB_ERROR_NOT_FOUND
;
1125 /* Do the actual claim */
1126 kresult
= (*plugInInterface
)->QueryInterface(plugInInterface
,
1127 CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID
),
1128 (LPVOID
)&cInterface
->interface
);
1129 /* We no longer need the intermediate plug-in */
1130 IODestroyPlugInInterface (plugInInterface
);
1131 if (kresult
|| !cInterface
->interface
) {
1132 usbi_err (HANDLE_CTX (dev_handle
), "QueryInterface: %s", darwin_error_str(kresult
));
1133 return darwin_to_libusb (kresult
);
1136 /* claim the interface */
1137 kresult
= (*(cInterface
->interface
))->USBInterfaceOpen(cInterface
->interface
);
1139 usbi_err (HANDLE_CTX (dev_handle
), "USBInterfaceOpen: %s", darwin_error_str(kresult
));
1140 return darwin_to_libusb (kresult
);
1143 /* update list of endpoints */
1144 kresult
= get_endpoints (dev_handle
, iface
);
1146 /* this should not happen */
1147 darwin_release_interface (dev_handle
, iface
);
1148 usbi_err (HANDLE_CTX (dev_handle
), "could not build endpoint table");
1152 cInterface
->cfSource
= NULL
;
1154 /* create async event source */
1155 kresult
= (*(cInterface
->interface
))->CreateInterfaceAsyncEventSource (cInterface
->interface
, &cInterface
->cfSource
);
1156 if (kresult
!= kIOReturnSuccess
) {
1157 usbi_err (HANDLE_CTX (dev_handle
), "could not create async event source");
1159 /* can't continue without an async event source */
1160 (void)darwin_release_interface (dev_handle
, iface
);
1162 return darwin_to_libusb (kresult
);
1165 /* add the cfSource to the async thread's run loop */
1166 CFRunLoopAddSource(libusb_darwin_acfl
, cInterface
->cfSource
, kCFRunLoopDefaultMode
);
1168 usbi_info (HANDLE_CTX (dev_handle
), "interface opened");
1173 static int darwin_release_interface(struct libusb_device_handle
*dev_handle
, int iface
) {
1174 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)dev_handle
->os_priv
;
1177 /* current interface */
1178 struct darwin_interface
*cInterface
= &priv
->interfaces
[iface
];
1180 /* Check to see if an interface is open */
1181 if (!cInterface
->interface
)
1182 return LIBUSB_SUCCESS
;
1184 /* clean up endpoint data */
1185 cInterface
->num_endpoints
= 0;
1187 /* delete the interface's async event source */
1188 if (cInterface
->cfSource
) {
1189 CFRunLoopRemoveSource (libusb_darwin_acfl
, cInterface
->cfSource
, kCFRunLoopDefaultMode
);
1190 CFRelease (cInterface
->cfSource
);
1193 kresult
= (*(cInterface
->interface
))->USBInterfaceClose(cInterface
->interface
);
1195 usbi_err (HANDLE_CTX (dev_handle
), "USBInterfaceClose: %s", darwin_error_str(kresult
));
1197 kresult
= (*(cInterface
->interface
))->Release(cInterface
->interface
);
1198 if (kresult
!= kIOReturnSuccess
)
1199 usbi_err (HANDLE_CTX (dev_handle
), "Release: %s", darwin_error_str(kresult
));
1201 cInterface
->interface
= IO_OBJECT_NULL
;
1203 return darwin_to_libusb (kresult
);
1206 static int darwin_set_interface_altsetting(struct libusb_device_handle
*dev_handle
, int iface
, int altsetting
) {
1207 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)dev_handle
->os_priv
;
1210 /* current interface */
1211 struct darwin_interface
*cInterface
= &priv
->interfaces
[iface
];
1213 if (!cInterface
->interface
)
1214 return LIBUSB_ERROR_NO_DEVICE
;
1216 kresult
= (*(cInterface
->interface
))->SetAlternateInterface (cInterface
->interface
, altsetting
);
1217 if (kresult
!= kIOReturnSuccess
)
1218 darwin_reset_device (dev_handle
);
1220 /* update list of endpoints */
1221 kresult
= get_endpoints (dev_handle
, iface
);
1223 /* this should not happen */
1224 darwin_release_interface (dev_handle
, iface
);
1225 usbi_err (HANDLE_CTX (dev_handle
), "could not build endpoint table");
1229 return darwin_to_libusb (kresult
);
1232 static int darwin_clear_halt(struct libusb_device_handle
*dev_handle
, unsigned char endpoint
) {
1233 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)dev_handle
->os_priv
;
1235 /* current interface */
1236 struct darwin_interface
*cInterface
;
1237 uint8_t pipeRef
, iface
;
1240 /* determine the interface/endpoint to use */
1241 if (ep_to_pipeRef (dev_handle
, endpoint
, &pipeRef
, &iface
) != 0) {
1242 usbi_err (HANDLE_CTX (dev_handle
), "endpoint not found on any open interface");
1244 return LIBUSB_ERROR_NOT_FOUND
;
1247 cInterface
= &priv
->interfaces
[iface
];
1249 #if (InterfaceVersion < 190)
1250 kresult
= (*(cInterface
->interface
))->ClearPipeStall(cInterface
->interface
, pipeRef
);
1252 /* newer versions of darwin support clearing additional bits on the device's endpoint */
1253 kresult
= (*(cInterface
->interface
))->ClearPipeStallBothEnds(cInterface
->interface
, pipeRef
);
1256 usbi_err (HANDLE_CTX (dev_handle
), "ClearPipeStall: %s", darwin_error_str (kresult
));
1258 return darwin_to_libusb (kresult
);
1261 static int darwin_reset_device(struct libusb_device_handle
*dev_handle
) {
1262 struct darwin_device_priv
*dpriv
= (struct darwin_device_priv
*)dev_handle
->dev
->os_priv
;
1265 kresult
= (*(dpriv
->device
))->ResetDevice (dpriv
->device
);
1267 usbi_err (HANDLE_CTX (dev_handle
), "ResetDevice: %s", darwin_error_str (kresult
));
1269 return darwin_to_libusb (kresult
);
1272 static int darwin_kernel_driver_active(struct libusb_device_handle
*dev_handle
, int interface
) {
1273 struct darwin_device_priv
*dpriv
= (struct darwin_device_priv
*)dev_handle
->dev
->os_priv
;
1274 io_service_t usbInterface
;
1278 kresult
= darwin_get_interface (dpriv
->device
, interface
, &usbInterface
);
1280 usbi_err (HANDLE_CTX (dev_handle
), "darwin_get_interface: %s", darwin_error_str(kresult
));
1282 return darwin_to_libusb (kresult
);
1285 driver
= IORegistryEntryCreateCFProperty (usbInterface
, kIOBundleIdentifierKey
, kCFAllocatorDefault
, 0);
1286 IOObjectRelease (usbInterface
);
1298 /* attaching/detaching kernel drivers is not currently supported (maybe in the future?) */
1299 static int darwin_attach_kernel_driver (struct libusb_device_handle
*dev_handle
, int interface
) {
1302 return LIBUSB_ERROR_NOT_SUPPORTED
;
1305 static int darwin_detach_kernel_driver (struct libusb_device_handle
*dev_handle
, int interface
) {
1308 return LIBUSB_ERROR_NOT_SUPPORTED
;
1311 static void darwin_destroy_device(struct libusb_device
*dev
) {
1315 static int submit_bulk_transfer(struct usbi_transfer
*itransfer
) {
1316 struct libusb_transfer
*transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
1317 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)transfer
->dev_handle
->os_priv
;
1320 uint8_t transferType
;
1321 /* None of the values below are used in libusbx for bulk transfers */
1322 uint8_t direction
, number
, interval
, pipeRef
, iface
;
1323 uint16_t maxPacketSize
;
1325 struct darwin_interface
*cInterface
;
1327 if (IS_XFEROUT(transfer
) && transfer
->flags
& LIBUSB_TRANSFER_ADD_ZERO_PACKET
)
1328 return LIBUSB_ERROR_NOT_SUPPORTED
;
1330 if (ep_to_pipeRef (transfer
->dev_handle
, transfer
->endpoint
, &pipeRef
, &iface
) != 0) {
1331 usbi_err (TRANSFER_CTX (transfer
), "endpoint not found on any open interface");
1333 return LIBUSB_ERROR_NOT_FOUND
;
1336 cInterface
= &priv
->interfaces
[iface
];
1338 (*(cInterface
->interface
))->GetPipeProperties (cInterface
->interface
, pipeRef
, &direction
, &number
,
1339 &transferType
, &maxPacketSize
, &interval
);
1341 /* submit the request */
1342 /* timeouts are unavailable on interrupt endpoints */
1343 if (transferType
== kUSBInterrupt
) {
1344 if (IS_XFERIN(transfer
))
1345 ret
= (*(cInterface
->interface
))->ReadPipeAsync(cInterface
->interface
, pipeRef
, transfer
->buffer
,
1346 transfer
->length
, darwin_async_io_callback
, itransfer
);
1348 ret
= (*(cInterface
->interface
))->WritePipeAsync(cInterface
->interface
, pipeRef
, transfer
->buffer
,
1349 transfer
->length
, darwin_async_io_callback
, itransfer
);
1351 itransfer
->flags
|= USBI_TRANSFER_OS_HANDLES_TIMEOUT
;
1353 if (IS_XFERIN(transfer
))
1354 ret
= (*(cInterface
->interface
))->ReadPipeAsyncTO(cInterface
->interface
, pipeRef
, transfer
->buffer
,
1355 transfer
->length
, transfer
->timeout
, transfer
->timeout
,
1356 darwin_async_io_callback
, (void *)itransfer
);
1358 ret
= (*(cInterface
->interface
))->WritePipeAsyncTO(cInterface
->interface
, pipeRef
, transfer
->buffer
,
1359 transfer
->length
, transfer
->timeout
, transfer
->timeout
,
1360 darwin_async_io_callback
, (void *)itransfer
);
1364 usbi_err (TRANSFER_CTX (transfer
), "bulk transfer failed (dir = %s): %s (code = 0x%08x)", IS_XFERIN(transfer
) ? "In" : "Out",
1365 darwin_error_str(ret
), ret
);
1367 return darwin_to_libusb (ret
);
1370 static int submit_iso_transfer(struct usbi_transfer
*itransfer
) {
1371 struct libusb_transfer
*transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
1372 struct darwin_transfer_priv
*tpriv
= usbi_transfer_get_os_priv(itransfer
);
1373 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)transfer
->dev_handle
->os_priv
;
1376 uint8_t pipeRef
, iface
;
1378 AbsoluteTime atTime
;
1381 struct darwin_interface
*cInterface
;
1383 /* construct an array of IOUSBIsocFrames, reuse the old one if possible */
1384 if (tpriv
->isoc_framelist
&& tpriv
->num_iso_packets
!= transfer
->num_iso_packets
) {
1385 free(tpriv
->isoc_framelist
);
1386 tpriv
->isoc_framelist
= NULL
;
1389 if (!tpriv
->isoc_framelist
) {
1390 tpriv
->num_iso_packets
= transfer
->num_iso_packets
;
1391 tpriv
->isoc_framelist
= (IOUSBIsocFrame
*) calloc (transfer
->num_iso_packets
, sizeof(IOUSBIsocFrame
));
1392 if (!tpriv
->isoc_framelist
)
1393 return LIBUSB_ERROR_NO_MEM
;
1396 /* copy the frame list from the libusbx descriptor (the structures differ only is member order) */
1397 for (i
= 0 ; i
< transfer
->num_iso_packets
; i
++)
1398 tpriv
->isoc_framelist
[i
].frReqCount
= transfer
->iso_packet_desc
[i
].length
;
1400 /* determine the interface/endpoint to use */
1401 if (ep_to_pipeRef (transfer
->dev_handle
, transfer
->endpoint
, &pipeRef
, &iface
) != 0) {
1402 usbi_err (TRANSFER_CTX (transfer
), "endpoint not found on any open interface");
1404 return LIBUSB_ERROR_NOT_FOUND
;
1407 cInterface
= &priv
->interfaces
[iface
];
1409 /* Last but not least we need the bus frame number */
1410 kresult
= (*(cInterface
->interface
))->GetBusFrameNumber(cInterface
->interface
, &frame
, &atTime
);
1412 usbi_err (TRANSFER_CTX (transfer
), "failed to get bus frame number: %d", kresult
);
1413 free(tpriv
->isoc_framelist
);
1414 tpriv
->isoc_framelist
= NULL
;
1416 return darwin_to_libusb (kresult
);
1419 /* schedule for a frame a little in the future */
1422 if (cInterface
->frames
[transfer
->endpoint
] && frame
< cInterface
->frames
[transfer
->endpoint
])
1423 frame
= cInterface
->frames
[transfer
->endpoint
];
1425 /* submit the request */
1426 if (IS_XFERIN(transfer
))
1427 kresult
= (*(cInterface
->interface
))->ReadIsochPipeAsync(cInterface
->interface
, pipeRef
, transfer
->buffer
, frame
,
1428 transfer
->num_iso_packets
, tpriv
->isoc_framelist
, darwin_async_io_callback
,
1431 kresult
= (*(cInterface
->interface
))->WriteIsochPipeAsync(cInterface
->interface
, pipeRef
, transfer
->buffer
, frame
,
1432 transfer
->num_iso_packets
, tpriv
->isoc_framelist
, darwin_async_io_callback
,
1435 cInterface
->frames
[transfer
->endpoint
] = frame
+ transfer
->num_iso_packets
/ 8;
1437 if (kresult
!= kIOReturnSuccess
) {
1438 usbi_err (TRANSFER_CTX (transfer
), "isochronous transfer failed (dir: %s): %s", IS_XFERIN(transfer
) ? "In" : "Out",
1439 darwin_error_str(kresult
));
1440 free (tpriv
->isoc_framelist
);
1441 tpriv
->isoc_framelist
= NULL
;
1444 return darwin_to_libusb (kresult
);
1447 static int submit_control_transfer(struct usbi_transfer
*itransfer
) {
1448 struct libusb_transfer
*transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
1449 struct libusb_control_setup
*setup
= (struct libusb_control_setup
*) transfer
->buffer
;
1450 struct darwin_device_priv
*dpriv
= (struct darwin_device_priv
*)transfer
->dev_handle
->dev
->os_priv
;
1451 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)transfer
->dev_handle
->os_priv
;
1452 struct darwin_transfer_priv
*tpriv
= usbi_transfer_get_os_priv(itransfer
);
1456 bzero(&tpriv
->req
, sizeof(tpriv
->req
));
1458 /* IOUSBDeviceInterface expects the request in cpu endianess */
1459 tpriv
->req
.bmRequestType
= setup
->bmRequestType
;
1460 tpriv
->req
.bRequest
= setup
->bRequest
;
1461 /* these values should be in bus order from libusb_fill_control_setup */
1462 tpriv
->req
.wValue
= OSSwapLittleToHostInt16 (setup
->wValue
);
1463 tpriv
->req
.wIndex
= OSSwapLittleToHostInt16 (setup
->wIndex
);
1464 tpriv
->req
.wLength
= OSSwapLittleToHostInt16 (setup
->wLength
);
1465 /* data is stored after the libusbx control block */
1466 tpriv
->req
.pData
= transfer
->buffer
+ LIBUSB_CONTROL_SETUP_SIZE
;
1467 tpriv
->req
.completionTimeout
= transfer
->timeout
;
1468 tpriv
->req
.noDataTimeout
= transfer
->timeout
;
1470 itransfer
->flags
|= USBI_TRANSFER_OS_HANDLES_TIMEOUT
;
1472 /* all transfers in libusb-1.0 are async */
1474 if (transfer
->endpoint
) {
1475 struct darwin_interface
*cInterface
;
1476 uint8_t pipeRef
, iface
;
1478 if (ep_to_pipeRef (transfer
->dev_handle
, transfer
->endpoint
, &pipeRef
, &iface
) != 0) {
1479 usbi_err (TRANSFER_CTX (transfer
), "endpoint not found on any open interface");
1481 return LIBUSB_ERROR_NOT_FOUND
;
1484 cInterface
= &priv
->interfaces
[iface
];
1486 kresult
= (*(cInterface
->interface
))->ControlRequestAsyncTO (cInterface
->interface
, pipeRef
, &(tpriv
->req
), darwin_async_io_callback
, itransfer
);
1488 /* control request on endpoint 0 */
1489 kresult
= (*(dpriv
->device
))->DeviceRequestAsyncTO(dpriv
->device
, &(tpriv
->req
), darwin_async_io_callback
, itransfer
);
1491 if (kresult
!= kIOReturnSuccess
)
1492 usbi_err (TRANSFER_CTX (transfer
), "control request failed: %s", darwin_error_str(kresult
));
1494 return darwin_to_libusb (kresult
);
1497 static int darwin_submit_transfer(struct usbi_transfer
*itransfer
) {
1498 struct libusb_transfer
*transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
1500 switch (transfer
->type
) {
1501 case LIBUSB_TRANSFER_TYPE_CONTROL
:
1502 return submit_control_transfer(itransfer
);
1503 case LIBUSB_TRANSFER_TYPE_BULK
:
1504 case LIBUSB_TRANSFER_TYPE_INTERRUPT
:
1505 return submit_bulk_transfer(itransfer
);
1506 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
:
1507 return submit_iso_transfer(itransfer
);
1509 usbi_err (TRANSFER_CTX(transfer
), "unknown endpoint type %d", transfer
->type
);
1510 return LIBUSB_ERROR_INVALID_PARAM
;
1514 static int cancel_control_transfer(struct usbi_transfer
*itransfer
) {
1515 struct libusb_transfer
*transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
1516 struct darwin_device_priv
*dpriv
= (struct darwin_device_priv
*)transfer
->dev_handle
->dev
->os_priv
;
1519 usbi_info (ITRANSFER_CTX (itransfer
), "WARNING: aborting all transactions control pipe");
1522 return LIBUSB_ERROR_NO_DEVICE
;
1524 kresult
= (*(dpriv
->device
))->USBDeviceAbortPipeZero (dpriv
->device
);
1526 return darwin_to_libusb (kresult
);
1529 static int darwin_abort_transfers (struct usbi_transfer
*itransfer
) {
1530 struct libusb_transfer
*transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
1531 struct darwin_device_priv
*dpriv
= (struct darwin_device_priv
*)transfer
->dev_handle
->dev
->os_priv
;
1532 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)transfer
->dev_handle
->os_priv
;
1533 struct darwin_interface
*cInterface
;
1534 uint8_t pipeRef
, iface
;
1537 if (ep_to_pipeRef (transfer
->dev_handle
, transfer
->endpoint
, &pipeRef
, &iface
) != 0) {
1538 usbi_err (TRANSFER_CTX (transfer
), "endpoint not found on any open interface");
1540 return LIBUSB_ERROR_NOT_FOUND
;
1543 cInterface
= &priv
->interfaces
[iface
];
1546 return LIBUSB_ERROR_NO_DEVICE
;
1548 usbi_info (ITRANSFER_CTX (itransfer
), "WARNING: aborting all transactions on interface %d pipe %d", iface
, pipeRef
);
1550 /* abort transactions */
1551 (*(cInterface
->interface
))->AbortPipe (cInterface
->interface
, pipeRef
);
1553 usbi_info (ITRANSFER_CTX (itransfer
), "calling clear pipe stall to clear the data toggle bit");
1555 /* clear the data toggle bit */
1556 #if (InterfaceVersion < 190)
1557 kresult
= (*(cInterface
->interface
))->ClearPipeStall(cInterface
->interface
, pipeRef
);
1559 /* newer versions of darwin support clearing additional bits on the device's endpoint */
1560 kresult
= (*(cInterface
->interface
))->ClearPipeStallBothEnds(cInterface
->interface
, pipeRef
);
1563 return darwin_to_libusb (kresult
);
1566 static int darwin_cancel_transfer(struct usbi_transfer
*itransfer
) {
1567 struct libusb_transfer
*transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
1569 switch (transfer
->type
) {
1570 case LIBUSB_TRANSFER_TYPE_CONTROL
:
1571 return cancel_control_transfer(itransfer
);
1572 case LIBUSB_TRANSFER_TYPE_BULK
:
1573 case LIBUSB_TRANSFER_TYPE_INTERRUPT
:
1574 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
:
1575 return darwin_abort_transfers (itransfer
);
1577 usbi_err (TRANSFER_CTX(transfer
), "unknown endpoint type %d", transfer
->type
);
1578 return LIBUSB_ERROR_INVALID_PARAM
;
1582 static void darwin_clear_transfer_priv (struct usbi_transfer
*itransfer
) {
1583 struct libusb_transfer
*transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
1584 struct darwin_transfer_priv
*tpriv
= usbi_transfer_get_os_priv(itransfer
);
1586 if (transfer
->type
== LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
&& tpriv
->isoc_framelist
) {
1587 free (tpriv
->isoc_framelist
);
1588 tpriv
->isoc_framelist
= NULL
;
1592 static void darwin_async_io_callback (void *refcon
, IOReturn result
, void *arg0
) {
1593 struct usbi_transfer
*itransfer
= (struct usbi_transfer
*)refcon
;
1594 struct libusb_transfer
*transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
1595 struct darwin_device_handle_priv
*priv
= (struct darwin_device_handle_priv
*)transfer
->dev_handle
->os_priv
;
1596 UInt32 message
, size
;
1598 usbi_info (ITRANSFER_CTX (itransfer
), "an async io operation has completed");
1600 /* The size should never be larger than 4 GB - Also see libusb bug #117 */
1601 if ((intptr_t) arg0
> UINT32_MAX
)
1602 usbi_err (ITRANSFER_CTX (itransfer
),
1603 "async size truncation detected - please report this error");
1604 size
= (UInt32
) (intptr_t) arg0
;
1606 /* send a completion message to the device's file descriptor */
1607 message
= MESSAGE_ASYNC_IO_COMPLETE
;
1608 write (priv
->fds
[1], &message
, sizeof (message
));
1609 write (priv
->fds
[1], &itransfer
, sizeof (itransfer
));
1610 write (priv
->fds
[1], &result
, sizeof (IOReturn
));
1611 write (priv
->fds
[1], &size
, sizeof (size
));
1614 static int darwin_transfer_status (struct usbi_transfer
*itransfer
, kern_return_t result
) {
1615 if (itransfer
->flags
& USBI_TRANSFER_TIMED_OUT
)
1616 result
= kIOUSBTransactionTimeout
;
1619 case kIOReturnUnderrun
:
1620 case kIOReturnSuccess
:
1621 return LIBUSB_TRANSFER_COMPLETED
;
1622 case kIOReturnAborted
:
1623 return LIBUSB_TRANSFER_CANCELLED
;
1624 case kIOUSBPipeStalled
:
1625 usbi_warn (ITRANSFER_CTX (itransfer
), "transfer error: pipe is stalled");
1626 return LIBUSB_TRANSFER_STALL
;
1627 case kIOReturnOverrun
:
1628 usbi_err (ITRANSFER_CTX (itransfer
), "transfer error: data overrun");
1629 return LIBUSB_TRANSFER_OVERFLOW
;
1630 case kIOUSBTransactionTimeout
:
1631 usbi_err (ITRANSFER_CTX (itransfer
), "transfer error: timed out");
1632 itransfer
->flags
|= USBI_TRANSFER_TIMED_OUT
;
1633 return LIBUSB_TRANSFER_TIMED_OUT
;
1635 usbi_err (ITRANSFER_CTX (itransfer
), "transfer error: %s (value = 0x%08x)", darwin_error_str (result
), result
);
1636 return LIBUSB_TRANSFER_ERROR
;
1640 static void darwin_handle_callback (struct usbi_transfer
*itransfer
, kern_return_t result
, UInt32 io_size
) {
1641 struct libusb_transfer
*transfer
= USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer
);
1642 struct darwin_transfer_priv
*tpriv
= usbi_transfer_get_os_priv(itransfer
);
1643 int isIsoc
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
== transfer
->type
;
1644 int isBulk
= LIBUSB_TRANSFER_TYPE_BULK
== transfer
->type
;
1645 int isControl
= LIBUSB_TRANSFER_TYPE_CONTROL
== transfer
->type
;
1646 int isInterrupt
= LIBUSB_TRANSFER_TYPE_INTERRUPT
== transfer
->type
;
1649 if (!isIsoc
&& !isBulk
&& !isControl
&& !isInterrupt
) {
1650 usbi_err (TRANSFER_CTX(transfer
), "unknown endpoint type %d", transfer
->type
);
1654 usbi_info (ITRANSFER_CTX (itransfer
), "handling %s completion with kernel status %d",
1655 isControl
? "control" : isBulk
? "bulk" : isIsoc
? "isoc" : "interrupt", result
);
1657 if (kIOReturnSuccess
== result
|| kIOReturnUnderrun
== result
) {
1658 if (isIsoc
&& tpriv
->isoc_framelist
) {
1659 /* copy isochronous results back */
1661 for (i
= 0; i
< transfer
->num_iso_packets
; i
++) {
1662 struct libusb_iso_packet_descriptor
*lib_desc
= &transfer
->iso_packet_desc
[i
];
1663 lib_desc
->status
= darwin_to_libusb (tpriv
->isoc_framelist
[i
].frStatus
);
1664 lib_desc
->actual_length
= tpriv
->isoc_framelist
[i
].frActCount
;
1667 itransfer
->transferred
+= io_size
;
1670 /* it is ok to handle cancelled transfers without calling usbi_handle_transfer_cancellation (we catch timeout transfers) */
1671 usbi_handle_transfer_completion (itransfer
, darwin_transfer_status (itransfer
, result
));
1674 static int op_handle_events(struct libusb_context
*ctx
, struct pollfd
*fds
, POLL_NFDS_TYPE nfds
, int num_ready
) {
1675 struct usbi_transfer
*itransfer
;
1678 POLL_NFDS_TYPE i
= 0;
1682 usbi_mutex_lock(&ctx
->open_devs_lock
);
1683 for (i
= 0; i
< nfds
&& num_ready
> 0; i
++) {
1684 struct pollfd
*pollfd
= &fds
[i
];
1685 struct libusb_device_handle
*handle
;
1686 struct darwin_device_handle_priv
*hpriv
= NULL
;
1688 usbi_info (ctx
, "checking fd %i with revents = %x", fds
[i
], pollfd
->revents
);
1690 if (!pollfd
->revents
)
1694 list_for_each_entry(handle
, &ctx
->open_devs
, list
, struct libusb_device_handle
) {
1695 hpriv
= (struct darwin_device_handle_priv
*)handle
->os_priv
;
1696 if (hpriv
->fds
[0] == pollfd
->fd
)
1700 if (!(pollfd
->revents
& POLLERR
)) {
1701 ret
= read (hpriv
->fds
[0], &message
, sizeof (message
));
1702 if (ret
< (ssize_t
)sizeof (message
))
1705 /* could not poll the device-- response is to delete the device (this seems a little heavy-handed) */
1706 message
= MESSAGE_DEVICE_GONE
;
1709 case MESSAGE_DEVICE_GONE
:
1710 /* remove the device's async port from the runloop */
1711 if (hpriv
->cfSource
) {
1712 if (libusb_darwin_acfl
)
1713 CFRunLoopRemoveSource (libusb_darwin_acfl
, hpriv
->cfSource
, kCFRunLoopDefaultMode
);
1714 CFRelease (hpriv
->cfSource
);
1715 hpriv
->cfSource
= NULL
;
1718 usbi_remove_pollfd(HANDLE_CTX(handle
), hpriv
->fds
[0]);
1719 usbi_handle_disconnect(handle
);
1721 /* done with this device */
1723 case MESSAGE_ASYNC_IO_COMPLETE
:
1724 read (hpriv
->fds
[0], &itransfer
, sizeof (itransfer
));
1725 read (hpriv
->fds
[0], &kresult
, sizeof (IOReturn
));
1726 read (hpriv
->fds
[0], &io_size
, sizeof (UInt32
));
1728 darwin_handle_callback (itransfer
, kresult
, io_size
);
1731 usbi_err (ctx
, "unknown message received from device pipe");
1735 usbi_mutex_unlock(&ctx
->open_devs_lock
);
1740 static int darwin_clock_gettime(int clk_id
, struct timespec
*tp
) {
1741 mach_timespec_t sys_time
;
1742 clock_serv_t clock_ref
;
1745 case USBI_CLOCK_REALTIME
:
1746 /* CLOCK_REALTIME represents time since the epoch */
1747 clock_ref
= clock_realtime
;
1749 case USBI_CLOCK_MONOTONIC
:
1750 /* use system boot time as reference for the monotonic clock */
1751 clock_ref
= clock_monotonic
;
1754 return LIBUSB_ERROR_INVALID_PARAM
;
1757 clock_get_time (clock_ref
, &sys_time
);
1759 tp
->tv_sec
= sys_time
.tv_sec
;
1760 tp
->tv_nsec
= sys_time
.tv_nsec
;
1765 const struct usbi_os_backend darwin_backend
= {
1767 .init
= darwin_init
,
1768 .exit
= darwin_exit
,
1769 .get_device_list
= darwin_get_device_list
,
1770 .get_device_descriptor
= darwin_get_device_descriptor
,
1771 .get_active_config_descriptor
= darwin_get_active_config_descriptor
,
1772 .get_config_descriptor
= darwin_get_config_descriptor
,
1774 .open
= darwin_open
,
1775 .close
= darwin_close
,
1776 .get_configuration
= darwin_get_configuration
,
1777 .set_configuration
= darwin_set_configuration
,
1778 .claim_interface
= darwin_claim_interface
,
1779 .release_interface
= darwin_release_interface
,
1781 .set_interface_altsetting
= darwin_set_interface_altsetting
,
1782 .clear_halt
= darwin_clear_halt
,
1783 .reset_device
= darwin_reset_device
,
1785 .kernel_driver_active
= darwin_kernel_driver_active
,
1786 .detach_kernel_driver
= darwin_detach_kernel_driver
,
1787 .attach_kernel_driver
= darwin_attach_kernel_driver
,
1789 .destroy_device
= darwin_destroy_device
,
1791 .submit_transfer
= darwin_submit_transfer
,
1792 .cancel_transfer
= darwin_cancel_transfer
,
1793 .clear_transfer_priv
= darwin_clear_transfer_priv
,
1795 .handle_events
= op_handle_events
,
1797 .clock_gettime
= darwin_clock_gettime
,
1799 .device_priv_size
= sizeof(struct darwin_device_priv
),
1800 .device_handle_priv_size
= sizeof(struct darwin_device_handle_priv
),
1801 .transfer_priv_size
= sizeof(struct darwin_transfer_priv
),
1802 .add_iso_packet_size
= 0,