@@ -402,9 +402,19 @@ FN_INTERNAL void fnusb_reset_subdevice(fnusb_dev* dev, freenect_device* parent)
402402 dev -> dev_handle = NULL ;
403403 }
404404
405+ if (dev -> dev ) {
406+ libusb_unref_device (dev -> dev );
407+ dev -> dev = NULL ;
408+ }
409+
405410 dev -> parent = parent ;
406411}
407412
413+ FN_INTERNAL libusb_device * fnusb_try_ref_device (libusb_device * dev )
414+ {
415+ return dev ? libusb_ref_device (dev ) : NULL ;
416+ }
417+
408418FN_INTERNAL int fnusb_open_subdevices (freenect_device * dev , int index )
409419{
410420 freenect_context * ctx = dev -> parent ;
@@ -441,7 +451,9 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
441451 continue ;
442452 }
443453
444- camera = devs [i ]; // found the requested camera
454+ camera = devs [i ];
455+ dev -> usb_cam .dev = fnusb_try_ref_device (camera );
456+
445457 if (ctx -> enabled_subdevices & FREENECT_DEVICE_CAMERA )
446458 {
447459 res = libusb_open (camera , & dev -> usb_cam .dev_handle );
@@ -492,10 +504,13 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
492504 goto failure ;
493505 }
494506
507+
495508 // FIND MOTOR
509+ libusb_device * motor = fnusb_find_sibling_device (ctx , camera , devs , count , & fnusb_is_motor );
510+ dev -> usb_motor .dev = fnusb_try_ref_device (motor );
511+
496512 if (ctx -> enabled_subdevices & FREENECT_DEVICE_MOTOR )
497513 {
498- libusb_device * motor = fnusb_find_sibling_device (ctx , camera , devs , count , & fnusb_is_motor );
499514 if (motor == NULL ) {
500515 FN_ERROR ("Could not find device sibling\n" );
501516 res = -1 ;
@@ -529,10 +544,13 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
529544 dev -> usb_motor .PID = desc .idProduct ;
530545 }
531546
547+
532548 // FIND AUDIO
549+ libusb_device * audio = fnusb_find_sibling_device (ctx , camera , devs , count , & fnusb_is_audio );
550+ dev -> usb_audio .dev = fnusb_try_ref_device (audio );
551+
533552 if (ctx -> enabled_subdevices & FREENECT_DEVICE_AUDIO )
534553 {
535- libusb_device * audio = fnusb_find_sibling_device (ctx , camera , devs , count , & fnusb_is_audio );
536554 if (audio == NULL ) {
537555 FN_ERROR ("Could not find device sibling\n" );
538556 res = -1 ;
0 commit comments