From ee93bd93e87fee016b708e23a9a9e85a543f862a Mon Sep 17 00:00:00 2001 From: Niels Sascha Reedijk Date: Sun, 8 Oct 2006 20:12:53 +0000 Subject: [PATCH] Implement lazy pipe creation. The Default Pipes for each speed will be created as needed, instead off at the construction of the busmanager. This is due to the fact that the creation in the constructor would callback methods of the busmanager class which might cause improper behaviour with uninitialized variables. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19032 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../kernel/bus_managers/usb/BusManager.cpp | 37 ++++++++++++++----- src/add-ons/kernel/bus_managers/usb/usb_p.h | 4 +- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/add-ons/kernel/bus_managers/usb/BusManager.cpp b/src/add-ons/kernel/bus_managers/usb/BusManager.cpp index 72a73dae70..69680bb095 100644 --- a/src/add-ons/kernel/bus_managers/usb/BusManager.cpp +++ b/src/add-ons/kernel/bus_managers/usb/BusManager.cpp @@ -27,15 +27,9 @@ BusManager::BusManager(Stack *stack) for (int32 i = 0; i < 128; i++) fDeviceMap[i] = false; - // Set up the default pipes - for (int32 i = 0; i <= USB_SPEED_MAX; i++) { - fDefaultPipes[i] = new(std::nothrow) ControlPipe(fRootObject, 0, 0, - (usb_speed)i, 8); - if (!fDefaultPipes[i]) { - TRACE_ERROR(("usb BusManager: failed to allocate default pipes\n")); - return; - } - } + // Set the default pipes to NULL (these will be created when needed) + for (int32 i = 0; i <= USB_SPEED_MAX; i++) + fDefaultPipes[i] = 0; fInitOK = true; } @@ -45,6 +39,9 @@ BusManager::~BusManager() { Lock(); benaphore_destroy(&fLock); + for (int32 i = 0; i <= USB_SPEED_MAX; i++) + if (fDefaultPipes[i] != 0) + delete fDefaultPipes[i]; } @@ -103,7 +100,12 @@ BusManager::AllocateNewDevice(Hub *parent, usb_speed speed) } TRACE(("usb BusManager::AllocateNewDevice(): setting device address to %d\n", deviceAddress)); - ControlPipe *defaultPipe = fDefaultPipes[speed]; + ControlPipe *defaultPipe = GetDefaultPipe(speed); + + if (!defaultPipe) { + TRACE(("usb BusManager::AllocateNewDevice(): Error getting the default pipe for speed %d\n", (int)speed)); + return NULL; + } status_t result = B_ERROR; for (int32 i = 0; i < 15; i++) { @@ -232,3 +234,18 @@ BusManager::NotifyPipeChange(Pipe *pipe, usb_change change) // virtual function to be overridden return B_ERROR; } + +ControlPipe * +BusManager::GetDefaultPipe(usb_speed speed) +{ + if (fDefaultPipes[(int)speed] == 0) { + fDefaultPipes[(int)speed] = new(std::nothrow) ControlPipe(fRootObject, + 0, 0, (usb_speed)speed, 8); + if (!fDefaultPipes[(int)speed]) { + TRACE_ERROR(("usb BusManager: failed to allocate default pipe\n")); + return 0; + } + } + + return fDefaultPipes[(int)speed]; +} diff --git a/src/add-ons/kernel/bus_managers/usb/usb_p.h b/src/add-ons/kernel/bus_managers/usb/usb_p.h index 448ab1f9f9..1eae84ecdf 100644 --- a/src/add-ons/kernel/bus_managers/usb/usb_p.h +++ b/src/add-ons/kernel/bus_managers/usb/usb_p.h @@ -15,7 +15,7 @@ #include "BeOSCompatibility.h" -//#define TRACE_USB +#define TRACE_USB #ifdef TRACE_USB #define TRACE(x) dprintf x #define TRACE_ERROR(x) dprintf x @@ -182,6 +182,8 @@ protected: bool fInitOK; private: + ControlPipe *GetDefaultPipe(usb_speed); + benaphore fLock; bool fDeviceMap[128]; ControlPipe *fDefaultPipes[USB_SPEED_MAX + 1];