| 1 | --- a/libusb/libusbi.h |
| 2 | +++ b/libusb/libusbi.h |
| 3 | @@ -288,13 +288,21 @@ struct libusb_device_handle { |
| 4 | unsigned char os_priv[0]; |
| 5 | }; |
| 6 | |
| 7 | -#define USBI_TRANSFER_TIMED_OUT (1<<0) |
| 8 | - |
| 9 | enum { |
| 10 | USBI_CLOCK_MONOTONIC, |
| 11 | USBI_CLOCK_REALTIME |
| 12 | }; |
| 13 | |
| 14 | +/* flags for struct usbi_transfer: |
| 15 | + * USBI_TRANSFER_TIMED_OUT |
| 16 | + * USBI_TRANSFER_OS_HANDLES_TIMEOUT - set in the backend submit_transfer if the os will handle the timeout |
| 17 | + */ |
| 18 | +enum { |
| 19 | + USBI_TRANSFER_TIMED_OUT = (1 << 0), |
| 20 | + USBI_TRANSFER_OS_HANDLES_TIMEOUT = (1 << 1) |
| 21 | +}; |
| 22 | + |
| 23 | + |
| 24 | /* in-memory transfer layout: |
| 25 | * |
| 26 | * 1. struct usbi_transfer |
| 27 | diff --git a/libusb/os/darwin_usb.c b/libusb/os/darwin_usb.c |
| 28 | index 646c938..79ba6c6 100644 |
| 29 | --- a/libusb/os/darwin_usb.c |
| 30 | +++ b/libusb/os/darwin_usb.c |
| 31 | @@ -1137,6 +1137,8 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer) { |
| 32 | ret = (*(cInterface->interface))->WritePipeAsync(cInterface->interface, pipeRef, transfer->buffer, |
| 33 | transfer->length, darwin_async_io_callback, itransfer); |
| 34 | } else { |
| 35 | + itransfer->flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT; |
| 36 | + |
| 37 | if (is_read) |
| 38 | ret = (*(cInterface->interface))->ReadPipeAsyncTO(cInterface->interface, pipeRef, transfer->buffer, |
| 39 | transfer->length, transfer->timeout, transfer->timeout, |
| 40 | @@ -1171,10 +1173,19 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) { |
| 41 | /* are we reading or writing? */ |
| 42 | is_read = transfer->endpoint & LIBUSB_ENDPOINT_IN; |
| 43 | |
| 44 | - /* construct an array of IOUSBIsocFrames */ |
| 45 | - tpriv->isoc_framelist = (IOUSBIsocFrame*) calloc (transfer->num_iso_packets, sizeof(IOUSBIsocFrame)); |
| 46 | - if (!tpriv->isoc_framelist) |
| 47 | - return LIBUSB_ERROR_NO_MEM; |
| 48 | + /* construct an array of IOUSBIsocFrames, reuse the old one if possible */ |
| 49 | + if (tpriv->isoc_framelist != NULL && tpriv->num_iso_packets != transfer->num_iso_packets) |
| 50 | + { |
| 51 | + free(tpriv->isoc_framelist); |
| 52 | + tpriv->isoc_framelist = NULL; |
| 53 | + } |
| 54 | + if (tpriv->isoc_framelist == NULL) |
| 55 | + { |
| 56 | + tpriv->isoc_framelist = (IOUSBIsocFrame*) calloc (transfer->num_iso_packets, sizeof(IOUSBIsocFrame)); |
| 57 | + tpriv->num_iso_packets = transfer->num_iso_packets; |
| 58 | + if (!tpriv->isoc_framelist) |
| 59 | + return LIBUSB_ERROR_NO_MEM; |
| 60 | + } |
| 61 | |
| 62 | /* copy the frame list from the libusb descriptor (the structures differ only is member order) */ |
| 63 | for (i = 0 ; i < transfer->num_iso_packets ; i++) |
| 64 | @@ -1198,9 +1209,11 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) { |
| 65 | |
| 66 | return darwin_to_libusb (kresult); |
| 67 | } |
| 68 | - |
| 69 | /* schedule for a frame a little in the future */ |
| 70 | - frame += 2; |
| 71 | + frame += 4; |
| 72 | + |
| 73 | + if(cInterface->frames[transfer->endpoint] && frame < cInterface->frames[transfer->endpoint]) |
| 74 | + frame = cInterface->frames[transfer->endpoint]; |
| 75 | |
| 76 | /* submit the request */ |
| 77 | if (is_read) |
| 78 | @@ -1211,6 +1224,7 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) { |
| 79 | kresult = (*(cInterface->interface))->WriteIsochPipeAsync(cInterface->interface, pipeRef, transfer->buffer, frame, |
| 80 | transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback, |
| 81 | itransfer); |
| 82 | + cInterface->frames[transfer->endpoint] = frame + transfer->num_iso_packets / 8; |
| 83 | |
| 84 | if (kresult != kIOReturnSuccess) { |
| 85 | usbi_err (TRANSFER_CTX (transfer), "isochronous transfer failed (dir: %s): %s", is_read ? "In" : "Out", |
| 86 | @@ -1243,6 +1257,8 @@ static int submit_control_transfer(struct usbi_transfer *itransfer) { |
| 87 | tpriv->req.pData = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE; |
| 88 | tpriv->req.completionTimeout = transfer->timeout; |
| 89 | tpriv->req.noDataTimeout = transfer->timeout; |
| 90 | + |
| 91 | + itransfer->flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT; |
| 92 | |
| 93 | /* all transfers in libusb-1.0 are async */ |
| 94 | kresult = (*(dpriv->device))->DeviceRequestAsyncTO(dpriv->device, &(tpriv->req), darwin_async_io_callback, itransfer); |
| 95 | @@ -1358,6 +1374,9 @@ static void darwin_async_io_callback (void *refcon, IOReturn result, void *arg0) |
| 96 | } |
| 97 | |
| 98 | static int darwin_transfer_status (struct usbi_transfer *itransfer, kern_return_t result) { |
| 99 | + if (itransfer->flags & USBI_TRANSFER_TIMED_OUT) |
| 100 | + result = kIOUSBTransactionTimeout; |
| 101 | + |
| 102 | switch (result) { |
| 103 | case kIOReturnUnderrun: |
| 104 | case kIOReturnSuccess: |
| 105 | @@ -1372,6 +1391,7 @@ static int darwin_transfer_status (struct usbi_transfer *itransfer, kern_return_ |
| 106 | return LIBUSB_TRANSFER_OVERFLOW; |
| 107 | case kIOUSBTransactionTimeout: |
| 108 | usbi_err (ITRANSFER_CTX (itransfer), "transfer error: timed out"); |
| 109 | + itransfer->flags |= USBI_TRANSFER_TIMED_OUT; |
| 110 | return LIBUSB_TRANSFER_TIMED_OUT; |
| 111 | default: |
| 112 | usbi_err (ITRANSFER_CTX (itransfer), "transfer error: %s (value = 0x%08x)", darwin_error_str (result), result); |
| 113 | diff --git a/libusb/os/darwin_usb.h b/libusb/os/darwin_usb.h |
| 114 | index a71d464..4aadf8c 100644 |
| 115 | --- a/libusb/os/darwin_usb.h |
| 116 | +++ b/libusb/os/darwin_usb.h |
| 117 | @@ -139,6 +139,7 @@ struct darwin_device_handle_priv { |
| 118 | usb_interface_t **interface; |
| 119 | uint8_t num_endpoints; |
| 120 | CFRunLoopSourceRef cfSource; |
| 121 | + UInt64 frames[256]; |
| 122 | uint8_t endpoint_addrs[USB_MAXENDPOINTS]; |
| 123 | } interfaces[USB_MAXINTERFACES]; |
| 124 | }; |
| 125 | @@ -146,6 +147,7 @@ struct darwin_device_handle_priv { |
| 126 | struct darwin_transfer_priv { |
| 127 | /* Isoc */ |
| 128 | IOUSBIsocFrame *isoc_framelist; |
| 129 | + size_t num_iso_packets; |
| 130 | |
| 131 | /* Control */ |
| 132 | #if !defined (LIBUSB_NO_TIMEOUT_DEVICE) |
| 133 | No newline at end of file |