⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 init.c

📁 网络驱动开发
💻 C
📖 第 1 页 / 共 4 页
字号:
/*++

Copyright (c) 2000 Microsoft Corporation. All rights reserved.

   File:       init.c
 
               Developed for Toshiba by Elisa Research Inc., CA
               http://www.elisaresearch.com
               (510) 770-4920


Abstract:

Author:

	A. Wang

Environment:

	Kernel mode

Revision History:

	09/23/96		kyleb		Added support for NdisAllocateMemoryWithTag
	01/07/97		awang		Initial of Toshiba ATM 155 Device Driver.
	05/25/97		awang		Removed Registry SlotNumber & BusNumber and
                               corresponded varialbes.
   03/03/97        hhan        Added in 'ScatterGetherDMA'

--*/

#include "precomp.h"
#pragma hdrstop

#define	MODULE_NUMBER	MODULE_INIT

#pragma NDIS_INIT_FUNCTION(DriverEntry)

NTSTATUS
DriverEntry(
   IN  PDRIVER_OBJECT  DriverObject,
   IN  PUNICODE_STRING RegistryPath
   )
/*++

Routine Description:

   Entry point for the driver.

Arguments:

   DriverObject    -   Pointer to the system allocated DRIVER_OBJECT.
   RegistryPath    -   Pointer to the UNICODE string defining the registry
                       path for the driver's information.

Return Value:

--*/
{
   NDIS_STATUS                     Status;
   NDIS_MINIPORT_CHARACTERISTICS   TbAtm155Chars;
   NDIS_HANDLE                     hWrapper;

   DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_INFO,
       ("==>DriverEntry\n"));

   //
   //	Initialize the wrapper.
   //
   NdisMInitializeWrapper(
      &hWrapper,
      DriverObject,
      RegistryPath,
      NULL);
   if (NULL == hWrapper)
   {
      DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_ERR,
         ("NdisMInitializeWrapper failed!\n"));

      return(NDIS_STATUS_FAILURE);
   }

   ZERO_MEMORY(&TbAtm155Chars, sizeof(TbAtm155Chars));

   //
   //	Initialize the miniport characteristics.
   //
   TbAtm155Chars.MajorNdisVersion = TBATM155_NDIS_MAJOR_VERSION;
   TbAtm155Chars.MinorNdisVersion = TBATM155_NDIS_MINOR_VERSION;
   TbAtm155Chars.CheckForHangHandler = TbAtm155CheckForHang;
   TbAtm155Chars.DisableInterruptHandler = NULL;
   TbAtm155Chars.EnableInterruptHandler = NULL;
   TbAtm155Chars.HaltHandler = TbAtm155Halt;
   TbAtm155Chars.HandleInterruptHandler = TbAtm155HandleInterrupt;
   TbAtm155Chars.InitializeHandler = TbAtm155Initialize;
   TbAtm155Chars.ISRHandler = TbAtm155ISR;
   TbAtm155Chars.ResetHandler = TbAtm155Reset;
   TbAtm155Chars.ReturnPacketHandler = TbAtm155ReturnPacket;
   TbAtm155Chars.AllocateCompleteHandler = TbAtm155AllocateComplete;
   TbAtm155Chars.CoSendPacketsHandler = TbAtm155SendPackets;
   TbAtm155Chars.CoCreateVcHandler = TbAtm155CreateVc;
   TbAtm155Chars.CoDeleteVcHandler = TbAtm155DeleteVc;
   TbAtm155Chars.CoActivateVcHandler = TbAtm155ActivateVc;
   TbAtm155Chars.CoDeactivateVcHandler = TbAtm155DeactivateVc;
   TbAtm155Chars.CoRequestHandler = TbAtm155Request;

   //
   //	Register the miniport with NDIS.
   //
   Status = NdisMRegisterMiniport(
               hWrapper,
               &TbAtm155Chars,
               sizeof(TbAtm155Chars));
   if (NDIS_STATUS_SUCCESS == Status)
   {
       //
       //	Save the handle to the wrapper.
       //	
       gWrapperHandle = hWrapper;
   }

   IF_DBG(DBG_COMP_INIT, DBG_LEVEL_ERR)
   {
       if (NDIS_STATUS_SUCCESS != Status)
      {
       	DBGPRINT(DBG_COMP_INIT,DBG_LEVEL_ERR,
   	    	("NdisMRegisterMiniport failed! Status: 0x%x\n", Status));
      }
   }

#if (WINVER < 0x0501)
   //
   //   Get the DMA alignment that we need.
   //
   gDmaAlignmentRequired = NdisGetCacheFillSize();
   if (gDmaAlignmentRequired < TBATM155_MIN_DMA_ALIGNMENT)
   {
       gDmaAlignmentRequired = TBATM155_MIN_DMA_ALIGNMENT;
   }
#endif

   DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_INFO,
      ("<==DriverEntry\n"));

   return(Status);
}

VOID
tbAtm155InitConfigurationInformation(
   IN  PTBATM155_REGISTRY_PARAMETER    pRegistryParameter
   )
/*++

Routine Description:

   This routine will initialize the configuration information with defaults.
   This is so that we will have something to initialize the driver with
   in case there are no overrides in the registry.

Arguments:

   pRegistryParameter  -   Pointer to the storage for the configuration
                           information.

Return Value:

   None.

--*/
{
   DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_INFO,
       ("==>tbAtm155InitConfigurationInformation\n"));

   ZERO_MEMORY(
       pRegistryParameter,
       sizeof(TBATM155_REGISTRY_PARAMETER) * TbAtm155MaxRegistryEntry);

   //
   //	Start off with a default hash table of 13.
   //
   pRegistryParameter[TbAtm155VcHashTableSize].Value = 13;
   pRegistryParameter[TbAtm155TotalRxBuffs].Value = DEFAULT_TOTAL_RECEIVE_BUFFERS;
   pRegistryParameter[TbAtm155BigReceiveBufferSize].Value = DEFAULT_RECEIVE_BIG_BUFFER_SIZE;
   pRegistryParameter[TbAtm155SmallReceiveBufferSize].Value = DEFAULT_RECEIVE_SMALL_BUFFER_SIZE;
   pRegistryParameter[TbAtm155NumberOfMapRegisters].Value = MAXIMUM_MAP_REGISTERS;

   DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_INFO,
     	("<==tbAtm155InitConfigurationInformation\n"));
}


NDIS_STATUS
tbAtm155ReadConfigurationInformation(
   IN      NDIS_HANDLE                     ConfigurationHandle,
   IN      PTBATM155_REGISTRY_PARAMETER    pRegistryParameter,
   IN  OUT PUCHAR                          CurrentAddress
   )
/*++

Routine Description:

   This routine will read any configuration information that is stored in the
   registry.
   (11/10/97)
   Added a new registry "Network Address"

Arguments:

   ConfgurationHandle  -   NDIS handled used to open/read config information.
   pRegistryParameter  -   Pointer to storage for registry parameters.

Return Value:

   NDIS_STATUS_FAILURE     if we were unable to open the registry information
                           for the adapter.

   NDIS_STATUS_SUCCESS     otherwise.

--*/
{
   NDIS_STATUS						Status;
   NDIS_HANDLE                     ConfigHandle;
   UINT                            c;
   PUCHAR                          NewNetworkAddress;
   UINT                            NewNetworkAddressLength;


   UNREFERENCED_PARAMETER(pRegistryParameter);

   DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_INFO,
       ("==>tbAtm155ReadConfigurationInformation\n"));
   //
   //	Open the configuration section of the registry for this adapter.
   //
   NdisOpenConfiguration(&Status, &ConfigHandle, ConfigurationHandle);
   if (NDIS_STATUS_SUCCESS != Status)
   {
       DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_ERR,
           ("Unable to open the TbAtm155's Parameters registry key\n"));

       return(Status);
   }

   /*
    *  See if user has defined new MAC address.
    *
    */
   NdisReadNetworkAddress(
       (PNDIS_STATUS) &Status,
       (PVOID *) &NewNetworkAddress,
       &NewNetworkAddressLength,
       ConfigHandle);

   if((Status == NDIS_STATUS_SUCCESS) && (ATM_MAC_ADDRESS_LENGTH == NewNetworkAddressLength ))
   {
       DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_INFO, ("MAC Address over-ride!"));

       //
       //	Save the network address from registry to StationAddress
       //
       NdisMoveMemory(
           CurrentAddress,
           NewNetworkAddress,
           NewNetworkAddressLength);

#if DBG
       DbgPrint("New MAC Address = %.2x-%.2x-%.2x-%.2x-%.2x-%.2x\n",
           CurrentAddress[0],
           CurrentAddress[1],
           CurrentAddress[2],
           CurrentAddress[3],
           CurrentAddress[4],
           CurrentAddress[5]);
#endif
   }
   else
   {
       // 
       // If fail to get network address from registry, set
       // this station address to be broadcast address to get
       // network address on NIC later.
       // 
       for (c = 0; c < ATM_MAC_ADDRESS_LENGTH; c++)
       {
           CurrentAddress[c] = 0x0ff;
       }
   }

   //
   //	Close the configuration handle.
   //
   NdisCloseConfiguration(ConfigHandle);

   DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_INFO,
       ("<==tbAtm155ReadConfigurationInformation\n"));

   return(NDIS_STATUS_SUCCESS);
}

NDIS_STATUS
tbAtm155ReadPciConfiguration(
   IN  PADAPTER_BLOCK  pAdapter,
   IN  NDIS_HANDLE     ConfigurationHandle
   )
/*++

Routine Description:

   This routine will get the resources that were assigned the adapter.  Verify
   that the adapter is one that we can support and program the PCI slot for
   needed parameters.

Arguments:

   pAdapter            -   Pointer to the adapter to program.
   ConfgurationHandle  -   NDIS handled used to open/read config information.

Return Value:

   NDIS_STATUS_FAILURE     for general failures.
   NDIS_STATUS_ADAPGTER_NOT_FOUND if there is an invalid DeviceID or VendorID.
   NDIS_STATUS_SUCCESS if we have succeeded in getting our resources.
	
--*/
{
   PHARDWARE_INFO                  pHwInfo = pAdapter->HardwareInfo;
   PCM_PARTIAL_RESOURCE_DESCRIPTOR Resource;
   USHORT                          VendorID;
   USHORT                          DeviceID;
   USHORT                          CommandWord;
   NDIS_STATUS                     Status;
   UINT                            c;
   UINT                            Temp;
   UCHAR                           ResourceBuf[ATM_RESOURCE_BUF_SIZE];
   PNDIS_RESOURCE_LIST             ResourceList = (PNDIS_RESOURCE_LIST)ResourceBuf;
   UINT                            ResourceBufSize = ATM_RESOURCE_BUF_SIZE;

   DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_INFO,
       ("==>tbAtm155ReadPciConfiguration\n"));

   //
   //   Query the adapter resources.
   //
   NdisMQueryAdapterResources(
       &Status,
       ConfigurationHandle,
       ResourceList,
       &ResourceBufSize);

   if (NDIS_STATUS_SUCCESS != Status)
   {
       DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_ERR,
           ("NdisMQueryAdapterResources() failed: 0x%x\n", Status));

       return(Status);
   }

   //
   //	Walk the resource list to get the adapters configuration
   //	information.
   //
   for (c = 0; c < ResourceList->Count; c++)
   {
       Resource = &ResourceList->PartialDescriptors[c];
       switch (Resource->Type)
       {
           case CmResourceTypeInterrupt:
               //
               //	Save the interrupt number with our adapter block.
               //
               pHwInfo->InterruptLevel = Resource->u.Interrupt.Level;
               pHwInfo->InterruptVector = Resource->u.Interrupt.Vector;

               DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_INFO,
           	    ("Configured to use interrupt Level: %u interrupt vector: %u\n",
           	        pHwInfo->InterruptLevel,
           	        pHwInfo->InterruptVector));

               break;

           case CmResourceTypeMemory:
           
               //
               //	Save the memory mapped base physical address and it's length.
               //
               pHwInfo->PhysicalIoSpace = Resource->u.Memory.Start;
               pHwInfo->IoSpaceLength = Resource->u.Memory.Length;

               DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_INFO,
                   ("Config to use IO space to mapped mem 0x%x:0x%x of length 0x%x\n",
                       NdisGetPhysicalAddressHigh(pHwInfo->PhysicalIoSpace),
                       NdisGetPhysicalAddressLow(pHwInfo->PhysicalIoSpace),
                       pHwInfo->IoSpaceLength));

               break;

           case CmResourceTypePort:

               //
               //	Save the port.
               //
               pHwInfo->InitialPort = NdisGetPhysicalAddressLow(Resource->u.Port.Start);
               pHwInfo->NumberOfPorts = Resource->u.Port.Length;

               DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_INFO,
           	    ("Configured to use port memory 0x%x of length 0x%x\n",
           		    pHwInfo->InitialPort,
           		    pHwInfo->NumberOfPorts));

               break;
       }
   }

   //
   //	Read the VendorID.
   //
   Temp = NdisReadPciSlotInformation(
               pAdapter->MiniportAdapterHandle,
               0,      // NDIS knows the real slot number
               FIELD_OFFSET(PCI_COMMON_CONFIG, VendorID),
               &VendorID,
               sizeof(VendorID));
   if (Temp != sizeof(VendorID))
   {
       DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_ERR,
           ("Unable to read the VendorID\n"));

       return(NDIS_STATUS_FAILURE);
   }

   //
   //	Verify vendor ID.
   //
   if (TOSHIBA_PCI_VENDOR_ID != VendorID)
   {
       DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_ERR,
           ("Invalid PCI Vendor ID: 0x%x\n", VendorID));

       return(NDIS_STATUS_ADAPTER_NOT_FOUND);
   }

   //
   //	Read the DeviceID.
   //
   Temp = NdisReadPciSlotInformation(
               pAdapter->MiniportAdapterHandle,
               0,      // NDIS knows the real slot number
               FIELD_OFFSET(PCI_COMMON_CONFIG, DeviceID),
               &DeviceID,
               sizeof(DeviceID));
   if (Temp != sizeof(DeviceID))
   {
       DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_ERR,
           ("Unable to read the DeviceID\n"));

       return(NDIS_STATUS_FAILURE);
   }

   //
   //  Make sure we recognize the device ID.
   //
   if (TBATM155_PCI_DEVICE_ID != DeviceID)
   {
       DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_ERR,
           ("Invalid PCI Device ID: 0x%x\n", DeviceID));

       return(NDIS_STATUS_ADAPTER_NOT_FOUND);
   }

   //
   //	Read the Command word in from PCI COMMON Config space.
   //
   Temp = NdisReadPciSlotInformation(
               pAdapter->MiniportAdapterHandle,
               0,      // NDIS knows the real slot number
               FIELD_OFFSET(PCI_COMMON_CONFIG, Command),
               &CommandWord,
               sizeof(CommandWord));
   if (Temp != sizeof(CommandWord))
   {
       DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_ERR,
           ("Unable to read the CommandWord\n"));

       return(NDIS_STATUS_FAILURE);
   }

   //
   //	Does the command word meet the following requirements?
   //
   if (((CommandWord & PCI_ENABLE_IO_SPACE) != PCI_ENABLE_IO_SPACE) ||
       ((CommandWord & PCI_ENABLE_MEMORY_SPACE) != PCI_ENABLE_MEMORY_SPACE) ||
       ((CommandWord & PCI_ENABLE_BUS_MASTER) != PCI_ENABLE_BUS_MASTER))
   {
       DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_ERR,
           ("Invalid PCI Command Word: 0x%x\n", CommandWord));

       CommandWord |= PCI_ENABLE_IO_SPACE |
                      PCI_ENABLE_MEMORY_SPACE |
                      PCI_ENABLE_BUS_MASTER;

       Temp = NdisWritePciSlotInformation(
                   pAdapter->MiniportAdapterHandle,
                   0,      // NDIS knows the real slot number
                   FIELD_OFFSET(PCI_COMMON_CONFIG, Command),
                   &CommandWord,
                   sizeof(CommandWord));
       if (Temp != sizeof(CommandWord))
       {
           DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_ERR,
               ("Unable to write the PCI command register\n"));

           return(NDIS_STATUS_FAILURE);
       }

       Temp = NdisReadPciSlotInformation(
                   pAdapter->MiniportAdapterHandle,
                   0,      // NDIS knows the real slot number
                   FIELD_OFFSET(PCI_COMMON_CONFIG, Command),
                   &CommandWord,
                   sizeof(CommandWord));
       if (Temp != sizeof(CommandWord))
       {
           DBGPRINT(DBG_COMP_INIT, DBG_LEVEL_ERR,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -