From f13728c6e156f79f3b9286bcca9f3dcb4a22a285 Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Sun, 5 Feb 2017 16:20:58 +0100 Subject: [PATCH] Set the MaxIsoPacket field in the endpoint info if the endpoint is an isochronous endpoint. Use MaxIsoPacket as the iso packet size when preparing the iso transfer. --- usb/config.go | 19 +++++++++++++------ usb/iso.go | 8 +++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/usb/config.go b/usb/config.go index 9571174..62177e0 100644 --- a/usb/config.go +++ b/usb/config.go @@ -126,15 +126,22 @@ func newConfig(dev *C.libusb_device, cfg *C.struct_libusb_config_descriptor) Con } i.Endpoints = make([]EndpointInfo, 0, len(ends)) for _, end := range ends { - i.Endpoints = append(i.Endpoints, EndpointInfo{ + ei := EndpointInfo{ Address: uint8(end.bEndpointAddress), Attributes: uint8(end.bmAttributes), MaxPacketSize: uint16(end.wMaxPacketSize), - //MaxIsoPacket: uint32(C.libusb_get_max_iso_packet_size(dev, C.uchar(end.bEndpointAddress))), - PollInterval: uint8(end.bInterval), - RefreshRate: uint8(end.bRefresh), - SynchAddress: uint8(end.bSynchAddress), - }) + PollInterval: uint8(end.bInterval), + RefreshRate: uint8(end.bRefresh), + SynchAddress: uint8(end.bSynchAddress), + } + if TransferType(ei.Attributes)&TRANSFER_TYPE_MASK == TRANSFER_TYPE_ISOCHRONOUS { + // bits 0-10 identify the packet size, bits 11-12 are the number of additional transactions per microframe. + // Don't use libusb_get_max_iso_packet_size, as it has a bug where it returns the same value + // regardless of alternative setting used, where different alternative settings might define different + // max packet sizes. + ei.MaxIsoPacket = uint32(end.wMaxPacketSize) & 0x07ff * (uint32(end.wMaxPacketSize)>>11&3 + 1) + } + i.Endpoints = append(i.Endpoints, ei) } descs = append(descs, i) } diff --git a/usb/iso.go b/usb/iso.go index 3be4152..1044de3 100644 --- a/usb/iso.go +++ b/usb/iso.go @@ -37,10 +37,8 @@ func iso_callback(cptr unsafe.Pointer) { } func (end *endpoint) allocTransfer() *Transfer { - // Use libusb_get_max_iso_packet_size ? const ( - iso_packets = 8 // 128 // 242 - packet_size = 2 * 960 // 1760 + iso_packets = 8 // 128 // 242 ) xfer := C.libusb_alloc_transfer(C.int(iso_packets)) @@ -49,7 +47,7 @@ func (end *endpoint) allocTransfer() *Transfer { return nil } - buf := make([]byte, iso_packets*packet_size) + buf := make([]byte, iso_packets*end.EndpointInfo.MaxIsoPacket) done := make(chan struct{}, 1) xfer.dev_handle = end.Device.handle @@ -60,7 +58,7 @@ func (end *endpoint) allocTransfer() *Transfer { xfer.length = C.int(len(buf)) xfer.num_iso_packets = iso_packets - C.libusb_set_iso_packet_lengths(xfer, packet_size) + C.libusb_set_iso_packet_lengths(xfer, C.uint(end.EndpointInfo.MaxIsoPacket)) /* pkts := *(*[]C.struct_libusb_packet_descriptor)(unsafe.Pointer(&reflect.SliceHeader{ Data: uintptr(unsafe.Pointer(&xfer.iso_packet_desc)),