Skip to content

Commit 3ee9794

Browse files
committed
add fnusb_dev.dev
Signed-off-by: Benn Snyder <[email protected]>
1 parent b85947e commit 3ee9794

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/usb_libusb10.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
408418
FN_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;

src/usb_libusb10.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ typedef struct {
5656

5757
typedef struct {
5858
freenect_device *parent; //so we can go up from the libusb userdata
59+
libusb_device *dev;
5960
libusb_device_handle *dev_handle;
60-
int device_dead; // set to 1 when the underlying libusb_device_handle vanishes (ie, Kinect was unplugged)
61+
short device_dead; // set to 1 when the underlying libusb_device_handle vanishes (ie, Kinect was unplugged)
6162
int VID;
6263
int PID;
6364
} fnusb_dev;

0 commit comments

Comments
 (0)