Index: trunk/src/wnaspi32/aspilib.cpp
===================================================================
--- trunk/src/wnaspi32/aspilib.cpp	(revision 4264)
+++ trunk/src/wnaspi32/aspilib.cpp	(revision 4265)
@@ -1,3 +1,3 @@
-/* $Id: aspilib.cpp,v 1.4 2000-09-14 19:09:16 sandervl Exp $ */
+/* $Id: aspilib.cpp,v 1.5 2000-09-15 13:25:46 sandervl Exp $ */
 /*
  *  ASPI Router Library
@@ -21,5 +21,7 @@
 #include     <stdio.h>
 #include     <misc.h>
-#include "aspilib.h"
+#include     "aspilib.h"
+
+#define WNASPI32_MUTEX_NAME "\\SEM32\\ODIN\\ASPIROUT"
 
 //***************************************************************************
@@ -30,5 +32,5 @@
 //*                                                                         *
 //***************************************************************************
-scsiObj::scsiObj() : buffer(NULL)
+scsiObj::scsiObj() : buffer(NULL), hmtxDriver(0)
 {
    memset(&SRBlock, 0, sizeof(SRBlock));
@@ -202,17 +204,28 @@
 BOOL scsiObj::init(ULONG bufsize)
 {
-  BOOL  success;
-  ULONG rc;
-
-  rc = DosAllocMem(&buffer, bufsize, OBJ_TILE | PAG_READ | PAG_WRITE | PAG_COMMIT);
-  if (rc) return FALSE;
-  success=openDriver();                         // call openDriver member function
-  if (!success) return FALSE;
-  success=initSemaphore();                      // call initSemaphore member function
-  if (!success) return FALSE;
-
-  success=initBuffer();
-
-  return TRUE;
+  BOOL   success;
+  APIRET rc;
+
+
+   rc = DosCreateMutexSem(WNASPI32_MUTEX_NAME, (HMTX*)&hmtxDriver,
+                          0, TRUE);
+   if(rc == ERROR_DUPLICATE_NAME) {
+	rc = DosOpenMutexSem(WNASPI32_MUTEX_NAME, (HMTX*)&hmtxDriver);
+   }
+   if(rc != NO_ERROR) {
+	dprintf(("scsiObj::init: DosCreate/OpenMutexSem failed! (rc=%d)", rc));
+        return FALSE;
+   }
+
+   rc = DosAllocMem(&buffer, bufsize, OBJ_TILE | PAG_READ | PAG_WRITE | PAG_COMMIT);
+   if (rc) return FALSE;
+   success=openDriver();                         // call openDriver member function
+   if (!success) return FALSE;
+   success=initSemaphore();                      // call initSemaphore member function
+   if (!success) return FALSE;
+
+   success=initBuffer();
+ 
+   return TRUE;
 }
 
@@ -239,5 +252,5 @@
   if (!success)
   {
-    printf("closeSemaphore() unsuccessful.\n");
+    dprintf(("scsiObj::close: closeSemaphore() unsuccessful."));
     return FALSE;
   }
@@ -245,13 +258,21 @@
   if (!success)
   {
+    dprintf(("scsiObj::close: closeDriver() unsucessful."));
     return FALSE;
-    printf("closeDriver() unsucessful.\n");
   }
   rc = DosFreeMem(buffer);
   if (rc)
   {
-    printf("DosFreeMem unsuccessful. return code: %ld\n", rc);
+    dprintf(("scsiObj::close: DosFreeMem unsuccessful. return code: %ld", rc));
     return FALSE;
   }
+  if(hmtxDriver) {
+  	rc = DosCloseMutexSem((HMTX)hmtxDriver);
+  	if(rc != NO_ERROR) {
+    		dprintf(("scsiObj::close: DosCloseMutexSem unsuccessful. return code: %ld", rc));
+    		return FALSE;
+  	}
+  }
+
   return TRUE;
 }
@@ -451,44 +472,33 @@
 //***************************************************************************
 //***************************************************************************
-BOOL fGainDrvAccess( BOOL fWait,
-                     ULONG *phSem)
-{
-  ULONG rc;
-  rc = DosCreateMutexSem ( "\\SEM32\\ODIN\\ASPIROUT",
-                           (HMTX*)phSem,
-                           0,
-                           TRUE);
-  if(NO_ERROR==rc)
-    return TRUE;
-  if((ERROR_DUPLICATE_NAME!=rc)||(!fWait))
-    return FALSE;
-  rc = DosOpenMutexSem ( "\\SEM32\\ODIN\\ASPIROUT",
-                         (HMTX*)phSem);
-  if(NO_ERROR!=rc)
-    return FALSE;
-
-  rc = DosRequestMutexSem( (HMTX)(*phSem),
-                           SEM_INDEFINITE_WAIT);
-
-  if(NO_ERROR!=rc)
-    return FALSE;
-
-  return TRUE;
-}
-//***************************************************************************
-//***************************************************************************
-BOOL fReleaseDrvAccess(ULONG hSem)
-{
-  ULONG rc;
-  BOOL frc = TRUE;
-
-  rc = DosReleaseMutexSem((HMTX)hSem);
-  if(NO_ERROR!=rc)
-    frc=FALSE;
-
-  rc = DosCloseMutexSem((HMTX)hSem);
-  if(NO_ERROR!=rc)
-    frc=FALSE;
-
-  return frc;
-}
+BOOL scsiObj::access(BOOL fWait)
+{
+ APIRET rc;
+
+  if(!fWait) {
+  	rc = DosRequestMutexSem((HMTX) hmtxDriver, SEM_INDEFINITE_WAIT);
+  }
+  else 	rc = DosRequestMutexSem((HMTX) hmtxDriver, 100);
+
+  if(rc != NO_ERROR) {
+	dprintf(("scsiObj::access: DosRequestMutexSem failed with rc = %d", rc));
+    	return FALSE;
+  }
+  return TRUE;
+}
+//***************************************************************************
+//***************************************************************************
+BOOL scsiObj::release()
+{
+  APIRET rc;
+
+  rc = DosReleaseMutexSem((HMTX)hmtxDriver);
+  if(rc != NO_ERROR) {
+	dprintf(("scsiObj::access: DosReleaseMutexSem failed with rc = %d", rc));
+    	return FALSE;
+  }
+
+  return TRUE;
+}
+//***************************************************************************
+//***************************************************************************
Index: trunk/src/wnaspi32/aspilib.h
===================================================================
--- trunk/src/wnaspi32/aspilib.h	(revision 4264)
+++ trunk/src/wnaspi32/aspilib.h	(revision 4265)
@@ -28,4 +28,5 @@
     BOOL        closeDriver();
     BOOL        initBuffer();
+    ULONG       hmtxDriver;
 
   public:
@@ -50,4 +51,7 @@
     ULONG       SendSRBlock(VOID);
     ULONG       getNumHosts();
+    BOOL        access(BOOL fWait);
+    BOOL        release();
+
 
     SRBOS2      SRBlock;                // SCSI Request Block
@@ -56,11 +60,5 @@
 };
 
-
-extern "C"
-{
-  BOOL fGainDrvAccess( BOOL fWait, ULONG *phSem);
-  BOOL fReleaseDrvAccess(ULONG hSem);
-}
-
+extern scsiObj *aspi;
 
 #endif // _ASPILIB_H_
Index: trunk/src/wnaspi32/initterm.cpp
===================================================================
--- trunk/src/wnaspi32/initterm.cpp	(revision 4265)
+++ trunk/src/wnaspi32/initterm.cpp	(revision 4265)
@@ -0,0 +1,133 @@
+/* $Id: initterm.cpp,v 1.4 2000-09-15 13:25:48 sandervl Exp $ */
+
+/*
+ * DLL entry point
+ *
+ * Copyright 1998 Sander van Leeuwen
+ * Copyright 1998 Peter Fitzsimmons
+ *
+ *
+ * Project Odin Software License can be found in LICENSE.TXT
+ *
+ */
+
+/*-------------------------------------------------------------*/
+/* INITERM.C -- Source for a custom dynamic link library       */
+/*              initialization and termination (_DLL_InitTerm) */
+/*              function.                                      */
+/*                                                             */
+/* When called to perform initialization, this sample function */
+/* gets storage for an array of integers, and initializes its  */
+/* elements with random integers.  At termination time, it     */
+/* frees the array.  Substitute your own special processing.   */
+/*-------------------------------------------------------------*/
+
+
+/* Include files */
+#define  INCL_DOSMODULEMGR
+#define  INCL_DOSPROCESS
+#include <os2wrap.h>    //Odin32 OS/2 api wrappers
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <odin.h>
+#include <win32type.h>
+#include <winconst.h>
+#include <odinlx.h>
+#include <misc.h>       /*PLF Wed  98-03-18 23:18:15*/
+#include "aspilib.h"
+
+extern "C" {
+void CDECL _ctordtorInit( void );
+void CDECL _ctordtorTerm( void );
+
+ //Win32 resource table (produced by wrc)
+ extern DWORD _Resource_PEResTab;
+}
+
+static HMODULE dllHandle = 0;
+scsiObj *aspi = NULL;
+//******************************************************************************
+//******************************************************************************
+BOOL WINAPI LibMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
+{
+   switch (fdwReason)
+   {
+   case DLL_PROCESS_ATTACH:
+        aspi = new scsiObj();
+	if(aspi == NULL) {
+		dprintf(("WNASPI32: LibMain; can't allocate aspi object!"));
+		return FALSE;
+	}
+    	if(aspi->init(65535) == FALSE)
+        {
+		delete aspi;
+		aspi = NULL;
+		dprintf(("WNASPI32: LibMain; can't init aspi object!"));
+		return FALSE;
+	}
+	dprintf(("WNASPI32: LibMain; aspi object created successfully"));
+	return TRUE;
+
+   case DLL_THREAD_ATTACH:
+   case DLL_THREAD_DETACH:
+	return TRUE;
+
+   case DLL_PROCESS_DETACH:
+	if(aspi) {
+        	aspi->close();
+		delete aspi;
+		aspi = NULL;
+	}
+	_ctordtorTerm();
+	return TRUE;
+   }
+   return FALSE;
+}
+/****************************************************************************/
+/* _DLL_InitTerm is the function that gets called by the operating system   */
+/* loader when it loads and frees this DLL for each process that accesses   */
+/* this DLL.  However, it only gets called the first time the DLL is loaded */
+/* and the last time it is freed for a particular process.  The system      */
+/* linkage convention MUST be used because the operating system loader is   */
+/* calling this function.                                                   */
+/****************************************************************************/
+unsigned long SYSTEM _DLL_InitTerm(unsigned long hModule, unsigned long
+                                   ulFlag)
+{
+   size_t i;
+   APIRET rc;
+
+   /*-------------------------------------------------------------------------*/
+   /* If ulFlag is zero then the DLL is being loaded so initialization should */
+   /* be performed.  If ulFlag is 1 then the DLL is being freed so            */
+   /* termination should be performed.                                        */
+   /*-------------------------------------------------------------------------*/
+
+   switch (ulFlag) {
+      case 0:
+         _ctordtorInit();
+
+         CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed  98-03-18 05:28:48*/
+
+	 dllHandle = RegisterLxDll(hModule, LibMain, (PVOID)&_Resource_PEResTab);
+         if(dllHandle == 0) 
+		return 0UL;
+
+         break;
+      case 1:
+         if(dllHandle) {
+	 	UnregisterLxDll(dllHandle);
+         }
+         break;
+      default:
+         return 0UL;
+   }
+
+   /***********************************************************/
+   /* A non-zero value must be returned to indicate success.  */
+   /***********************************************************/
+   return 1UL;
+}
+//******************************************************************************
+//******************************************************************************
Index: trunk/src/wnaspi32/makefile
===================================================================
--- trunk/src/wnaspi32/makefile	(revision 4264)
+++ trunk/src/wnaspi32/makefile	(revision 4265)
@@ -1,3 +1,3 @@
-# $Id: makefile,v 1.10 2000-07-19 19:05:27 sandervl Exp $
+# $Id: makefile,v 1.11 2000-09-15 13:25:49 sandervl Exp $
 
 #
@@ -27,5 +27,5 @@
 $(OBJDIR)\winaspi32.obj \
 $(OBJDIR)\wnaspi32rsrc.obj \
-$(PDWIN32_LIB)\dllentry.obj
+$(OBJDIR)\initterm.obj
 
 
Index: trunk/src/wnaspi32/winaspi32.cpp
===================================================================
--- trunk/src/wnaspi32/winaspi32.cpp	(revision 4264)
+++ trunk/src/wnaspi32/winaspi32.cpp	(revision 4265)
@@ -1,3 +1,3 @@
-/* $Id: winaspi32.cpp,v 1.8 2000-09-14 19:09:17 sandervl Exp $ */
+/* $Id: winaspi32.cpp,v 1.9 2000-09-15 13:25:50 sandervl Exp $ */
 /*
  * WNASPI routines
@@ -28,4 +28,8 @@
 //#include "callback.h"
 
+#ifdef DEBUG
+#define DEBUG_BUFFER
+#endif
+
 DEFAULT_DEBUG_CHANNEL(aspi)
 ODINDEBUGCHANNEL(WNASPI32)
@@ -80,13 +84,12 @@
   cdb = &prb->CDBByte[0];
   cmd = prb->CDBByte[0];
-  if (TRACE_ON(aspi))
-  {
-      DPRINTF("CDB buffer[");
+#ifdef DEBUG_BUFFER
+      dprintfNoEOL(("CDB buffer["));
       for (i = 0; i < prb->SRB_CDBLen; i++) {
-          if (i != 0) DPRINTF(",");
-          DPRINTF("%02x", *cdb++);
-      }
-      DPRINTF("]\n");
-  }
+          if (i != 0) dprintfNoEOL((",0x%02x", *cdb++));
+	  else        dprintfNoEOL(("0x%02x", *cdb++));
+      }
+      dprintfNoEOL(("]\n"));
+#endif
 }
 
@@ -178,4 +181,14 @@
               lpPRB->SRB_BufPointer,
               lpPRB->SRB_BufLen);
+
+#ifdef DEBUG_BUFFER
+      char *cdb = (char *)lpPRB->SRB_BufPointer;
+      dprintfNoEOL(("Write SRB buffer["));
+      for (int i = 0; i < lpPRB->SRB_BufLen; i++) {
+          if (i != 0) dprintfNoEOL((",0x%02x", *cdb++));
+	  else        dprintfNoEOL(("0x%02x", *cdb++));
+      }
+      dprintfNoEOL(("]\n"));
+#endif
     }
   }
@@ -195,4 +208,14 @@
                   aspi->buffer,
                   lpPRB->SRB_BufLen);
+#ifdef DEBUG_BUFFER
+      char *cdb = (char *)lpPRB->SRB_BufPointer;
+      dprintfNoEOL(("Read SRB buffer["));
+      for (int i = 0; i < lpPRB->SRB_BufLen; i++) {
+          if (i != 0) dprintfNoEOL((",0x%02x", *cdb++));
+	  else        dprintfNoEOL(("0x%02x", *cdb++));
+      }
+      dprintfNoEOL(("]\n"));
+#endif
+
         }
       }
@@ -251,6 +274,4 @@
 {
   LONG rc;
-  scsiObj *aspi;
-  ULONG hmtxDriver;
   BYTE bNumDrv;
   HKEY hkeyDrvInfo;
@@ -263,12 +284,6 @@
   bNumDrv = 0;
 
-  aspi = new scsiObj();
-
-  if( fGainDrvAccess( FALSE, &hmtxDriver) ) // Do nonblocking call for info
-  {
-
-    if(aspi->init(65535)==TRUE)
-    {
-
+  if(aspi->access(FALSE)) //'non-blocking' call
+  {
         bNumDrv = aspi->getNumHosts();
         brc = SS_COMP;
@@ -287,10 +302,5 @@
           RegCloseKey( hkeyDrvInfo);
         }
-        aspi->close();
-    }
-    else
-      brc = SS_FAILED_INIT;
-
-    fReleaseDrvAccess( hmtxDriver);
+        aspi->release();
   }
   else
@@ -324,5 +334,4 @@
     }
   }
-  delete aspi;
 
   return ((brc << 8) | bNumDrv); /* FIXME: get # of host adapters installed */
@@ -337,6 +346,4 @@
     ULONG ulParam, ulReturn;
     BYTE  bRC;
-    scsiObj *aspi;
-    ULONG hmtxDriver;
     LONG rc;
 
@@ -357,12 +364,8 @@
     }
 
-    aspi = new scsiObj();
-
     dwRC = SS_ERR;
 
-    if( fGainDrvAccess( TRUE, &hmtxDriver) ) // Block if a SRB is pending
-    {
-      if(aspi->init(65535)==TRUE)
-      {
+    if(aspi->access(TRUE)) // Block if a SRB is pending
+    {
         switch (lpSRB->common.SRB_Cmd)
         {
@@ -414,20 +417,12 @@
              break;
 
-          } // end switch (lpSRB->common.SRB_Cmd)
-
-      }
-      else
-      {
-        dwRC = bRC;
-      }
-
-      fReleaseDrvAccess( hmtxDriver);
-      aspi->close();
+        } // end switch (lpSRB->common.SRB_Cmd)
+
+      	aspi->release();
     }
     else
     {
-      dwRC = SS_NO_ASPI;
-    }
-    delete aspi;
+        dwRC = SS_NO_ASPI;
+    }
 
     return dwRC;
