Skip to content

Commit e450a23

Browse files
committed
whb04b: Proper close and reconnect
1 parent 27c6952 commit e450a23

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/hal/user_comps/xhc-whb04b-6/usb.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ Usb::InitStatus Usb::init()
598598
if (r != 0)
599599
{
600600
std::cerr << endl << "failed to initialize usb context" << endl;
601+
mDoReconnect=true;
601602
return InitStatus::RETRY;
602603
}
603604
*verboseInitOut << " ok" << endl;
@@ -632,6 +633,7 @@ Usb::InitStatus Usb::init()
632633
if (devicesCount < 0)
633634
{
634635
std::cerr << endl << "failed to get device list" << endl;
636+
mDoReconnect=true;
635637
return InitStatus::RETRY;
636638
}
637639

@@ -665,6 +667,9 @@ Usb::InitStatus Usb::init()
665667
r = libusb_detach_kernel_driver(deviceHandle, 0);
666668
if(r < 0){
667669
std::cerr << "libusb_detach_kernel_driver failed with " << r << endl;
670+
libusb_close(deviceHandle);
671+
deviceHandle = nullptr;
672+
mDoReconnect=true;
668673
return InitStatus::RETRY;
669674
}
670675
*verboseInitOut << " ok" << endl;
@@ -676,19 +681,26 @@ Usb::InitStatus Usb::init()
676681
else
677682
{
678683
std::cerr << "libusb_kernel_driver_active failed with " << r << endl;
684+
libusb_close(deviceHandle);
685+
deviceHandle = nullptr;
686+
mDoReconnect=true;
679687
return InitStatus::RETRY;
680688
}
681689
*verboseInitOut << "init claiming interface ...";
682690
r = libusb_claim_interface(deviceHandle, 0);
683691
if (r != 0)
684692
{
685693
std::cerr << endl << "failed to claim interface" << endl;
694+
libusb_close(deviceHandle);
695+
deviceHandle = nullptr;
696+
mDoReconnect=true;
686697
return InitStatus::RETRY;
687698
}
688699
*verboseInitOut << " ok" << endl;
689700
}
690701
return InitStatus::OK;
691702
}
703+
// ----------------------------------------------------------------------
692704
void Usb::handleTimeouts()
693705
{
694706
struct timeval timeout;
@@ -699,6 +711,7 @@ void Usb::handleTimeouts()
699711
assert((r == LIBUSB_SUCCESS) || (r == LIBUSB_ERROR_NO_DEVICE) || (r == LIBUSB_ERROR_BUSY) ||
700712
(r == LIBUSB_ERROR_TIMEOUT) || (r == LIBUSB_ERROR_INTERRUPTED));
701713
}
714+
// ----------------------------------------------------------------------
702715
void Usb::close()
703716
{
704717
struct timeval tv;
@@ -711,6 +724,7 @@ void Usb::close()
711724
libusb_close(deviceHandle);
712725
deviceHandle = nullptr;
713726
}
727+
// ----------------------------------------------------------------------
714728
void Usb::teardown()
715729
{
716730
libusb_exit(context);

src/hal/user_comps/xhc-whb04b-6/xhc-whb04b6.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,10 @@ int XhcWhb04b6Component::run()
460460
process();
461461
mUsb.teardown();
462462
}
463+
else
464+
{
465+
mUsb.teardown();
466+
}
463467
}
464468
teardownHal();
465469

0 commit comments

Comments
 (0)