// bus.inc - bus related shells

#define WILDCARDBUSNUMBER  -1

//
// These are the Digi PCI ID numbers.
// All numbers are decimal.
//
#if defined(SUPPORT_HOUSTON) || defined(SUPPORT_LONDON) || defined(SUPPORT_CHASKA)
[PCIVendorInfo-DigiRAS]
VendorIDList = { DIGI_PCI_VENDORID, +
                 IBM_PCI_VENDORID, +
               }
#endif
#if defined(SUPPORT_CLYDESDALE)
[PCIVendorInfo-PowerSync]
VendorIDList = { DIGI_PCI_VENDORID }
#endif
#if defined(SUPPORT_SYNC570)
[PCIVendorInfo-Sync570]
VendorIDList = { DIGI_PCI_VENDORID }
#endif
#if defined(SUPPORT_ACCELEPORT_2000)
[PCIVendorInfo-AccelePort2000]
VendorIDList = { DIGI_PCI_VENDORID }
#endif

#if defined(SUPPORT_HOUSTON) || defined(SUPPORT_LONDON) || defined(SUPPORT_CHASKA)
[PCICardInfo-DigiRAS]
PciIDList = { DIGI_PCI_DEVICEID_HOUSTON, +
              DIGI_CPCI_DEVICEID_HOUSTON, +
              DIGI_PCI_DEVICEID_LONDON, +
              DIGI_PCI_DEVICEID_CHASKA, +

              IBM_PCI_DEVICEID_HOUSTON, +
              IBM_PCI_DEVICEID_LONDON, +
              IBM_PCI_DEVICEID_CHASKA, +
            }
#endif
#if defined(SUPPORT_CLYDESDALE)
[PCICardInfo-PowerSync]
PciIDList = { DIGI_PCI_DEVICEID_CLYDE_NON_CSU_2, +
              DIGI_PCI_DEVICEID_CLYDE_NON_CSU_4, +
              DIGI_PCI_DEVICEID_CLYDE_NON_CSU_603_2, +
              DIGI_PCI_DEVICEID_CLYDE_NON_CSU_603_4, +
              DIGI_CPCI_DEVICEID_CLYDE_NON_CSU_2, +
              DIGI_CPCI_DEVICEID_CLYDE_NON_CSU_4, +
              DIGI_CPCI_DEVICEID_CLYDE_NON_CSU_603_2, +
              DIGI_CPCI_DEVICEID_CLYDE_NON_CSU_603_4, +
              DIGI_PMC_DEVICEID_CLYDE_NON_CSU_2, +
              DIGI_PMC_DEVICEID_CLYDE_NON_CSU_603_2, +
            }
#endif
#if defined(SUPPORT_SYNC570)
[PCICardInfo-Sync570]
PciIDList = { DIGI_PCI_DEVICEID_SYNC570_PCI_2PORT, +
              DIGI_PCI_DEVICEID_SYNC570_PCI_4PORT, +
            }
#endif
#if defined(SUPPORT_ACCELEPORT_2000)
[PCICardInfo-AccelePort2000]
PciIDList = { DIGI_PCI_DEVICEID_ACCELEPORT_2000 }
#endif

//
// Not all board types support the same bus architectures
// Dependent on $(BoardType)
//
#if defined(SUPPORT_HOUSTON) || defined(SUPPORT_LONDON) || defined(SUPPORT_CHASKA)
[BusTypesSupported-DigiRAS]
5     // PCI
#endif
#if defined(SUPPORT_CLYDESDALE)
[BusTypesSupported-PowerSync]
5     // PCI
#endif
#if defined(SUPPORT_SYNC570)
[BusTypesSupported-Sync570]
1     // ISA
2	  // EISA
5     // PCI
#endif
#if defined(SUPPORT_ACCELEPORT_2000)
[BusTypesSupported-AccelePort2000]
5     // PCI
#endif

[BusTypesSupported]
BusTypeList = ^(BusTypesSupported-$(BoardType),1)

#if defined(SUPPORT_SYNC570)
[ISABoardOptionsIn-Sync570]
"2 Port ISA SYNC570"
"4 Port ISA SYNC570"
#endif

[ISABoardOptionsIn]
BoardTypesIn = ^(ISABoardOptionsIn-$(BoardType),1)

#if defined(SUPPORT_SYNC570)
[ISABoardIdentityOut-Sync570]
"DigiSync570Port2Isa"
"DigiSync570Port4Isa"
#endif

[ISABoardIdentityOut]
BoardIdentityOut = ^(ISABoardIdentityOut-$(BoardType),1)

[BusInterfaceMap]
1 = ISA
2 = ISA
5 = PCI

//
//	Get data about the Bus
//
//    $0 = Board type [$(Option)]
//		$1 = Product hardware description (for user)
//
//		$R0 = Boolean indicating success.
//		$R1 = Bus name
//		$R1 = Bus type
//		$R2 = Bus number
//
[GetBusData]

	set BoardType = $($0)
	set Description = $($1)
	_Enter("GetBusData", _Show(BoardType)_Show(Description))

	read-syms BusTypesSupported

	set Success = FALSE
	set BusName = EMPTY_STR

	_Trace("GetBusData", _Show(!STF_NCDETINFO))
	_IfEmptyList($(!STF_NCDETINFO))

      QueryListSize NumberOfBusesSupported $(BusTypeList)
	  _Trace("GetBusData", _Show(BusTypeList))
      ifint $(NumberOfBusesSupported) > 1
			_Call("GetBusTypeDialog", _Show(Description))
		   _UtilityShell GetBusTypeDialog $(Description)
			_Return("GetBusTypeDialog", _Show($R0)_Show($R1)_Show($R2))
         _OnShellErrorGoto(Exit)
         _OnUtilityShellErrorGoto($($R0), Exit)

		   set BusType = $($R1)
		   set BusNumber = $($R2)
      else
		   set BusType = *($(BusTypeList), 1)
		   set BusNumber = WILDCARDBUSNUMBER
      endif

	else
      // If the BusType & BusNumber have been passed to us via STF_NCDETINFO
      // we make the assumption that those values are correct.
		set BusType = *($(!STF_NCDETINFO), 5)
		set BusNumber = *($(!STF_NCDETINFO), 6)
	endif

	Ifcontains(i) $(BusType) not-in $(BusTypeList)
      _AssertMsg(FALSE, "Unsupported bus type!")
		read-syms InstallMessages-$(!STF_LANGUAGE)
		_LocalShell SetupMessage "FATAL" $(BusTypeNotSupported)
		goto Exit
   endif

	set BusName = #(BusInterfaceMap, $(BusType), 1)
	_Trace("GetBusData", _Show(BusType)_Show(BusNumber)_Show(BusName))

	set Success = TRUE

Exit = +

	_Exit("GetBusData", _Show(Success)_Show(BusName)_Show(BusType)_Show(BusNumber))
	Return $(Success) $(BusName) $(BusType) $(BusNumber)


// Copied from utility.inf
[InitBaseVars]
KeyNull            = ""
MAXIMUM_ALLOWED    = 33554432
KeyInfo            = {}
RegistryErrorIndex = NO_ERROR
NoTitle            = 0
CurrentControlSet  = "SYSTEM\CurrentControlSet"
ServicesBaseName   = $(CurrentControlSet)"\Services"
NetworkCardKeyName = $(!NTN_SoftwareBase)"\Microsoft\Windows NT\CurrentVersion\NetworkCards"
!RegLastError      = NO_ERROR

// Returns YES iff the following match: BusNumber, SlotNumber, PCI ID, Sub ID, Vendor ID
// Allows multiple board types to be installed (DxbX instances) in the same slot...
// Original version (IsNetCardAlreadyInstalled) from utility.inf was modified to do the extra checking

[IsThisCardAlreadyInstalled]

    read-syms InitBaseVars

    set AlreadyExisted  = "NO"
    set CardBusNum  = $($0)
    set CardSlotNum = $($1)
    set CardPciId = $($2)
    set CardSubId = $($3)
    set CardVendorId = $($4)

	_Enter("IsThisCardAlreadyInstalled", _Show(CardBusNum)_Show(CardSlotNum)_Show(CardPciId)_Show(CardSubId)_Show(CardVendorId))

    OpenRegKey $(!REG_H_LOCAL) "" $(NetworkCardKeyName) $(MAXIMUM_ALLOWED) IE_KeyNetcards
    Ifstr $(IE_KeyNetcards) == $(KeyNull)
       set RegistryErrorIndex = UNABLE_OPEN_NETWORKCARD_SECTION
       goto IE_Return
    endif

    EnumRegKey $(IE_KeyNetcards) IE_KeyNameList

    ForListDo  $(IE_KeyNameList)
        set IE_KeyName = *($($),1)
        OpenRegKey $(IE_KeyNetcards) "" $(IE_KeyName) $(MAXIMUM_ALLOWED) IE_Card
        Ifstr $(IE_Card) == $(KeyNull)
           set RegistryErrorIndex = UNABLE_OPEN_NETWORKCARD_SECTION
           goto IE_Return
        endif

        GetRegValue $(IE_Card), "ServiceName", ServiceNameInfo
        set ServiceName = *($(ServiceNameInfo), 4)
        OpenRegKey $(!REG_H_LOCAL) "" +
           $(ServicesBaseName)"\"$(ServiceName)"\Parameters" +
           $(MAXIMUM_ALLOWED) IE_KeyService
        Ifstr $(IE_KeyService) == $(KeyNull)
           set RegistryErrorIndex = UNABLE_OPEN_NETWORKCARD_SECTION
           goto IE_Return
        endif

		// Get the Registry's idea of what's installed
        GetRegValue $(IE_KeyService), "BusNumber", BusInfo
        GetRegValue $(IE_KeyService), "SlotNumber", SlotInfo
        GetRegValue $(IE_KeyService), "PciId", PciInfo
        GetRegValue $(IE_KeyService), "SubDeviceId", SubIdInfo
        GetRegValue $(IE_KeyService), "VendorId", VendorIdInfo

        set RegBusNum = *($(BusInfo), 4)
        set RegSlotNum = *($(SlotInfo), 4)
        set RegPciId = *($(PciInfo), 4)
        set RegSubId = *($(SubIdInfo), 4)
        set RegVendorId = *($(VendorIdInfo), 4)
	 	_Trace("IsThisCardAlreadyInstalled",_Show(RegBusNum)_Show(RegSlotNum)_Show(RegPciId)_Show(RegSubId)_Show(RegVendorId))

		// We need an exact match here since we allow multiple instances to be installed in the same slot
        ifint $(RegBusNum) == $(CardBusNum)
            ifint $(RegSlotNum) == $(CardSlotNum)
	            ifint $(RegPciId) == $(CardPciId)
			        ifint $(RegSubId) == $(CardSubId)
						ifint $(RegVendorId) == $(CardVendorId)
							set AlreadyExisted = "YES"
						endif
					endif
				endif
            endif
        endif

    EndForListDo

IE_Return = +
	_Exit("IsThisCardAlreadyInstalled", _Show(RegistryErrorIndex)_Show(AlreadyExisted))
    return $(RegistryErrorIndex) $(AlreadyExisted)
// Copied from utility.inf

//
//	Find first uninstalled adapter un the given bus.
//
//		$0 = Adapter list
//		$1 = Bus number
//
//		$R0 = First adapter found (list data) [EMPTY_LIST = error]
//
[FirstUninstalledAdapter]

	set AdapterList = $($0)
	set BusNumber = $($1)
	_Enter("FirstUninstalledAdapter", _Show(AdapterList)_Show(BusNumber))

	set Adapter = EMPTY_LIST

	ForListDo $(AdapterList)
		set AdapterBusNum = *($($), 1)

		_IfOr(ifint $(BusNumber) == WILDCARDBUSNUMBER, ifint $(AdapterBusNum) == $(BusNumber))

			// This is what the AdapterList elements look like:
			//    BusNumber
			//    SlotNumber
			//    VendorId
			//    DeviceId
			//    SubVendorId
			//    SubSystemId
			//    RevisionId

			set AdapterSlotNum = *($($), 2)
			set AdapterVendorId = *($($), 3)
			set AdapterPciId = *($($), 4)
			set AdapterSubId = *($($), 6)
			_Trace("FirstUninstalledAdapter - Loop Params: ",_Show(AdapterSlotNum)_Show(AdapterVendorId)_Show(AdapterPciId)_Show(AdapterSubId))

			_LocalShell IsThisCardAlreadyInstalled $(AdapterBusNum) $(AdapterSlotNum) $(AdapterPciId) $(AdapterSubId) $(AdapterVendorId)

			ifstr(i) $($R1) != "YES"
				set Adapter = $($)
				goto Exit
			endif

		_EndIfOr

	EndForListDo

Exit = +

	_Exit("FirstUninstalledAdapter", _Show(Adapter))
	Return $(Adapter)

//	Common parameters to Find* shell sections
//
//		$0 = Interface type
//		$1 = Bus number
//		$2 = Board type [$(Option)]
//
//		$R0 = Adapter Data (as a list) [EMPTY_LIST = error]
//

[FindPCI]
	set InterfaceType = $($0)
	set BusNumberIn = $($1)
	set BoardType = $($2)
	_Enter("FindPCI", _Show(InterfaceType)_Show(BusNumberIn)_Show(BoardType))

	read-syms PCIVendorInfo-$(BoardType)
	read-syms PCICardInfo-$(BoardType)

	set AdapterData = EMPTY_LIST
	set AdapterFullList = EMPTY_LIST

	// Load the library here, it's used once for each board we find...
	read-syms DetectionConstants
	LoadLibrary "x" $(DETECTDLL_PATH) DetectHandle

	_Trace("FindPCI - VendorIDList: ", _Show(VendorIDList))
	_Trace("FindPCI - PciIDList: ", _Show(PciIDList))

	ForListDo $(VendorIDList)
		set VendorID = $($)

		_Trace("FindPCI - Loop:", _Show(VendorID))

		ForListDo $(PciIDList)
			set PciID = $($)

			_Trace("FindPCI - Loop:", _Show(PciID))

			// Look for all the PCI cards with this ID.
			_Call("GetPCIInformation", _Show(VendorID)_Show(PciID))
			_UtilityShell GetPCIInformation $(VendorID) $(PciID)
			_Return("GetPCIInformation", _Show($R0))
			_OnShellErrorGoto(Exit)
			set AdapterList = $($R0)
			_Trace("Found PCI adapter(s): ", _Show(AdapterList))

			// For all of the AdapterList entries, get the board's PCI info and plug it into the ever growing array
			ForListDo $(AdapterList)
				set BusNumber = *($($), 1)
				set SlotNumber = *($($), 2)

				_Call("LibraryProcedure DetectEntry", _Show(!STF_HWND)_Show(BoardType)_Show(InterfaceType)_Show(BusNumber)_Show(SlotNumber))
				LibraryProcedure Result $(DetectHandle) DetectEntry $(!STF_HWND) $(BoardType) $(InterfaceType) $(BusNumber) $(SlotNumber)
				_Return("DetectEntry", _Show(Result))
				// Check the results for this board...
				set DetectionResult = *($(Result), 1)

				//
				// This is what we get back from DetectEntry for PCI information:
				//
				//    BusNumber
				//    SlotNumber
				//    VendorId
				//    DeviceId
				//    SubVendorId
				//    SubSystemId
				//    RevisionId
				//

				_ToBoolean(ifstr(i) $(DetectionResult) == "SUCCESS", DetectionWorked)
				_AssertGoto($(DetectionWorked), Exit)
				set NewElement = *($(Result), 2)
				set AdapterFullList = >($(AdapterFullList), $(NewElement))

				_Trace("List So Far: ", _Show(AdapterFullList))

			EndForListDo // AdapterList

		EndForListDo // PciIdList

	EndForListDo // VendorIDList

	// Now we call FirstUninstalledAdapter with the derived list...
	_IfNotEmptyList($(AdapterFullList))
		_LocalShell FirstUninstalledAdapter $(AdapterFullList) $(BusNumberIn)
		set AdapterData = $($R0)
		_IfNotEmptyList($(AdapterData))
			_Trace("This one isn't installed yet: ", _Show1(AdapterData))
			goto Exit
		endif
	endif

Exit = +

	FreeLibrary $(DetectHandle)

	_Exit("FindPCI", _Show(AdapterData))
	Return $(AdapterData)

[FindISA]
	set InterfaceType = $($0)
	set BusNumber = $($1)
	set BoardType = $($2)
	_Enter("FindISA", _Show(InterfaceType)_Show(BusNumber)_Show(BoardType))
//
//  Detection of ISA adapters is not a requirement. This will be studied during
//  integration testing and put in if it is ready.  If not we will look at it in
//  the net release.
//
//
//	set AdapterData = EMPTY_LIST
//
//	set UsedMemBaseList = EMPTY_LIST
//	set UsedIOBaseList = EMPTY_LIST
//	set UsedIRQList = EMPTY_LIST
//
//	ifint $(BusNumber) == WILDCARDBUSNUMBER
//      _Warning("FindISA was passed WILDCARDBUSNUMBER, defaulting to BusNumber = 0", "")
//     // NOTE: This is unexpected behavior, but just in case it actually happens we can NOT pass
//      //       WILDCARDBUSNUMBER to LibraryProcedure Result $(DetectHandle) DetectEntry,
//      //       so we default to BusNumber 0.
//      set BusNumber = 0
//  endif
//
//	_LocalShell EnumAdapters { $(BoardType) } FALSE
//	set AdapterDataList = $($R0)
//
//	ForListDo $(AdapterDataList)
//		set ServiceName = *($($), 1)
//		_Trace("FindISA", _Show(ServiceName))
//
//		_OpenServiceParamKey($(ServiceName), KeyService)
//
//		_GetRegDwordByKey $(KeyService) DXB_REGVAL_IO_ADDRESS
//      _Assert($($R0))
//		set UsedIOBaseList = >($(UsedIOBaseList), $($R1))
//
//		_GetRegDwordByKey $(KeyService) DXB_REGVAL_MEMORY_ADDRESS
//      _Assert($($R0))
//		set UsedMemBaseList = >($(UsedMemBaseList), $($R1))
//
//		_GetRegDwordByKey $(KeyService) DXB_REGVAL_INTERRUPT
//      _Assert($($R0))
//		set UsedIRQList = >($(UsedIRQList), $($R1))
//
//		_CloseRegKey($(KeyService))
//	EndForListDo

	// The detection DLL uses NetDetect to find available resources and to read/write
	// IOPort memory to find adapters. It returns an INF style list of adapters
	// found when successful. A YESNO MessageBox is displayed first asking the user
	// if they want to continue. NO is returned as "NO". If there is any problem "FAIL"
	// is returned and the DLL will put up a MessageBox with an error string.

//	_SubroutineShell PopBillboard
//	read-syms DetectionConstants
//	LoadLibrary "x" $(DETECTDLL_PATH) DetectHandle
//	_Call("LibraryProcedure DetectEntry", _Show(BoardType)_Show(!STF_HWND)_Show(InterfaceType)_Show(BusNumber)_Show(UsedIOBaseList)_Show(UsedMemBaseList)_Show(UsedIRQList))
//	LibraryProcedure Result $(DetectHandle) DetectEntry $(BoardType) $(!STF_HWND) $(InterfaceType) $(BusNumber) $(UsedIOBaseList) $(UsedMemBaseList) $(UsedIRQList)
//	_Return("DetectEntry", _Show(Result))

//	// Finding an adapter is not required

	read-syms ISABoardOptionsIn
	read-syms ISABoardIdentityOut

	LoadLibrary "x" $(!STF_SRCDIR)\DigiDlg.dll !DIGIDLGHANDLE

	set DlgType = "Combination"
	set DlgTemplate = "IDD_ISABOARDTYPE"

	// Pick an Adapter
	set Caption = "Select The Board Type"
	set Combo1Label = "&Board Type:"
	set Combo1List = $(BoardTypesIn)
	set Combo1Out  = *($(BoardTypesIn), 1)
	set ComboListItemsIn  = {Combo1List}
	set ComboListItemsOut = {Combo1Out}

	_Trace("FindISA", _Show(BoardTypesIn)_Show(BoardIdentityOut))
	_Trace("FindISA", _Show(Combo1List)_Show(Combo1Out))
	_Trace("FindISA", _Show(ComboListItemsIn)_Show(ComboListItemsOut))

	ui start "DlgTemplate" $(!DIGIDLGHANDLE)
	ui pop 1

	// Find the Identity Based on the Adapter Chocen
	ForListDo $(Combo1List)
		ifstr(i) $(Combo1Out) == $($)
			set Identity = *($(BoardIdentityOut),$(#))
			goto BoardTypeSet
		endif
	EndForListDo

BoardTypeSet = +

	set AdapterData = { $(BusNumber), 0, 768, 851968, 7, $(Identity) }

   //
   // This is what we get back from DetectEntry for ISA information:
   //
   //    IOBase
   //    MemBase
   //    IRQ
   //

//   set DetectionResult = *($(Result), 1)
//	ifstr(i) $(DetectionResult) != "SUCCESS"
//		goto Error_FreeLibrary
//	endif
//
//   set DetectionData = *($(Result), 2)
//   set AdapterData = { $(BusNumber), +
//                       0, +
//                       *($(DetectionData), 1), +
//                       *($(DetectionData), 2), +
//                       *($(DetectionData), 3)  +
//                     }

//Error_FreeLibrary = +

//	FreeLibrary $(DetectHandle)

Exit = +

	ifstr(i) $(DIGIDLGHANDLE) != ""
		FreeLibrary $(DIGIDLGHANDLE)
		set $(DIGIDLGHANDLE) = ""
	Endif

	_Exit("FindISA", _Show(AdapterData))
	Return $(AdapterData)
