@@ -318,8 +318,8 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
318318 if (cnt < 0 )
319319 return -1 ;
320320
321- int i = 0 , nr_cam = 0 , nr_mot = 0 ;
322- int nr_audio = 0 ;
321+ int i = 0 , nr_cam = 0 ;
322+
323323 int res ;
324324 struct libusb_device_descriptor desc ;
325325
@@ -424,19 +424,32 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
424424 if (ctx -> enabled_subdevices == FREENECT_DEVICE_CAMERA || res < 0 )
425425 cnt = 0 ;
426426
427- // Search for the motor
428- for (i = 0 ; i < cnt ; i ++ )
429- {
427+ //FIND MOTOR BASED ON PARENT HUB
428+ if ( (ctx -> enabled_subdevices & FREENECT_DEVICE_MOTOR ) && dev -> usb_cam .dev != NULL )
429+ {
430+
431+ libusb_device * camera = libusb_get_device ( dev -> usb_cam .dev );
432+ libusb_device * cameraParent = libusb_get_parent ( camera );
433+
434+ if ( cameraParent != NULL )
435+ {
436+
437+ for (i = 0 ; i < cnt ; i ++ )
438+ {
439+
440+ // Match audio based on camera parent
441+ if ( cameraParent == libusb_get_parent (devs [i ]) )
442+ {
443+
430444 int r = libusb_get_device_descriptor (devs [i ], & desc );
431445 if (r < 0 )
432446 continue ;
433447
434448 if (desc .idVendor != VID_MICROSOFT )
435449 continue ;
436- if ((ctx -> enabled_subdevices & FREENECT_DEVICE_MOTOR ) && !dev -> usb_motor .dev && desc .idProduct == PID_NUI_MOTOR )
437- {
438- // If the index given by the user matches our camera index
439- if (nr_mot == index )
450+
451+ // This has to be the device we are looking for as it is a Kinect motor device with the same parent as the camera
452+ if ( !dev -> usb_motor .dev && desc .idProduct == PID_NUI_MOTOR )
440453 {
441454 dev -> usb_motor .VID = desc .idVendor ;
442455 dev -> usb_motor .PID = desc .idProduct ;
@@ -456,18 +469,39 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
456469 dev -> usb_motor .dev = NULL ;
457470 break ;
458471 }
459- }
460- else
461- {
462- nr_mot ++ ;
463- }
464- }
465-
466- // Search for the audio
467- if ((ctx -> enabled_subdevices & FREENECT_DEVICE_AUDIO ) && !dev -> usb_audio .dev && (desc .idProduct == PID_NUI_AUDIO || fnusb_is_pid_k4w_audio (desc .idProduct )))
468- {
469- // If the index given by the user matches our audio index
470- if (nr_audio == index )
472+
473+ // This has to be the device we need, as it is matched by parent - so don't try any others
474+ break ;
475+ }
476+ }
477+ }
478+ }
479+ }
480+
481+ // FIND AUDIO BASED ON PARENT HUB
482+ if ( (ctx -> enabled_subdevices & FREENECT_DEVICE_AUDIO ) && dev -> usb_cam .dev != NULL )
483+ {
484+
485+ libusb_device * camera = libusb_get_device ( dev -> usb_cam .dev );
486+ libusb_device * cameraParent = libusb_get_parent ( camera );
487+
488+ if ( cameraParent != NULL )
489+ {
490+
491+ for (i = 0 ; i < cnt ; i ++ )
492+ {
493+ // Match audio based on camera parent
494+ if ( cameraParent == libusb_get_parent (devs [i ]) )
495+ {
496+ int r = libusb_get_device_descriptor (devs [i ], & desc );
497+ if (r < 0 )
498+ continue ;
499+
500+ if (desc .idVendor != VID_MICROSOFT )
501+ continue ;
502+
503+ // This has to be the device we are looking for as it is a Kinect audio device with the same parent as the camera
504+ if ( !dev -> usb_audio .dev && (desc .idProduct == PID_NUI_AUDIO || fnusb_is_pid_k4w_audio (desc .idProduct )))
471505 {
472506 dev -> usb_audio .VID = desc .idVendor ;
473507 dev -> usb_audio .PID = desc .idProduct ;
@@ -625,10 +659,11 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
625659 }
626660 free (audio_serial );
627661 }
628- }
629- else
630- {
631- nr_audio ++ ;
662+
663+ // This has to be the device we need, as it is matched by parent - so don't try any others
664+ break ;
665+ }
666+ }
632667 }
633668 }
634669 }
0 commit comments