protocol.c
上传用户:zhuzhu0204
上传日期:2020-07-13
资源大小:13165k
文件大小:45k
开发平台:

Visual C++

  1. /*++
  2. Copyright(c) 1992-2000  Microsoft Corporation
  3. Module Name:
  4.     protocol.c
  5. Abstract:
  6.     Ndis Intermediate Miniport driver sample. This is a passthru driver.
  7. Author:
  8. Environment:
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. #define MAX_PACKET_POOL_SIZE 0x0000FFFF
  14. #define MIN_PACKET_POOL_SIZE 0x000000FF
  15. VOID
  16. PtBindAdapter(
  17.     OUT PNDIS_STATUS            Status,
  18.     IN  NDIS_HANDLE             BindContext,
  19.     IN  PNDIS_STRING            DeviceName,
  20.     IN  PVOID                   SystemSpecific1,
  21.     IN  PVOID                   SystemSpecific2
  22.     )
  23. /*++
  24. Routine Description:
  25.     Called by NDIS to bind to a miniport below.
  26. Arguments:
  27.     Status            - Return status of bind here.
  28.     BindContext        - Can be passed to NdisCompleteBindAdapter if this call is pended.
  29.     DeviceName         - Device name to bind to. This is passed to NdisOpenAdapter.
  30.     SystemSpecific1    - Can be passed to NdisOpenProtocolConfiguration to read per-binding information
  31.     SystemSpecific2    - Unused
  32. Return Value:
  33.     NDIS_STATUS_PENDING    if this call is pended. In this case call NdisCompleteBindAdapter
  34.     to complete.
  35.     Anything else          Completes this call synchronously
  36. --*/
  37. {
  38.     NDIS_HANDLE                     ConfigHandle = NULL;
  39.     PNDIS_CONFIGURATION_PARAMETER   Param;
  40.     NDIS_STRING                     DeviceStr = NDIS_STRING_CONST("UpperBindings");
  41.     PADAPT                          pAdapt = NULL;
  42.     NDIS_STATUS                     Sts;
  43.     UINT                            MediumIndex;
  44.     ULONG                           TotalSize;
  45.     BOOLEAN                         LockAllocated = FALSE;
  46.     UNREFERENCED_PARAMETER(BindContext);
  47.     UNREFERENCED_PARAMETER(SystemSpecific2);
  48.     
  49.     DBGPRINT(("==> Protocol BindAdaptern"));
  50.     do
  51.     {
  52.         //
  53.         // Access the configuration section for our binding-specific
  54.         // parameters.
  55.         //
  56.         NdisOpenProtocolConfiguration(Status,
  57.                                        &ConfigHandle,
  58.                                        SystemSpecific1);
  59.         if (*Status != NDIS_STATUS_SUCCESS)
  60.         {
  61.             break;
  62.         }
  63.         //
  64.         // Read the "UpperBindings" reserved key that contains a list
  65.         // of device names representing our miniport instances corresponding
  66.         // to this lower binding. Since this is a 1:1 IM driver, this key
  67.         // contains exactly one name.
  68.         //
  69.         // If we want to implement a N:1 mux driver (N adapter instances
  70.         // over a single lower binding), then UpperBindings will be a
  71.         // MULTI_SZ containing a list of device names - we would loop through
  72.         // this list, calling NdisIMInitializeDeviceInstanceEx once for
  73.         // each name in it.
  74.         //
  75.         NdisReadConfiguration(Status,
  76.                               &Param,
  77.                               ConfigHandle,
  78.                               &DeviceStr,
  79.                               NdisParameterString);
  80.         if (*Status != NDIS_STATUS_SUCCESS)
  81.         {
  82.             break;
  83.         }
  84.         //
  85.         // Allocate memory for the Adapter structure. This represents both the
  86.         // protocol context as well as the adapter structure when the miniport
  87.         // is initialized.
  88.         //
  89.         // In addition to the base structure, allocate space for the device
  90.         // instance string.
  91.         //
  92.         TotalSize = sizeof(ADAPT) + Param->ParameterData.StringData.MaximumLength;
  93. // BEGIN_PTUSERIO   在这个函数中,我们要初始化设备名称
  94. // 为下层适配器名称申请空间
  95. TotalSize += DeviceName->MaximumLength;
  96. // END_PTUSERIO
  97.         NdisAllocateMemoryWithTag(&pAdapt, TotalSize, TAG);
  98.         if (pAdapt == NULL)
  99.         {
  100.             *Status = NDIS_STATUS_RESOURCES;
  101.             break;
  102.         }
  103.         //
  104.         // Initialize the adapter structure. We copy in the IM device
  105.         // name as well, because we may need to use it in a call to
  106.         // NdisIMCancelInitializeDeviceInstance. The string returned
  107.         // by NdisReadConfiguration is active (i.e. available) only
  108.         // for the duration of this call to our BindAdapter handler.
  109.         //
  110.         NdisZeroMemory(pAdapt, TotalSize);
  111.         pAdapt->DeviceName.MaximumLength = Param->ParameterData.StringData.MaximumLength;
  112.         pAdapt->DeviceName.Length = Param->ParameterData.StringData.Length;
  113.         pAdapt->DeviceName.Buffer = (PWCHAR)((ULONG_PTR)pAdapt + sizeof(ADAPT));
  114.         NdisMoveMemory(pAdapt->DeviceName.Buffer,
  115.                        Param->ParameterData.StringData.Buffer,
  116.                        Param->ParameterData.StringData.MaximumLength);
  117. // BEGIN_PTUSERIO 在这里移动设备名称
  118. // 构造下层设备名称字符串
  119. pAdapt->LowerDeviceName.MaximumLength = DeviceName->MaximumLength;
  120. pAdapt->LowerDeviceName.Length = DeviceName->Length;
  121. // 你看上面的pAdapt->DeviceName.Buffer指向结构的末尾,我们的pAdapt->LowerDeviceName.Buffer再向后面添加
  122. // 让缓冲区指向结构的最后(要在上面的pAdapt->DeviceName.Buffer之后)
  123. pAdapt->LowerDeviceName.Buffer = (PWCHAR)((ULONG_PTR)pAdapt + sizeof(ADAPT) + pAdapt->DeviceName.MaximumLength);
  124. // 移动内存
  125. NdisMoveMemory(pAdapt->LowerDeviceName.Buffer, DeviceName->Buffer, DeviceName->MaximumLength);
  126. // BEGIN_PTEX_FILTER
  127.         //
  128.         // 初始化此适配器上的过滤相关数据
  129.         //
  130.         FltOnInitAdapter(pAdapt);
  131. // END_PTEX_FILTER
  132. // 添加协议对适配器的引用
  133. PtRefAdapter(pAdapt);
  134. // END_PTUSERIO
  135.         NdisInitializeEvent(&pAdapt->Event);
  136.         NdisAllocateSpinLock(&pAdapt->Lock);
  137.         LockAllocated = TRUE;
  138.         //
  139.         // Allocate a packet pool for sends. We need this to pass sends down.
  140.         // We cannot use the same packet descriptor that came down to our send
  141.         // handler (see also NDIS 5.1 packet stacking).
  142.         //
  143.         NdisAllocatePacketPoolEx(Status,
  144.                                    &pAdapt->SendPacketPoolHandle,
  145.                                    MIN_PACKET_POOL_SIZE,
  146.                                    MAX_PACKET_POOL_SIZE - MIN_PACKET_POOL_SIZE,
  147.                                    sizeof(SEND_RSVD));
  148.         if (*Status != NDIS_STATUS_SUCCESS)
  149.         {
  150.             break;
  151.         }
  152.         //
  153.         // Allocate a packet pool for receives. We need this to indicate receives.
  154.         // Same consideration as sends (see also NDIS 5.1 packet stacking).
  155.         //
  156.         NdisAllocatePacketPoolEx(Status,
  157.                                    &pAdapt->RecvPacketPoolHandle,
  158.                                    MIN_PACKET_POOL_SIZE,
  159.                                    MAX_PACKET_POOL_SIZE - MIN_PACKET_POOL_SIZE,
  160.                                    PROTOCOL_RESERVED_SIZE_IN_PACKET);
  161.         if (*Status != NDIS_STATUS_SUCCESS)
  162.         {
  163.             break;
  164.         }
  165.         //
  166.         // Now open the adapter below and complete the initialization
  167.         //
  168.         NdisOpenAdapter(Status,
  169.                           &Sts,
  170.                           &pAdapt->BindingHandle,
  171.                           &MediumIndex,
  172.                           MediumArray,
  173.                           sizeof(MediumArray)/sizeof(NDIS_MEDIUM),
  174.                           ProtHandle,
  175.                           pAdapt,
  176.                           DeviceName,
  177.                           0,
  178.                           NULL);
  179.         if (*Status == NDIS_STATUS_PENDING)
  180.         {
  181.             NdisWaitEvent(&pAdapt->Event, 0);
  182.             *Status = pAdapt->Status;
  183.         }
  184.         if (*Status != NDIS_STATUS_SUCCESS)
  185.         {
  186.             break;
  187.         }
  188.         pAdapt->Medium = MediumArray[MediumIndex];
  189.         //
  190.         // Now ask NDIS to initialize our miniport (upper) edge.
  191.         // Set the flag below to synchronize with a possible call
  192.         // to our protocol Unbind handler that may come in before
  193.         // our miniport initialization happens.
  194.         //
  195.         pAdapt->MiniportInitPending = TRUE;
  196.         NdisInitializeEvent(&pAdapt->MiniportInitEvent);
  197.         *Status = NdisIMInitializeDeviceInstanceEx(DriverHandle,
  198.                                            &pAdapt->DeviceName,
  199.                                            pAdapt);
  200.         if (*Status != NDIS_STATUS_SUCCESS)
  201.         {
  202.             DBGPRINT(("BindAdapter: Adapt %p, IMInitializeDeviceInstance error %xn",
  203.                 pAdapt, *Status));
  204.             break;
  205.         }
  206.     } while(FALSE);
  207.     //
  208.     // Close the configuration handle now - see comments above with
  209.     // the call to NdisIMInitializeDeviceInstanceEx.
  210.     //
  211.     if (ConfigHandle != NULL)
  212.     {
  213.         NdisCloseConfiguration(ConfigHandle);
  214.     }
  215.     if (*Status != NDIS_STATUS_SUCCESS)
  216.     {
  217.         if (pAdapt != NULL)
  218.         {
  219.             if (pAdapt->BindingHandle != NULL)
  220.             {
  221.                 NDIS_STATUS    LocalStatus;
  222.                 //
  223.                 // Close the binding we opened above.
  224.                 //
  225.                 NdisResetEvent(&pAdapt->Event);
  226.                 
  227.                 NdisCloseAdapter(&LocalStatus, pAdapt->BindingHandle);
  228.                 pAdapt->BindingHandle = NULL;
  229.                 if (LocalStatus == NDIS_STATUS_PENDING)
  230.                 {
  231.                      NdisWaitEvent(&pAdapt->Event, 0);
  232.                      LocalStatus = pAdapt->Status;
  233.                 }
  234.             }
  235.  
  236. // BEGIN_PTUSERIO
  237. // 移除协议对适配器的引用
  238. PtDerefAdapter(pAdapt);
  239. // END_PTUSERIO
  240.             pAdapt = NULL;
  241.         }
  242.     }
  243.     DBGPRINT(("<== Protocol BindAdapter: pAdapt %p, Status %xn", pAdapt, *Status));
  244. }
  245. VOID
  246. PtOpenAdapterComplete(
  247.     IN  NDIS_HANDLE             ProtocolBindingContext,
  248.     IN  NDIS_STATUS             Status,
  249.     IN  NDIS_STATUS             OpenErrorStatus
  250.     )
  251. /*++
  252. Routine Description:
  253.     Completion routine for NdisOpenAdapter issued from within the PtBindAdapter. Simply
  254.     unblock the caller.
  255. Arguments:
  256.     ProtocolBindingContext    Pointer to the adapter
  257.     Status                    Status of the NdisOpenAdapter call
  258.     OpenErrorStatus            Secondary status(ignored by us).
  259. Return Value:
  260.     None
  261. --*/
  262. {
  263.     PADAPT      pAdapt =(PADAPT)ProtocolBindingContext;
  264.     
  265.     UNREFERENCED_PARAMETER(OpenErrorStatus);
  266.     
  267.     DBGPRINT(("==> PtOpenAdapterComplete: Adapt %p, Status %xn", pAdapt, Status));
  268.     pAdapt->Status = Status;
  269.     NdisSetEvent(&pAdapt->Event);
  270. }
  271. VOID
  272. PtUnbindAdapter(
  273.     OUT PNDIS_STATUS        Status,
  274.     IN  NDIS_HANDLE            ProtocolBindingContext,
  275.     IN  NDIS_HANDLE            UnbindContext
  276.     )
  277. /*++
  278. Routine Description:
  279.     Called by NDIS when we are required to unbind to the adapter below.
  280.     This functions shares functionality with the miniport's HaltHandler.
  281.     The code should ensure that NdisCloseAdapter and NdisFreeMemory is called
  282.     only once between the two functions
  283. Arguments:
  284.     Status                    Placeholder for return status
  285.     ProtocolBindingContext    Pointer to the adapter structure
  286.     UnbindContext            Context for NdisUnbindComplete() if this pends
  287. Return Value:
  288.     Status for NdisIMDeinitializeDeviceContext
  289. --*/
  290. {
  291.     PADAPT         pAdapt =(PADAPT)ProtocolBindingContext;
  292.     NDIS_STATUS    LocalStatus;
  293.     UNREFERENCED_PARAMETER(UnbindContext);
  294.     
  295.     DBGPRINT(("==> PtUnbindAdapter: Adapt %pn", pAdapt));
  296.     //
  297.     // Set the flag that the miniport below is unbinding, so the request handlers will
  298.     // fail any request comming later
  299.     // 
  300.     NdisAcquireSpinLock(&pAdapt->Lock);
  301.     pAdapt->UnbindingInProcess = TRUE;
  302.     if (pAdapt->QueuedRequest == TRUE)
  303.     {
  304.         pAdapt->QueuedRequest = FALSE;
  305.         NdisReleaseSpinLock(&pAdapt->Lock);
  306.         PtRequestComplete(pAdapt,
  307.                          &pAdapt->Request,
  308.                          NDIS_STATUS_FAILURE );
  309.     }
  310.     else
  311.     {
  312.         NdisReleaseSpinLock(&pAdapt->Lock);
  313.     }
  314. #ifndef WIN9X
  315.     //
  316.     // Check if we had called NdisIMInitializeDeviceInstanceEx and
  317.     // we are awaiting a call to MiniportInitialize.
  318.     //
  319.     if (pAdapt->MiniportInitPending == TRUE)
  320.     {
  321.         //
  322.         // Try to cancel the pending IMInit process.
  323.         //
  324.         LocalStatus = NdisIMCancelInitializeDeviceInstance(
  325.                         DriverHandle,
  326.                         &pAdapt->DeviceName);
  327.         if (LocalStatus == NDIS_STATUS_SUCCESS)
  328.         {
  329.             //
  330.             // Successfully cancelled IM Initialization; our
  331.             // Miniport Initialize routine will not be called
  332.             // for this device.
  333.             //
  334.             pAdapt->MiniportInitPending = FALSE;
  335.             ASSERT(pAdapt->MiniportHandle == NULL);
  336.         }
  337.         else
  338.         {
  339.             //
  340.             // Our Miniport Initialize routine will be called
  341.             // (may be running on another thread at this time).
  342.             // Wait for it to finish.
  343.             //
  344.             NdisWaitEvent(&pAdapt->MiniportInitEvent, 0);
  345.             ASSERT(pAdapt->MiniportInitPending == FALSE);
  346.         }
  347.     }
  348. #endif // !WIN9X
  349.     //
  350.     // Call NDIS to remove our device-instance. We do most of the work
  351.     // inside the HaltHandler.
  352.     //
  353.     // The Handle will be NULL if our miniport Halt Handler has been called or
  354.     // if the IM device was never initialized
  355.     //
  356.     
  357.     if (pAdapt->MiniportHandle != NULL)
  358.     {
  359.         *Status = NdisIMDeInitializeDeviceInstance(pAdapt->MiniportHandle);
  360.         if (*Status != NDIS_STATUS_SUCCESS)
  361.         {
  362.             *Status = NDIS_STATUS_FAILURE;
  363.         }
  364.     }
  365.     else
  366.     {
  367.         //
  368.         // We need to do some work here. 
  369.         // Close the binding below us 
  370.         // and release the memory allocated.
  371.         //
  372.         if(pAdapt->BindingHandle != NULL)
  373.         {
  374.             NdisResetEvent(&pAdapt->Event);
  375.             NdisCloseAdapter(Status, pAdapt->BindingHandle);
  376.             //
  377.             // Wait for it to complete
  378.             //
  379.             if(*Status == NDIS_STATUS_PENDING)
  380.             {
  381.                  NdisWaitEvent(&pAdapt->Event, 0);
  382.                  *Status = pAdapt->Status;
  383.             }
  384.             pAdapt->BindingHandle = NULL;
  385.         }
  386.         else
  387.         {
  388.             //
  389.             // Both Our MiniportHandle and Binding Handle  should not be NULL.
  390.             //
  391.             *Status = NDIS_STATUS_FAILURE;
  392.             ASSERT(0);
  393.         }
  394.         //
  395.         //    Free the memory here, if was not released earlier(by calling the HaltHandler)
  396.         //
  397.    // BEGIN_PTUSERIO
  398. // 移除协议对适配器的引用
  399. PtDerefAdapter(pAdapt);
  400. // END_PTUSERIO
  401.     }
  402.     DBGPRINT(("<== PtUnbindAdapter: Adapt %pn", pAdapt));
  403. }
  404. VOID
  405. PtUnloadProtocol(
  406.     VOID
  407. )
  408. {
  409.     NDIS_STATUS Status;
  410.     if (ProtHandle != NULL)
  411.     {
  412.         NdisDeregisterProtocol(&Status, ProtHandle);
  413.         ProtHandle = NULL;
  414.     }
  415.     DBGPRINT(("PtUnloadProtocol: done!n"));
  416. }
  417. VOID
  418. PtCloseAdapterComplete(
  419.     IN    NDIS_HANDLE            ProtocolBindingContext,
  420.     IN    NDIS_STATUS            Status
  421.     )
  422. /*++
  423. Routine Description:
  424.     Completion for the CloseAdapter call.
  425. Arguments:
  426.     ProtocolBindingContext    Pointer to the adapter structure
  427.     Status                    Completion status
  428. Return Value:
  429.     None.
  430. --*/
  431. {
  432.     PADAPT      pAdapt =(PADAPT)ProtocolBindingContext;
  433.     DBGPRINT(("CloseAdapterComplete: Adapt %p, Status %xn", pAdapt, Status));
  434.     pAdapt->Status = Status;
  435.     NdisSetEvent(&pAdapt->Event);
  436. }
  437. VOID
  438. PtResetComplete(
  439.     IN  NDIS_HANDLE            ProtocolBindingContext,
  440.     IN  NDIS_STATUS            Status
  441.     )
  442. /*++
  443. Routine Description:
  444.     Completion for the reset.
  445. Arguments:
  446.     ProtocolBindingContext    Pointer to the adapter structure
  447.     Status                    Completion status
  448. Return Value:
  449.     None.
  450. --*/
  451. {
  452.     UNREFERENCED_PARAMETER(ProtocolBindingContext);
  453.     UNREFERENCED_PARAMETER(Status);
  454.     //
  455.     // We never issue a reset, so we should not be here.
  456.     //
  457.     ASSERT(0);
  458. }
  459. VOID
  460. PtRequestComplete(
  461.     IN  NDIS_HANDLE            ProtocolBindingContext,
  462.     IN  PNDIS_REQUEST          NdisRequest,
  463.     IN  NDIS_STATUS            Status
  464.     )
  465. /*++
  466. Routine Description:
  467.     Completion handler for the previously posted request. All OIDS
  468.     are completed by and sent to the same miniport that they were requested for.
  469.     If Oid == OID_PNP_QUERY_POWER then the data structure needs to returned with all entries =
  470.     NdisDeviceStateUnspecified
  471. Arguments:
  472.     ProtocolBindingContext    Pointer to the adapter structure
  473.     NdisRequest                The posted request
  474.     Status                    Completion status
  475. Return Value:
  476.     None
  477. --*/
  478. {
  479.     PADAPT        pAdapt = (PADAPT)ProtocolBindingContext;
  480.     NDIS_OID      Oid = pAdapt->Request.DATA.SET_INFORMATION.Oid ;
  481. // BEGIN_PTUSERIO
  482.    if(NdisRequest != &(pAdapt->Request))
  483.    {
  484.   DevRequestComplete(pAdapt, NdisRequest,Status);
  485.       return;
  486.    }
  487. // END_PTUSERIO
  488.     //
  489.     // Since our request is not outstanding anymore
  490.     //
  491.     ASSERT(pAdapt->OutstandingRequests == TRUE);
  492.     pAdapt->OutstandingRequests = FALSE;
  493.     //
  494.     // Complete the Set or Query, and fill in the buffer for OID_PNP_CAPABILITIES, if need be.
  495.     //
  496.     switch (NdisRequest->RequestType)
  497.     {
  498.       case NdisRequestQueryInformation:
  499.         //
  500.         // We never pass OID_PNP_QUERY_POWER down.
  501.         //
  502.         ASSERT(Oid != OID_PNP_QUERY_POWER);
  503.         if ((Oid == OID_PNP_CAPABILITIES) && (Status == NDIS_STATUS_SUCCESS))
  504.         {
  505.             MPQueryPNPCapabilities(pAdapt, &Status);
  506.         }
  507.         *pAdapt->BytesReadOrWritten = NdisRequest->DATA.QUERY_INFORMATION.BytesWritten;
  508.         *pAdapt->BytesNeeded = NdisRequest->DATA.QUERY_INFORMATION.BytesNeeded;
  509.         if ((Oid == OID_GEN_MAC_OPTIONS) && (Status == NDIS_STATUS_SUCCESS))
  510.         {
  511.             //
  512.             // Remove the no-loopback bit from mac-options. In essence we are
  513.             // telling NDIS that we can handle loopback. We don't, but the
  514.             // interface below us does. If we do not do this, then loopback
  515.             // processing happens both below us and above us. This is wasteful
  516.             // at best and if Netmon is running, it will see multiple copies
  517.             // of loopback packets when sniffing above us.
  518.             //
  519.             // Only the lowest miniport is a stack of layered miniports should
  520.             // ever report this bit set to NDIS.
  521.             //
  522.             *(PULONG)NdisRequest->DATA.QUERY_INFORMATION.InformationBuffer &= ~NDIS_MAC_OPTION_NO_LOOPBACK;
  523.         }
  524.         NdisMQueryInformationComplete(pAdapt->MiniportHandle,
  525.                                       Status);
  526.         break;
  527.       case NdisRequestSetInformation:
  528.         ASSERT( Oid != OID_PNP_SET_POWER);
  529.         *pAdapt->BytesReadOrWritten = NdisRequest->DATA.SET_INFORMATION.BytesRead;
  530.         *pAdapt->BytesNeeded = NdisRequest->DATA.SET_INFORMATION.BytesNeeded;
  531.         NdisMSetInformationComplete(pAdapt->MiniportHandle,
  532.                                     Status);
  533.         break;
  534.       default:
  535.         ASSERT(0);
  536.         break;
  537.     }
  538.     
  539. }
  540. VOID
  541. PtStatus(
  542.     IN  NDIS_HANDLE         ProtocolBindingContext,
  543.     IN  NDIS_STATUS         GeneralStatus,
  544.     IN  PVOID               StatusBuffer,
  545.     IN  UINT                StatusBufferSize
  546.     )
  547. /*++
  548. Routine Description:
  549.     Status handler for the lower-edge(protocol).
  550. Arguments:
  551.     ProtocolBindingContext    Pointer to the adapter structure
  552.     GeneralStatus             Status code
  553.     StatusBuffer              Status buffer
  554.     StatusBufferSize          Size of the status buffer
  555. Return Value:
  556.     None
  557. --*/
  558. {
  559.     PADAPT      pAdapt = (PADAPT)ProtocolBindingContext;
  560.     //
  561.     // Pass up this indication only if the upper edge miniport is initialized
  562.     // and powered on. Also ignore indications that might be sent by the lower
  563.     // miniport when it isn't at D0.
  564.     //
  565.     if ((pAdapt->MiniportHandle != NULL)  &&
  566.         (pAdapt->MPDeviceState == NdisDeviceStateD0) &&
  567.         (pAdapt->PTDeviceState == NdisDeviceStateD0))    
  568.     {
  569.         if ((GeneralStatus == NDIS_STATUS_MEDIA_CONNECT) || 
  570.             (GeneralStatus == NDIS_STATUS_MEDIA_DISCONNECT))
  571.         {
  572.             
  573.             pAdapt->LastIndicatedStatus = GeneralStatus;
  574.         }
  575.         NdisMIndicateStatus(pAdapt->MiniportHandle,
  576.                             GeneralStatus,
  577.                             StatusBuffer,
  578.                             StatusBufferSize);
  579.     }
  580.     //
  581.     // Save the last indicated media status 
  582.     //
  583.     else
  584.     {
  585.         if ((pAdapt->MiniportHandle != NULL) && 
  586.         ((GeneralStatus == NDIS_STATUS_MEDIA_CONNECT) || 
  587.             (GeneralStatus == NDIS_STATUS_MEDIA_DISCONNECT)))
  588.         {
  589.             pAdapt->LatestUnIndicateStatus = GeneralStatus;
  590.         }
  591.     }
  592.     
  593. }
  594. VOID
  595. PtStatusComplete(
  596.     IN NDIS_HANDLE            ProtocolBindingContext
  597.     )
  598. /*++
  599. Routine Description:
  600. Arguments:
  601. Return Value:
  602. --*/
  603. {
  604.     PADAPT      pAdapt = (PADAPT)ProtocolBindingContext;
  605.     //
  606.     // Pass up this indication only if the upper edge miniport is initialized
  607.     // and powered on. Also ignore indications that might be sent by the lower
  608.     // miniport when it isn't at D0.
  609.     //
  610.     if ((pAdapt->MiniportHandle != NULL)  &&
  611.         (pAdapt->MPDeviceState == NdisDeviceStateD0) &&
  612.         (pAdapt->PTDeviceState == NdisDeviceStateD0))    
  613.     {
  614.         NdisMIndicateStatusComplete(pAdapt->MiniportHandle);
  615.     }
  616. }
  617. VOID
  618. PtSendComplete(
  619.     IN  NDIS_HANDLE            ProtocolBindingContext,
  620.     IN  PNDIS_PACKET           Packet,
  621.     IN  NDIS_STATUS            Status
  622.     )
  623. /*++
  624. Routine Description:
  625.     Called by NDIS when the miniport below had completed a send. We should
  626.     complete the corresponding upper-edge send this represents.
  627. Arguments:
  628.     ProtocolBindingContext - Points to ADAPT structure
  629.     Packet - Low level packet being completed
  630.     Status - status of send
  631. Return Value:
  632.     None
  633. --*/
  634. {
  635.     PADAPT            pAdapt = (PADAPT)ProtocolBindingContext;
  636.     PNDIS_PACKET      Pkt; 
  637.     NDIS_HANDLE       PoolHandle;
  638. #ifdef NDIS51
  639.     //
  640.     // Packet stacking:
  641.     //
  642.     // Determine if the packet we are completing is the one we allocated. If so, then
  643.     // get the original packet from the reserved area and completed it and free the
  644.     // allocated packet. If this is the packet that was sent down to us, then just
  645.     // complete it
  646.     //
  647.     PoolHandle = NdisGetPoolFromPacket(Packet);
  648.     if (PoolHandle != pAdapt->SendPacketPoolHandle)
  649.     {
  650.         //
  651.         // We had passed down a packet belonging to the protocol above us.
  652.         //
  653.         // DBGPRINT(("PtSendComp: Adapt %p, Stacked Packet %pn", pAdapt, Packet));
  654.         NdisMSendComplete(pAdapt->MiniportHandle,
  655.                           Packet,
  656.                           Status);
  657.     }
  658.     else
  659. #endif // NDIS51
  660.     {
  661.         PSEND_RSVD        SendRsvd;
  662.         SendRsvd = (PSEND_RSVD)(Packet->ProtocolReserved);
  663.         Pkt = SendRsvd->OriginalPkt;
  664.     
  665. #ifndef WIN9X
  666.         NdisIMCopySendCompletePerPacketInfo (Pkt, Packet);
  667. #endif
  668.     
  669.         NdisDprFreePacket(Packet);
  670.         NdisMSendComplete(pAdapt->MiniportHandle,
  671.                                  Pkt,
  672.                                  Status);
  673.     }
  674.     //
  675.     // Decrease the outstanding send count
  676.     //
  677.     ADAPT_DECR_PENDING_SENDS(pAdapt);
  678. }       
  679. VOID
  680. PtTransferDataComplete(
  681.     IN  NDIS_HANDLE         ProtocolBindingContext,
  682.     IN  PNDIS_PACKET        Packet,
  683.     IN  NDIS_STATUS         Status,
  684.     IN  UINT                BytesTransferred
  685.     )
  686. /*++
  687. Routine Description:
  688.     Entry point called by NDIS to indicate completion of a call by us
  689.     to NdisTransferData.
  690.     See notes under SendComplete.
  691. Arguments:
  692. Return Value:
  693. --*/
  694. {
  695.     PADAPT      pAdapt =(PADAPT)ProtocolBindingContext;
  696.     if(pAdapt->MiniportHandle)
  697.     {
  698.         NdisMTransferDataComplete(pAdapt->MiniportHandle,
  699.                                   Packet,
  700.                                   Status,
  701.                                   BytesTransferred);
  702.     }
  703. }
  704. NDIS_STATUS
  705. PtReceive(
  706.     IN  NDIS_HANDLE         ProtocolBindingContext,
  707.     IN  NDIS_HANDLE         MacReceiveContext,
  708.     IN  PVOID               HeaderBuffer,
  709.     IN  UINT                HeaderBufferSize,
  710.     IN  PVOID               LookAheadBuffer,
  711.     IN  UINT                LookAheadBufferSize,
  712.     IN  UINT                PacketSize
  713.     )
  714. /*++
  715. Routine Description:
  716.     Handle receive data indicated up by the miniport below. We pass
  717.     it along to the protocol above us.
  718.     If the miniport below indicates packets, NDIS would more
  719.     likely call us at our ReceivePacket handler. However we
  720.     might be called here in certain situations even though
  721.     the miniport below has indicated a receive packet, e.g.
  722.     if the miniport had set packet status to NDIS_STATUS_RESOURCES.
  723.         
  724. Arguments:
  725.     <see DDK ref page for ProtocolReceive>
  726. Return Value:
  727.     NDIS_STATUS_SUCCESS if we processed the receive successfully,
  728.     NDIS_STATUS_XXX error code if we discarded it.
  729. --*/
  730. {
  731.     PADAPT            pAdapt = (PADAPT)ProtocolBindingContext;
  732.     PNDIS_PACKET      MyPacket, Packet;
  733.     NDIS_STATUS       Status = NDIS_STATUS_SUCCESS;
  734.     if ((!pAdapt->MiniportHandle) || (pAdapt->MPDeviceState > NdisDeviceStateD0))
  735.     {
  736.         Status = NDIS_STATUS_FAILURE;
  737.     }
  738.     else do
  739.     {
  740. // BEGIN_PTEX_FILTER
  741.         BOOLEAN bPass;
  742.   
  743.         bPass = FltFilterReceive(
  744.                         pAdapt,
  745.                         MacReceiveContext,
  746.                         HeaderBuffer,
  747.                         HeaderBufferSize,
  748.                         LookAheadBuffer,
  749.                         LookAheadBufferSize,
  750.                         PacketSize
  751.                         );
  752. if(!bPass)
  753. {
  754. // 拒绝这个封包
  755. Status = NDIS_STATUS_SUCCESS;
  756.             break;
  757. }
  758.    
  759. // END_PTEX_FILTER
  760.         //
  761.         // Get at the packet, if any, indicated up by the miniport below.
  762.         //
  763.         Packet = NdisGetReceivedPacket(pAdapt->BindingHandle, MacReceiveContext);
  764.         if (Packet != NULL)
  765.         {
  766.             //
  767.             // The miniport below did indicate up a packet. Use information
  768.             // from that packet to construct a new packet to indicate up.
  769.             //
  770. #ifdef NDIS51
  771.             //
  772.             // NDIS 5.1 NOTE: Do not reuse the original packet in indicating
  773.             // up a receive, even if there is sufficient packet stack space.
  774.             // If we had to do so, we would have had to overwrite the
  775.             // status field in the original packet to NDIS_STATUS_RESOURCES,
  776.             // and it is not allowed for protocols to overwrite this field
  777.             // in received packets.
  778.             //
  779. #endif // NDIS51
  780.             //
  781.             // Get a packet off the pool and indicate that up
  782.             //
  783.             NdisDprAllocatePacket(&Status,
  784.                                 &MyPacket,
  785.                                 pAdapt->RecvPacketPoolHandle);
  786.             if (Status == NDIS_STATUS_SUCCESS)
  787.             {
  788.                 //
  789.                 // Make our packet point to data from the original
  790.                 // packet. NOTE: this works only because we are
  791.                 // indicating a receive directly from the context of
  792.                 // our receive indication. If we need to queue this
  793.                 // packet and indicate it from another thread context,
  794.                 // we will also have to allocate a new buffer and copy
  795.                 // over the packet contents, OOB data and per-packet
  796.                 // information. This is because the packet data
  797.                 // is available only for the duration of this
  798.                 // receive indication call.
  799.                 //
  800.                 MyPacket->Private.Head = Packet->Private.Head;
  801.                 MyPacket->Private.Tail = Packet->Private.Tail;
  802.                 //
  803.                 // Get the original packet (it could be the same packet as the
  804.                 // one received or a different one based on the number of layered
  805.                 // miniports below) and set it on the indicated packet so the OOB
  806.                 // data is visible correctly at protocols above.
  807.                 //
  808.                 NDIS_SET_ORIGINAL_PACKET(MyPacket, NDIS_GET_ORIGINAL_PACKET(Packet));
  809.                 NDIS_SET_PACKET_HEADER_SIZE(MyPacket, HeaderBufferSize);
  810.                 //
  811.                 // Copy packet flags.
  812.                 //
  813.                 NdisGetPacketFlags(MyPacket) = NdisGetPacketFlags(Packet);
  814.                 //
  815.                 // Force protocols above to make a copy if they want to hang
  816.                 // on to data in this packet. This is because we are in our
  817.                 // Receive handler (not ReceivePacket) and we can't return a
  818.                 // ref count from here.
  819.                 //
  820.                 NDIS_SET_PACKET_STATUS(MyPacket, NDIS_STATUS_RESOURCES);
  821.                 //
  822.                 // By setting NDIS_STATUS_RESOURCES, we also know that we can reclaim
  823.                 // this packet as soon as the call to NdisMIndicateReceivePacket
  824.                 // returns.
  825.                 //
  826.                 NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &MyPacket, 1);
  827.                 //
  828.                 // Reclaim the indicated packet. Since we had set its status
  829.                 // to NDIS_STATUS_RESOURCES, we are guaranteed that protocols
  830.                 // above are done with it.
  831.                 //
  832.                 NdisDprFreePacket(MyPacket);
  833.                 break;
  834.             }
  835.         }
  836.         else
  837.         {
  838.             //
  839.             // The miniport below us uses the old-style (not packet)
  840.             // receive indication. Fall through.
  841.             //
  842.         }
  843.         //
  844.         // Fall through if the miniport below us has either not
  845.         // indicated a packet or we could not allocate one
  846.         //
  847.         pAdapt->IndicateRcvComplete = TRUE;
  848.         switch (pAdapt->Medium)
  849.         {
  850.             case NdisMedium802_3:
  851.             case NdisMediumWan:
  852.                 NdisMEthIndicateReceive(pAdapt->MiniportHandle,
  853.                                              MacReceiveContext,
  854.                                              HeaderBuffer,
  855.                                              HeaderBufferSize,
  856.                                              LookAheadBuffer,
  857.                                              LookAheadBufferSize,
  858.                                              PacketSize);
  859.                 break;
  860.             case NdisMedium802_5:
  861.                 NdisMTrIndicateReceive(pAdapt->MiniportHandle,
  862.                                             MacReceiveContext,
  863.                                             HeaderBuffer,
  864.                                             HeaderBufferSize,
  865.                                             LookAheadBuffer,
  866.                                             LookAheadBufferSize,
  867.                                             PacketSize);
  868.                 break;
  869.             case NdisMediumFddi:
  870.                 NdisMFddiIndicateReceive(pAdapt->MiniportHandle,
  871.                                               MacReceiveContext,
  872.                                               HeaderBuffer,
  873.                                               HeaderBufferSize,
  874.                                               LookAheadBuffer,
  875.                                               LookAheadBufferSize,
  876.                                               PacketSize);
  877.                 break;
  878.             default:
  879.                 ASSERT(FALSE);
  880.                 break;
  881.         }
  882.     } while(FALSE);
  883.     return Status;
  884. }
  885. VOID
  886. PtReceiveComplete(
  887.     IN NDIS_HANDLE        ProtocolBindingContext
  888.     )
  889. /*++
  890. Routine Description:
  891.     Called by the adapter below us when it is done indicating a batch of
  892.     received packets.
  893. Arguments:
  894.     ProtocolBindingContext    Pointer to our adapter structure.
  895. Return Value:
  896.     None
  897. --*/
  898. {
  899.     PADAPT        pAdapt =(PADAPT)ProtocolBindingContext;
  900.     if (((pAdapt->MiniportHandle != NULL)
  901.                 && (pAdapt->MPDeviceState > NdisDeviceStateD0))
  902.                 && (pAdapt->IndicateRcvComplete))
  903.     {
  904.         switch (pAdapt->Medium)
  905.         {
  906.             case NdisMedium802_3:
  907.             case NdisMediumWan:
  908.                 NdisMEthIndicateReceiveComplete(pAdapt->MiniportHandle);
  909.                 break;
  910.             case NdisMedium802_5:
  911.                 NdisMTrIndicateReceiveComplete(pAdapt->MiniportHandle);
  912.                 break;
  913.             case NdisMediumFddi:
  914.                 NdisMFddiIndicateReceiveComplete(pAdapt->MiniportHandle);
  915.                 break;
  916.             default:
  917.                 ASSERT(FALSE);
  918.                 break;
  919.         }
  920.     }
  921.     pAdapt->IndicateRcvComplete = FALSE;
  922. }
  923. INT
  924. PtReceivePacket(
  925.     IN NDIS_HANDLE            ProtocolBindingContext,
  926.     IN PNDIS_PACKET           Packet
  927.     )
  928. /*++
  929. Routine Description:
  930.     ReceivePacket handler. Called by NDIS if the miniport below supports
  931.     NDIS 4.0 style receives. Re-package the buffer chain in a new packet
  932.     and indicate the new packet to protocols above us. Any context for
  933.     packets indicated up must be kept in the MiniportReserved field.
  934.     NDIS 5.1 - packet stacking - if there is sufficient "stack space" in
  935.     the packet passed to us, we can use the same packet in a receive
  936.     indication.
  937. Arguments:
  938.     ProtocolBindingContext - Pointer to our adapter structure.
  939.     Packet - Pointer to the packet
  940. Return Value:
  941.     == 0 -> We are done with the packet
  942.     != 0 -> We will keep the packet and call NdisReturnPackets() this
  943.             many times when done.
  944. --*/
  945. {
  946.     PADAPT              pAdapt =(PADAPT)ProtocolBindingContext;
  947.     NDIS_STATUS         Status;
  948.     PNDIS_PACKET        MyPacket;
  949.     BOOLEAN             Remaining;
  950.     //
  951.     // Drop the packet silently if the upper miniport edge isn't initialized or
  952.     // the miniport edge is in low power state
  953.     //
  954.     if ((!pAdapt->MiniportHandle) || (pAdapt->MPDeviceState > NdisDeviceStateD0))
  955.     {
  956.           return 0;
  957.     }
  958. // BEGIN_PTEX_FILTER
  959.    //
  960.    // 调用过滤接收封包的函数
  961.    //
  962.   if(!FltFilterReceivePacket( pAdapt, Packet ))
  963.   return 0;
  964. // END_PTEX_FILTER
  965. #ifdef NDIS51
  966.     //
  967.     // Check if we can reuse the same packet for indicating up.
  968.     // See also: PtReceive(). 
  969.     //
  970.     (VOID)NdisIMGetCurrentPacketStack(Packet, &Remaining);
  971.     if (Remaining)
  972.     {
  973.         //
  974.         // We can reuse "Packet". Indicate it up and be done with it.
  975.         //
  976.         Status = NDIS_GET_PACKET_STATUS(Packet);
  977.         NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &Packet, 1);
  978.         return((Status != NDIS_STATUS_RESOURCES) ? 1 : 0);
  979.     }
  980. #endif // NDIS51
  981.     //
  982.     // Get a packet off the pool and indicate that up
  983.     //
  984.     NdisDprAllocatePacket(&Status,
  985.                            &MyPacket,
  986.                            pAdapt->RecvPacketPoolHandle);
  987.     if (Status == NDIS_STATUS_SUCCESS)
  988.     {
  989.         PRECV_RSVD            RecvRsvd;
  990.         RecvRsvd = (PRECV_RSVD)(MyPacket->MiniportReserved);
  991.         RecvRsvd->OriginalPkt = Packet;
  992.         MyPacket->Private.Head = Packet->Private.Head;
  993.         MyPacket->Private.Tail = Packet->Private.Tail;
  994.         //
  995.         // Get the original packet (it could be the same packet as the one
  996.         // received or a different one based on the number of layered miniports
  997.         // below) and set it on the indicated packet so the OOB data is visible
  998.         // correctly to protocols above us.
  999.         //
  1000.         NDIS_SET_ORIGINAL_PACKET(MyPacket, NDIS_GET_ORIGINAL_PACKET(Packet));
  1001.         //
  1002.         // Set Packet Flags
  1003.         //
  1004.         NdisGetPacketFlags(MyPacket) = NdisGetPacketFlags(Packet);
  1005.         Status = NDIS_GET_PACKET_STATUS(Packet);
  1006.         NDIS_SET_PACKET_STATUS(MyPacket, Status);
  1007.         NDIS_SET_PACKET_HEADER_SIZE(MyPacket, NDIS_GET_PACKET_HEADER_SIZE(Packet));
  1008.         NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &MyPacket, 1);
  1009.         //
  1010.         // Check if we had indicated up the packet with NDIS_STATUS_RESOURCES
  1011.         // NOTE -- do not use NDIS_GET_PACKET_STATUS(MyPacket) for this since
  1012.         // it might have changed! Use the value saved in the local variable.
  1013.         //
  1014.         if (Status == NDIS_STATUS_RESOURCES)
  1015.         {
  1016.             //
  1017.             // Our ReturnPackets handler will not be called for this packet.
  1018.             // We should reclaim it right here.
  1019.             //
  1020.             NdisDprFreePacket(MyPacket);
  1021.         }
  1022.         return((Status != NDIS_STATUS_RESOURCES) ? 1 : 0);
  1023.     }
  1024.     else
  1025.     {
  1026.         //
  1027.         // We are out of packets. Silently drop it.
  1028.         //
  1029.         return(0);
  1030.     }
  1031. }
  1032. NDIS_STATUS
  1033. PtPNPHandler(
  1034.     IN NDIS_HANDLE        ProtocolBindingContext,
  1035.     IN PNET_PNP_EVENT     pNetPnPEvent
  1036.     )
  1037. /*++
  1038. Routine Description:
  1039.     This is called by NDIS to notify us of a PNP event related to a lower
  1040.     binding. Based on the event, this dispatches to other helper routines.
  1041.     NDIS 5.1: forward this event to the upper protocol(s) by calling
  1042.     NdisIMNotifyPnPEvent.
  1043. Arguments:
  1044.     ProtocolBindingContext - Pointer to our adapter structure. Can be NULL
  1045.                 for "global" notifications
  1046.     pNetPnPEvent - Pointer to the PNP event to be processed.
  1047. Return Value:
  1048.     NDIS_STATUS code indicating status of event processing.
  1049. --*/
  1050. {
  1051.     PADAPT            pAdapt  =(PADAPT)ProtocolBindingContext;
  1052.     NDIS_STATUS       Status  = NDIS_STATUS_SUCCESS;
  1053.     DBGPRINT(("PtPnPHandler: Adapt %p, Event %dn", pAdapt, pNetPnPEvent->NetEvent));
  1054.     switch (pNetPnPEvent->NetEvent)
  1055.     {
  1056.         case NetEventSetPower:
  1057.             Status = PtPnPNetEventSetPower(pAdapt, pNetPnPEvent);
  1058.             break;
  1059.          case NetEventReconfigure:
  1060.             Status = PtPnPNetEventReconfigure(pAdapt, pNetPnPEvent);
  1061.             break;
  1062.          default:
  1063. #ifdef NDIS51
  1064.             //
  1065.             // Pass on this notification to protocol(s) above, before
  1066.             // doing anything else with it.
  1067.             //
  1068.             if (pAdapt && pAdapt->MiniportHandle)
  1069.             {
  1070.                 Status = NdisIMNotifyPnPEvent(pAdapt->MiniportHandle, pNetPnPEvent);
  1071.             }
  1072. #else
  1073.             Status = NDIS_STATUS_SUCCESS;
  1074. #endif // NDIS51
  1075.             break;
  1076.     }
  1077.     return Status;
  1078. }
  1079. NDIS_STATUS
  1080. PtPnPNetEventReconfigure(
  1081.     IN PADAPT            pAdapt,
  1082.     IN PNET_PNP_EVENT    pNetPnPEvent
  1083.     )
  1084. /*++
  1085. Routine Description:
  1086.     This routine is called from NDIS to notify our protocol edge of a
  1087.     reconfiguration of parameters for either a specific binding (pAdapt
  1088.     is not NULL), or global parameters if any (pAdapt is NULL).
  1089. Arguments:
  1090.     pAdapt - Pointer to our adapter structure.
  1091.     pNetPnPEvent - the reconfigure event
  1092. Return Value:
  1093.     NDIS_STATUS_SUCCESS
  1094. --*/
  1095. {
  1096.     NDIS_STATUS    ReconfigStatus = NDIS_STATUS_SUCCESS;
  1097.     NDIS_STATUS    ReturnStatus = NDIS_STATUS_SUCCESS;
  1098.     do
  1099.     {
  1100.         //
  1101.         // Is this is a global reconfiguration notification ?
  1102.         //
  1103.         if (pAdapt == NULL)
  1104.         {
  1105.             //
  1106.             // An important event that causes this notification to us is if
  1107.             // one of our upper-edge miniport instances was enabled after being
  1108.             // disabled earlier, e.g. from Device Manager in Win2000. Note that
  1109.             // NDIS calls this because we had set up an association between our
  1110.             // miniport and protocol entities by calling NdisIMAssociateMiniport.
  1111.             //
  1112.             // Since we would have torn down the lower binding for that miniport,
  1113.             // we need NDIS' assistance to re-bind to the lower miniport. The
  1114.             // call to NdisReEnumerateProtocolBindings does exactly that.
  1115.             //
  1116.             NdisReEnumerateProtocolBindings (ProtHandle);        
  1117.             break;
  1118.         }
  1119. #ifdef NDIS51
  1120.         //
  1121.         // Pass on this notification to protocol(s) above before doing anything
  1122.         // with it.
  1123.         //
  1124.         if (pAdapt->MiniportHandle)
  1125.         {
  1126.             ReturnStatus = NdisIMNotifyPnPEvent(pAdapt->MiniportHandle, pNetPnPEvent);
  1127.         }
  1128. #endif // NDIS51
  1129.         ReconfigStatus = NDIS_STATUS_SUCCESS;
  1130.     } while(FALSE);
  1131.     DBGPRINT(("<==PtPNPNetEventReconfigure: pAdapt %pn", pAdapt));
  1132. #ifdef NDIS51
  1133.     //
  1134.     // Overwrite status with what upper-layer protocol(s) returned.
  1135.     //
  1136.     ReconfigStatus = ReturnStatus;
  1137. #endif
  1138.     return ReconfigStatus;
  1139. }
  1140. NDIS_STATUS
  1141. PtPnPNetEventSetPower(
  1142.     IN PADAPT            pAdapt,
  1143.     IN PNET_PNP_EVENT    pNetPnPEvent
  1144.     )
  1145. /*++
  1146. Routine Description:
  1147.     This is a notification to our protocol edge of the power state
  1148.     of the lower miniport. If it is going to a low-power state, we must
  1149.     wait here for all outstanding sends and requests to complete.
  1150.     NDIS 5.1:  Since we use packet stacking, it is not sufficient to
  1151.     check usage of our local send packet pool to detect whether or not
  1152.     all outstanding sends have completed. For this, use the new API
  1153.     NdisQueryPendingIOCount.
  1154.     NDIS 5.1: Use the 5.1 API NdisIMNotifyPnPEvent to pass on PnP
  1155.     notifications to upper protocol(s).
  1156. Arguments:
  1157.     pAdapt            -    Pointer to the adpater structure
  1158.     pNetPnPEvent    -    The Net Pnp Event. this contains the new device state
  1159. Return Value:
  1160.     NDIS_STATUS_SUCCESS or the status returned by upper-layer protocols.
  1161. --*/
  1162. {
  1163.     PNDIS_DEVICE_POWER_STATE       pDeviceState  =(PNDIS_DEVICE_POWER_STATE)(pNetPnPEvent->Buffer);
  1164.     NDIS_DEVICE_POWER_STATE        PrevDeviceState = pAdapt->PTDeviceState;  
  1165.     NDIS_STATUS                    Status;
  1166.     NDIS_STATUS                    ReturnStatus;
  1167. #ifdef NDIS51
  1168.     ULONG                          PendingIoCount = 0;
  1169. #endif // NDIS51
  1170.     ReturnStatus = NDIS_STATUS_SUCCESS;
  1171.     //
  1172.     // Set the Internal Device State, this blocks all new sends or receives
  1173.     //
  1174.     NdisAcquireSpinLock(&pAdapt->Lock);
  1175.     pAdapt->PTDeviceState = *pDeviceState;
  1176.     //
  1177.     // Check if the miniport below is going to a low power state.
  1178.     //
  1179.     if (pAdapt->PTDeviceState > NdisDeviceStateD0)
  1180.     {
  1181.         //
  1182.         // If the miniport below is going to standby, fail all incoming requests
  1183.         //
  1184.         if (PrevDeviceState == NdisDeviceStateD0)
  1185.         {
  1186.             pAdapt->StandingBy = TRUE;
  1187.         }
  1188.         NdisReleaseSpinLock(&pAdapt->Lock);
  1189. #ifdef NDIS51
  1190.         //
  1191.         // Notify upper layer protocol(s) first.
  1192.         //
  1193.         if (pAdapt->MiniportHandle != NULL)
  1194.         {
  1195.             ReturnStatus = NdisIMNotifyPnPEvent(pAdapt->MiniportHandle, pNetPnPEvent);
  1196.         }
  1197. #endif // NDIS51
  1198.         //
  1199.         // Wait for outstanding sends and requests to complete.
  1200.         //
  1201.         while (pAdapt->OutstandingSends != 0)
  1202.         {
  1203.             NdisMSleep(2);
  1204.         }
  1205.         while (pAdapt->OutstandingRequests == TRUE)
  1206.         {
  1207.             //
  1208.             // sleep till outstanding requests complete
  1209.             //
  1210.             NdisMSleep(2);
  1211.         }
  1212.         //
  1213.         // If the below miniport is going to low power state, complete the queued request
  1214.         //
  1215.         NdisAcquireSpinLock(&pAdapt->Lock);
  1216.         if (pAdapt->QueuedRequest)
  1217.         {
  1218.             pAdapt->QueuedRequest = FALSE;
  1219.             NdisReleaseSpinLock(&pAdapt->Lock);
  1220.             PtRequestComplete(pAdapt, &pAdapt->Request, NDIS_STATUS_FAILURE);
  1221.         }
  1222.         else
  1223.         {
  1224.             NdisReleaseSpinLock(&pAdapt->Lock);
  1225.         }
  1226.             
  1227.         ASSERT(NdisPacketPoolUsage(pAdapt->SendPacketPoolHandle) == 0);
  1228.         ASSERT(pAdapt->OutstandingRequests == FALSE);
  1229.     }
  1230.     else
  1231.     {
  1232.         //
  1233.         // If the physical miniport is powering up (from Low power state to D0), 
  1234.         // clear the flag
  1235.         //
  1236.         if (PrevDeviceState > NdisDeviceStateD0)
  1237.         {
  1238.             pAdapt->StandingBy = FALSE;
  1239.         }
  1240.         //
  1241.         // The device below is being turned on. If we had a request
  1242.         // pending, send it down now.
  1243.         //
  1244.         if (pAdapt->QueuedRequest == TRUE)
  1245.         {
  1246.             pAdapt->QueuedRequest = FALSE;
  1247.         
  1248.             pAdapt->OutstandingRequests = TRUE;
  1249.             NdisReleaseSpinLock(&pAdapt->Lock);
  1250.             NdisRequest(&Status,
  1251.                         pAdapt->BindingHandle,
  1252.                         &pAdapt->Request);
  1253.             if (Status != NDIS_STATUS_PENDING)
  1254.             {
  1255.                 PtRequestComplete(pAdapt,
  1256.                                   &pAdapt->Request,
  1257.                                   Status);
  1258.                 
  1259.             }
  1260.         }
  1261.         else
  1262.         {
  1263.             NdisReleaseSpinLock(&pAdapt->Lock);
  1264.         }
  1265. #ifdef NDIS51
  1266.         //
  1267.         // Pass on this notification to protocol(s) above
  1268.         //
  1269.         if (pAdapt->MiniportHandle)
  1270.         {
  1271.             ReturnStatus = NdisIMNotifyPnPEvent(pAdapt->MiniportHandle, pNetPnPEvent);
  1272.         }
  1273. #endif // NDIS51
  1274.     }
  1275.     return ReturnStatus;
  1276. }