Skip to content

Commit bb19703

Browse files
committed
Merge pull request #30 from christiankerl/fix_max_iso_packet_size
reimplement custom version of libusb_get_max_iso_packet_size
2 parents 2169d01 + 16bb849 commit bb19703

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

depends/install_deps.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ cd `dirname $0`
33
DEPENDS_DIR=`pwd`
44

55
# libusbx with superspeed patch
6-
LIBUSBX_SOURCE_DIR=$DEPENDS_DIR/libusbx_src
7-
LIBUSBX_INSTALL_DIR=$DEPENDS_DIR/libusbx
6+
LIBUSB_SOURCE_DIR=$DEPENDS_DIR/libusb_src
7+
LIBUSB_INSTALL_DIR=$DEPENDS_DIR/libusb
88

9-
rm -rf $LIBUSBX_SOURCE_DIR $LIBUSBX_INSTALL_DIR
9+
rm -rf $LIBUSB_SOURCE_DIR $LIBUSB_INSTALL_DIR
1010

11-
git clone -b superspeed https://github.com/JoshBlake/libusbx.git $LIBUSBX_SOURCE_DIR
11+
git clone https://github.com/libusb/libusb.git $LIBUSB_SOURCE_DIR
1212

13-
cd $LIBUSBX_SOURCE_DIR
13+
cd $LIBUSB_SOURCE_DIR
14+
git checkout v1.0.19
1415
git apply $DEPENDS_DIR/linux_usbfs_increase_max_iso_buffer_length.patch
1516
./bootstrap.sh
16-
./configure --prefix=$LIBUSBX_INSTALL_DIR
17+
./configure --prefix=$LIBUSB_INSTALL_DIR
1718
make && make install
1819

1920
cd $DEPENDS_DIR

examples/protonect/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ FIND_PACKAGE(OpenCV REQUIRED)
2727
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIR})
2828

2929
# LibUSB
30-
INCLUDE_DIRECTORIES("${MY_DIR}/../../depends/libusbx/include/libusb-1.0/")
31-
LINK_DIRECTORIES("${MY_DIR}/../../depends/libusbx/lib/")
30+
INCLUDE_DIRECTORIES("${MY_DIR}/../../depends/libusb/include/libusb-1.0/")
31+
LINK_DIRECTORIES("${MY_DIR}/../../depends/libusb/lib/")
3232

3333
# GLEW
3434
INCLUDE_DIRECTORIES("${MY_DIR}/../../depends/glew/include/")

examples/protonect/include/libfreenect2/protocol/usb_control.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class UsbControl
7979
Error
8080
};
8181

82+
ResultCode getIrMaxIsoPacketSize(int &size);
83+
8284
ResultCode setConfiguration();
8385
ResultCode claimInterfaces();
8486
ResultCode releaseInterfaces();

examples/protonect/src/libfreenect2.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,8 @@ bool Freenect2DeviceImpl::open()
422422
if(usb_control_.enablePowerStates() != UsbControl::Success) return false;
423423
if(usb_control_.setVideoTransferFunctionState(UsbControl::Disabled) != UsbControl::Success) return false;
424424

425-
size_t max_iso_packet_size = libusb_get_max_iso_packet_size(usb_device_, 0x84);
425+
int max_iso_packet_size;
426+
if(usb_control_.getIrMaxIsoPacketSize(max_iso_packet_size) != UsbControl::Success) return false;
426427

427428
if(max_iso_packet_size < 0x8400)
428429
{

examples/protonect/src/usb_control.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,54 @@ namespace libusb_ext
127127

128128
return libusb_control_transfer(handle, bmRequestType, bRequest, wValue, wIndex, data, wLength, timeout);
129129
}
130+
131+
int get_max_iso_packet_size(libusb_device *device, int configuration, int alternate_setting, int endpoint)
132+
{
133+
libusb_config_descriptor *config_desc;
134+
int r = LIBUSB_ERROR_NOT_FOUND;
135+
136+
r = libusb_get_config_descriptor_by_value(device, configuration, &config_desc);
137+
138+
if(r == LIBUSB_SUCCESS)
139+
{
140+
for(int interface_idx = 0; interface_idx < config_desc->bNumInterfaces; ++interface_idx)
141+
{
142+
const libusb_interface &interface = config_desc->interface[interface_idx];
143+
144+
if(interface.num_altsetting > alternate_setting)
145+
{
146+
const libusb_interface_descriptor &interface_desc = interface.altsetting[alternate_setting];
147+
const libusb_endpoint_descriptor *endpoint_desc = 0;
148+
149+
for(int endpoint_idx = 0; endpoint_idx < interface_desc.bNumEndpoints; ++endpoint_idx)
150+
{
151+
if(interface_desc.endpoint[endpoint_idx].bEndpointAddress == endpoint && (interface_desc.endpoint[endpoint_idx].bmAttributes & 0x3) == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS)
152+
{
153+
endpoint_desc = interface_desc.endpoint + endpoint_idx;
154+
break;
155+
}
156+
}
157+
158+
if(endpoint_desc != 0)
159+
{
160+
libusb_ss_endpoint_companion_descriptor *companion_desc;
161+
// ctx is only used for error reporting, libusb should better ask for a libusb_device anyway...
162+
r = libusb_get_ss_endpoint_companion_descriptor(NULL /* ctx */, endpoint_desc, &companion_desc);
163+
164+
if(r != LIBUSB_SUCCESS) continue;
165+
166+
r = companion_desc->wBytesPerInterval;
167+
168+
libusb_free_ss_endpoint_companion_descriptor(companion_desc);
169+
break;
170+
}
171+
}
172+
}
173+
}
174+
libusb_free_config_descriptor(config_desc);
175+
176+
return r;
177+
}
130178
}
131179

132180
UsbControl::UsbControl(libusb_device_handle *handle) :
@@ -255,5 +303,21 @@ UsbControl::ResultCode UsbControl::setIrInterfaceState(UsbControl::State state)
255303
return checkLibusbResult("setIrInterfaceState", r);
256304
}
257305

306+
307+
UsbControl::ResultCode UsbControl::getIrMaxIsoPacketSize(int &size)
308+
{
309+
size = 0;
310+
libusb_device *dev = libusb_get_device(handle_);
311+
int r = libusb_ext::get_max_iso_packet_size(dev, 1, 1, 0x84);
312+
313+
if(r > LIBUSB_SUCCESS)
314+
{
315+
size = r;
316+
r = LIBUSB_SUCCESS;
317+
}
318+
319+
return checkLibusbResult("getIrMaxIsoPacketSize", r);
320+
}
321+
258322
} /* namespace protocol */
259323
} /* namespace libfreenect2 */

0 commit comments

Comments
 (0)