@@ -402,9 +402,19 @@ FN_INTERNAL void fnusb_reset_subdevice(fnusb_dev* dev, freenect_device* parent)
402
402
dev -> dev_handle = NULL ;
403
403
}
404
404
405
+ if (dev -> dev ) {
406
+ libusb_unref_device (dev -> dev );
407
+ dev -> dev = NULL ;
408
+ }
409
+
405
410
dev -> parent = parent ;
406
411
}
407
412
413
+ FN_INTERNAL libusb_device * fnusb_try_ref_device (libusb_device * dev )
414
+ {
415
+ return dev ? libusb_ref_device (dev ) : NULL ;
416
+ }
417
+
408
418
FN_INTERNAL int fnusb_open_subdevices (freenect_device * dev , int index )
409
419
{
410
420
freenect_context * ctx = dev -> parent ;
@@ -441,7 +451,9 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
441
451
continue ;
442
452
}
443
453
444
- camera = devs [i ]; // found the requested camera
454
+ camera = devs [i ];
455
+ dev -> usb_cam .dev = fnusb_try_ref_device (camera );
456
+
445
457
if (ctx -> enabled_subdevices & FREENECT_DEVICE_CAMERA )
446
458
{
447
459
res = libusb_open (camera , & dev -> usb_cam .dev_handle );
@@ -492,10 +504,13 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
492
504
goto failure ;
493
505
}
494
506
507
+
495
508
// 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
+
496
512
if (ctx -> enabled_subdevices & FREENECT_DEVICE_MOTOR )
497
513
{
498
- libusb_device * motor = fnusb_find_sibling_device (ctx , camera , devs , count , & fnusb_is_motor );
499
514
if (motor == NULL ) {
500
515
FN_ERROR ("Could not find device sibling\n" );
501
516
res = -1 ;
@@ -529,10 +544,13 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
529
544
dev -> usb_motor .PID = desc .idProduct ;
530
545
}
531
546
547
+
532
548
// 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
+
533
552
if (ctx -> enabled_subdevices & FREENECT_DEVICE_AUDIO )
534
553
{
535
- libusb_device * audio = fnusb_find_sibling_device (ctx , camera , devs , count , & fnusb_is_audio );
536
554
if (audio == NULL ) {
537
555
FN_ERROR ("Could not find device sibling\n" );
538
556
res = -1 ;
0 commit comments