3 Copyright (c) 1992-2000 Microsoft Corporation
11 Ndis Intermediate Miniport driver sample. This is a passthru driver.
30 OUT PNDIS_STATUS OpenErrorStatus,
31 OUT PUINT SelectedMediumIndex,
32 IN PNDIS_MEDIUM MediumArray,
33 IN UINT MediumArraySize,
34 IN NDIS_HANDLE MiniportAdapterHandle,
35 IN NDIS_HANDLE WrapperConfigurationContext
41 This is the initialize handler which gets called as a result of
42 the BindAdapter handler calling NdisIMInitializeDeviceInstanceEx.
43 The context parameter which we pass there is the adapter structure
44 which we retrieve here.
48 OpenErrorStatus Not used by us.
49 SelectedMediumIndex Place-holder for what media we are using
50 MediumArray Array of ndis media passed down to us to pick from
51 MediumArraySize Size of the array
52 MiniportAdapterHandle The handle NDIS uses to refer to us
53 WrapperConfigurationContext For use by NdisOpenConfiguration
57 NDIS_STATUS_SUCCESS unless something goes wrong
63 NDIS_STATUS Status = NDIS_STATUS_FAILURE;
66 UNREFERENCED_PARAMETER(WrapperConfigurationContext);
71 // Start off by retrieving our adapter context and storing
72 // the Miniport handle in it.
74 pAdapt = NdisIMGetDeviceContext(MiniportAdapterHandle);
75 pAdapt->MiniportIsHalted = FALSE;
77 DBGPRINT(("==> Miniport Initialize: Adapt %p\n", pAdapt));
80 // Usually we export the medium type of the adapter below as our
81 // virtual miniport's medium type. However if the adapter below us
82 // is a WAN device, then we claim to be of medium type 802.3.
84 Medium = pAdapt->Medium;
86 if (Medium == NdisMediumWan)
88 Medium = NdisMedium802_3;
91 for (i = 0; i < MediumArraySize; i++)
93 if (MediumArray[i] == Medium)
95 *SelectedMediumIndex = i;
100 if (i == MediumArraySize)
102 Status = NDIS_STATUS_UNSUPPORTED_MEDIA;
108 // Set the attributes now. NDIS_ATTRIBUTE_DESERIALIZE enables us
109 // to make up-calls to NDIS without having to call NdisIMSwitchToMiniport
110 // or NdisIMQueueCallBack. This also forces us to protect our data using
111 // spinlocks where appropriate. Also in this case NDIS does not queue
112 // packets on our behalf. Since this is a very simple pass-thru
113 // miniport, we do not have a need to protect anything. However in
114 // a general case there will be a need to use per-adapter spin-locks
115 // for the packet queues at the very least.
117 NdisMSetAttributesEx(MiniportAdapterHandle,
119 0, // CheckForHangTimeInSeconds
120 NDIS_ATTRIBUTE_IGNORE_PACKET_TIMEOUT |
121 NDIS_ATTRIBUTE_IGNORE_REQUEST_TIMEOUT|
122 NDIS_ATTRIBUTE_INTERMEDIATE_DRIVER |
123 NDIS_ATTRIBUTE_DESERIALIZE |
124 NDIS_ATTRIBUTE_NO_HALT_ON_SUSPEND,
127 pAdapt->MiniportHandle = MiniportAdapterHandle;
129 // Initialize LastIndicatedStatus to be NDIS_STATUS_MEDIA_CONNECT
131 pAdapt->LastIndicatedStatus = NDIS_STATUS_MEDIA_CONNECT;
134 // Initialize the power states for both the lower binding (PTDeviceState)
135 // and our miniport edge to Powered On.
137 pAdapt->MPDeviceState = NdisDeviceStateD0;
138 pAdapt->PTDeviceState = NdisDeviceStateD0;
141 // Add this adapter to the global pAdapt List
143 NdisAcquireSpinLock(&GlobalLock);
145 pAdapt->Next = pAdaptList;
148 NdisReleaseSpinLock(&GlobalLock);
151 // Create an ioctl interface
153 (VOID)PtRegisterDevice();
155 Status = NDIS_STATUS_SUCCESS;
160 // If we had received an UnbindAdapter notification on the underlying
161 // adapter, we would have blocked that thread waiting for the IM Init
162 // process to complete. Wake up any such thread.
164 ASSERT(pAdapt->MiniportInitPending == TRUE);
165 pAdapt->MiniportInitPending = FALSE;
166 NdisSetEvent(&pAdapt->MiniportInitEvent);
168 if (Status == NDIS_STATUS_SUCCESS)
170 PtReferenceAdapt(pAdapt);
173 DBGPRINT(("<== Miniport Initialize: Adapt %p, Status %x\n", pAdapt, Status));
175 *OpenErrorStatus = Status;
184 IN NDIS_HANDLE MiniportAdapterContext,
185 IN PNDIS_PACKET Packet,
192 Send Packet handler. Either this or our SendPackets (array) handler is called
193 based on which one is enabled in our Miniport Characteristics.
197 MiniportAdapterContext Pointer to the adapter
198 Packet Packet to send
199 Flags Unused, passed down below
203 Return code from NdisSend
207 PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
209 PNDIS_PACKET MyPacket;
210 PVOID MediaSpecificInfo = NULL;
211 ULONG MediaSpecificInfoSize = 0;
214 // The driver should fail the send if the virtual miniport is in low
217 if (pAdapt->MPDeviceState > NdisDeviceStateD0)
219 return NDIS_STATUS_FAILURE;
224 // Use NDIS 5.1 packet stacking:
226 if (0) // XXX IPFW - make sure we don't go in here
228 PNDIS_PACKET_STACK pStack;
232 // Packet stacks: Check if we can use the same packet for sending down.
235 pStack = NdisIMGetCurrentPacketStack(Packet, &Remaining);
239 // We can reuse "Packet".
241 // NOTE: if we needed to keep per-packet information in packets
242 // sent down, we can use pStack->IMReserved[].
246 // If the below miniport is going to low power state, stop sending down any packet.
248 NdisAcquireSpinLock(&pAdapt->Lock);
249 if (pAdapt->PTDeviceState > NdisDeviceStateD0)
251 NdisReleaseSpinLock(&pAdapt->Lock);
252 return NDIS_STATUS_FAILURE;
254 pAdapt->OutstandingSends++;
255 NdisReleaseSpinLock(&pAdapt->Lock);
257 pAdapt->BindingHandle,
260 if (Status != NDIS_STATUS_PENDING)
262 ADAPT_DECR_PENDING_SENDS(pAdapt);
271 // We are either not using packet stacks, or there isn't stack space
272 // in the original packet passed down to us. Allocate a new packet
273 // to wrap the data with.
276 // If the below miniport is going to low power state, stop sending down any packet.
278 NdisAcquireSpinLock(&pAdapt->Lock);
279 if (pAdapt->PTDeviceState > NdisDeviceStateD0)
281 NdisReleaseSpinLock(&pAdapt->Lock);
282 return NDIS_STATUS_FAILURE;
285 pAdapt->OutstandingSends++;
286 NdisReleaseSpinLock(&pAdapt->Lock);
288 NdisAllocatePacket(&Status,
290 pAdapt->SendPacketPoolHandle);
292 if (Status == NDIS_STATUS_SUCCESS)
297 // Save a pointer to the original packet in our reserved
298 // area in the new packet. This is needed so that we can
299 // get back to the original packet when the new packet's send
302 SendRsvd = (PSEND_RSVD)(MyPacket->ProtocolReserved);
303 SendRsvd->OriginalPkt = Packet;
305 NdisGetPacketFlags(MyPacket) = Flags;
308 // Set up the new packet so that it describes the same
309 // data as the original packet.
311 NDIS_PACKET_FIRST_NDIS_BUFFER(MyPacket) = NDIS_PACKET_FIRST_NDIS_BUFFER(Packet);
312 NDIS_PACKET_LAST_NDIS_BUFFER(MyPacket) = NDIS_PACKET_LAST_NDIS_BUFFER(Packet);
315 // Work around the fact that NDIS does not initialize this
316 // to FALSE on Win9x.
318 NDIS_PACKET_VALID_COUNTS(MyPacket) = FALSE;
322 // Copy the OOB Offset from the original packet to the new
325 NdisMoveMemory(NDIS_OOB_DATA_FROM_PACKET(MyPacket),
326 NDIS_OOB_DATA_FROM_PACKET(Packet),
327 sizeof(NDIS_PACKET_OOB_DATA));
331 // Copy the right parts of per packet info into the new packet.
332 // This API is not available on Win9x since task offload is
333 // not supported on that platform.
335 NdisIMCopySendPerPacketInfo(MyPacket, Packet);
339 // Copy the Media specific information
341 NDIS_GET_PACKET_MEDIA_SPECIFIC_INFO(Packet,
343 &MediaSpecificInfoSize);
345 if (MediaSpecificInfo || MediaSpecificInfoSize)
347 NDIS_SET_PACKET_MEDIA_SPECIFIC_INFO(MyPacket,
349 MediaSpecificInfoSize);
351 #if 1 /* IPFW: query the firewall */
352 /* if dummynet keeps the packet, we mimic success.
353 * otherwise continue as usual.
356 int ret = ipfw2_qhandler_w32(MyPacket, OUTGOING,
357 MiniportAdapterContext);
360 return NDIS_STATUS_FAILURE;
361 else { //dummynet kept the packet
363 NdisIMCopySendCompletePerPacketInfo (Packet, MyPacket);
365 return NDIS_STATUS_SUCCESS; //otherwise simply continue
369 #endif /* end of IPFW code */
372 pAdapt->BindingHandle,
376 if (Status != NDIS_STATUS_PENDING)
379 NdisIMCopySendCompletePerPacketInfo (Packet, MyPacket);
381 NdisFreePacket(MyPacket);
382 ADAPT_DECR_PENDING_SENDS(pAdapt);
387 ADAPT_DECR_PENDING_SENDS(pAdapt);
389 // We are out of packets. Silently drop it. Alternatively we can deal with it:
390 // - By keeping separate send and receive pools
391 // - Dynamically allocate more pools as needed and free them when not needed
401 IN NDIS_HANDLE MiniportAdapterContext,
402 IN PPNDIS_PACKET PacketArray,
403 IN UINT NumberOfPackets
409 Send Packet Array handler. Either this or our SendPacket handler is called
410 based on which one is enabled in our Miniport Characteristics.
414 MiniportAdapterContext Pointer to our adapter
415 PacketArray Set of packets to send
416 NumberOfPackets Self-explanatory
424 PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
427 PVOID MediaSpecificInfo = NULL;
428 UINT MediaSpecificInfoSize = 0;
431 for (i = 0; i < NumberOfPackets; i++)
433 PNDIS_PACKET Packet, MyPacket;
435 Packet = PacketArray[i];
437 // The driver should fail the send if the virtual miniport is in low
440 if (pAdapt->MPDeviceState > NdisDeviceStateD0)
442 NdisMSendComplete(ADAPT_MINIPORT_HANDLE(pAdapt),
444 NDIS_STATUS_FAILURE);
451 // Use NDIS 5.1 packet stacking:
454 PNDIS_PACKET_STACK pStack;
458 // Packet stacks: Check if we can use the same packet for sending down.
460 pStack = NdisIMGetCurrentPacketStack(Packet, &Remaining);
464 // We can reuse "Packet".
466 // NOTE: if we needed to keep per-packet information in packets
467 // sent down, we can use pStack->IMReserved[].
471 // If the below miniport is going to low power state, stop sending down any packet.
473 NdisAcquireSpinLock(&pAdapt->Lock);
474 if (pAdapt->PTDeviceState > NdisDeviceStateD0)
476 NdisReleaseSpinLock(&pAdapt->Lock);
477 NdisMSendComplete(ADAPT_MINIPORT_HANDLE(pAdapt),
479 NDIS_STATUS_FAILURE);
483 pAdapt->OutstandingSends++;
484 NdisReleaseSpinLock(&pAdapt->Lock);
487 pAdapt->BindingHandle,
490 if (Status != NDIS_STATUS_PENDING)
492 NdisMSendComplete(ADAPT_MINIPORT_HANDLE(pAdapt),
496 ADAPT_DECR_PENDING_SENDS(pAdapt);
505 NdisAcquireSpinLock(&pAdapt->Lock);
507 // If the below miniport is going to low power state, stop sending down any packet.
509 if (pAdapt->PTDeviceState > NdisDeviceStateD0)
511 NdisReleaseSpinLock(&pAdapt->Lock);
512 Status = NDIS_STATUS_FAILURE;
515 pAdapt->OutstandingSends++;
516 NdisReleaseSpinLock(&pAdapt->Lock);
518 NdisAllocatePacket(&Status,
520 pAdapt->SendPacketPoolHandle);
522 if (Status == NDIS_STATUS_SUCCESS)
526 SendRsvd = (PSEND_RSVD)(MyPacket->ProtocolReserved);
527 SendRsvd->OriginalPkt = Packet;
529 NdisGetPacketFlags(MyPacket) = NdisGetPacketFlags(Packet);
531 NDIS_PACKET_FIRST_NDIS_BUFFER(MyPacket) = NDIS_PACKET_FIRST_NDIS_BUFFER(Packet);
532 NDIS_PACKET_LAST_NDIS_BUFFER(MyPacket) = NDIS_PACKET_LAST_NDIS_BUFFER(Packet);
535 // Work around the fact that NDIS does not initialize this
536 // to FALSE on Win9x.
538 NDIS_PACKET_VALID_COUNTS(MyPacket) = FALSE;
542 // Copy the OOB data from the original packet to the new
545 NdisMoveMemory(NDIS_OOB_DATA_FROM_PACKET(MyPacket),
546 NDIS_OOB_DATA_FROM_PACKET(Packet),
547 sizeof(NDIS_PACKET_OOB_DATA));
549 // Copy relevant parts of the per packet info into the new packet
552 NdisIMCopySendPerPacketInfo(MyPacket, Packet);
556 // Copy the Media specific information
558 NDIS_GET_PACKET_MEDIA_SPECIFIC_INFO(Packet,
560 &MediaSpecificInfoSize);
562 if (MediaSpecificInfo || MediaSpecificInfoSize)
564 NDIS_SET_PACKET_MEDIA_SPECIFIC_INFO(MyPacket,
566 MediaSpecificInfoSize);
570 pAdapt->BindingHandle,
573 if (Status != NDIS_STATUS_PENDING)
576 NdisIMCopySendCompletePerPacketInfo (Packet, MyPacket);
578 NdisFreePacket(MyPacket);
579 ADAPT_DECR_PENDING_SENDS(pAdapt);
585 // The driver cannot allocate a packet.
587 ADAPT_DECR_PENDING_SENDS(pAdapt);
592 if (Status != NDIS_STATUS_PENDING)
594 NdisMSendComplete(ADAPT_MINIPORT_HANDLE(pAdapt),
604 IN NDIS_HANDLE MiniportAdapterContext,
606 IN PVOID InformationBuffer,
607 IN ULONG InformationBufferLength,
608 OUT PULONG BytesWritten,
609 OUT PULONG BytesNeeded
615 Entry point called by NDIS to query for the value of the specified OID.
616 Typical processing is to forward the query down to the underlying miniport.
618 The following OIDs are filtered here:
620 OID_PNP_QUERY_POWER - return success right here
622 OID_GEN_SUPPORTED_GUIDS - do not forward, otherwise we will show up
623 multiple instances of private GUIDs supported by the underlying miniport.
625 OID_PNP_CAPABILITIES - we do send this down to the lower miniport, but
626 the values returned are postprocessed before we complete this request;
627 see PtRequestComplete.
629 NOTE on OID_TCP_TASK_OFFLOAD - if this IM driver modifies the contents
630 of data it passes through such that a lower miniport may not be able
631 to perform TCP task offload, then it should not forward this OID down,
632 but fail it here with the status NDIS_STATUS_NOT_SUPPORTED. This is to
633 avoid performing incorrect transformations on data.
635 If our miniport edge (upper edge) is at a low-power state, fail the request.
637 If our protocol edge (lower edge) has been notified of a low-power state,
638 we pend this request until the miniport below has been set to D0. Since
639 requests to miniports are serialized always, at most a single request will
644 MiniportAdapterContext Pointer to the adapter structure
645 Oid Oid for this query
646 InformationBuffer Buffer for information
647 InformationBufferLength Size of this buffer
648 BytesWritten Specifies how much info is written
649 BytesNeeded In case the buffer is smaller than what we need, tell them how much is needed
654 Return code from the NdisRequest below.
658 PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
659 NDIS_STATUS Status = NDIS_STATUS_FAILURE;
663 if (Oid == OID_PNP_QUERY_POWER)
666 // Do not forward this.
668 Status = NDIS_STATUS_SUCCESS;
672 if (Oid == OID_GEN_SUPPORTED_GUIDS)
675 // Do not forward this, otherwise we will end up with multiple
676 // instances of private GUIDs that the underlying miniport
679 Status = NDIS_STATUS_NOT_SUPPORTED;
683 if (Oid == OID_TCP_TASK_OFFLOAD)
686 // Fail this -if- this driver performs data transformations
687 // that can interfere with a lower driver's ability to offload
690 // Status = NDIS_STATUS_NOT_SUPPORTED;
695 // If the miniport below is unbinding, just fail any request
697 NdisAcquireSpinLock(&pAdapt->Lock);
698 if (pAdapt->UnbindingInProcess == TRUE)
700 NdisReleaseSpinLock(&pAdapt->Lock);
701 Status = NDIS_STATUS_FAILURE;
704 NdisReleaseSpinLock(&pAdapt->Lock);
706 // All other queries are failed, if the miniport is not at D0,
708 if (pAdapt->MPDeviceState > NdisDeviceStateD0)
710 Status = NDIS_STATUS_FAILURE;
714 pAdapt->Request.RequestType = NdisRequestQueryInformation;
715 pAdapt->Request.DATA.QUERY_INFORMATION.Oid = Oid;
716 pAdapt->Request.DATA.QUERY_INFORMATION.InformationBuffer = InformationBuffer;
717 pAdapt->Request.DATA.QUERY_INFORMATION.InformationBufferLength = InformationBufferLength;
718 pAdapt->BytesNeeded = BytesNeeded;
719 pAdapt->BytesReadOrWritten = BytesWritten;
722 // If the miniport below is binding, fail the request
724 NdisAcquireSpinLock(&pAdapt->Lock);
726 if (pAdapt->UnbindingInProcess == TRUE)
728 NdisReleaseSpinLock(&pAdapt->Lock);
729 Status = NDIS_STATUS_FAILURE;
733 // If the Protocol device state is OFF, mark this request as being
734 // pended. We queue this until the device state is back to D0.
736 if ((pAdapt->PTDeviceState > NdisDeviceStateD0)
737 && (pAdapt->StandingBy == FALSE))
739 pAdapt->QueuedRequest = TRUE;
740 NdisReleaseSpinLock(&pAdapt->Lock);
741 Status = NDIS_STATUS_PENDING;
745 // This is in the process of powering down the system, always fail the request
747 if (pAdapt->StandingBy == TRUE)
749 NdisReleaseSpinLock(&pAdapt->Lock);
750 Status = NDIS_STATUS_FAILURE;
753 pAdapt->OutstandingRequests = TRUE;
755 NdisReleaseSpinLock(&pAdapt->Lock);
758 // default case, most requests will be passed to the miniport below
761 pAdapt->BindingHandle,
765 if (Status != NDIS_STATUS_PENDING)
767 PtRequestComplete(pAdapt, &pAdapt->Request, Status);
768 Status = NDIS_STATUS_PENDING;
779 MPQueryPNPCapabilities(
780 IN OUT PADAPT pAdapt,
781 OUT PNDIS_STATUS pStatus
787 Postprocess a request for OID_PNP_CAPABILITIES that was forwarded
788 down to the underlying miniport, and has been completed by it.
792 pAdapt - Pointer to the adapter structure
793 pStatus - Place to return final status
802 PNDIS_PNP_CAPABILITIES pPNPCapabilities;
803 PNDIS_PM_WAKE_UP_CAPABILITIES pPMstruct;
805 if (pAdapt->Request.DATA.QUERY_INFORMATION.InformationBufferLength >= sizeof(NDIS_PNP_CAPABILITIES))
807 pPNPCapabilities = (PNDIS_PNP_CAPABILITIES)(pAdapt->Request.DATA.QUERY_INFORMATION.InformationBuffer);
810 // The following fields must be overwritten by an IM driver.
812 pPMstruct= & pPNPCapabilities->WakeUpCapabilities;
813 pPMstruct->MinMagicPacketWakeUp = NdisDeviceStateUnspecified;
814 pPMstruct->MinPatternWakeUp = NdisDeviceStateUnspecified;
815 pPMstruct->MinLinkChangeWakeUp = NdisDeviceStateUnspecified;
816 *pAdapt->BytesReadOrWritten = sizeof(NDIS_PNP_CAPABILITIES);
817 *pAdapt->BytesNeeded = 0;
821 // Setting our internal flags
822 // Default, device is ON
824 pAdapt->MPDeviceState = NdisDeviceStateD0;
825 pAdapt->PTDeviceState = NdisDeviceStateD0;
827 *pStatus = NDIS_STATUS_SUCCESS;
831 *pAdapt->BytesNeeded= sizeof(NDIS_PNP_CAPABILITIES);
832 *pStatus = NDIS_STATUS_RESOURCES;
839 IN NDIS_HANDLE MiniportAdapterContext,
841 __in_bcount(InformationBufferLength) IN PVOID InformationBuffer,
842 IN ULONG InformationBufferLength,
843 OUT PULONG BytesRead,
844 OUT PULONG BytesNeeded
850 Miniport SetInfo handler.
852 In the case of OID_PNP_SET_POWER, record the power state and return the OID.
854 If the device is suspended, do not block the SET_POWER_OID
855 as it is used to reactivate the Passthru miniport
858 PM- If the MP is not ON (DeviceState > D0) return immediately (except for 'query power' and 'set power')
859 If MP is ON, but the PT is not at D0, then queue the queue the request for later processing
861 Requests to miniports are always serialized
866 MiniportAdapterContext Pointer to the adapter structure
867 Oid Oid for this query
868 InformationBuffer Buffer for information
869 InformationBufferLength Size of this buffer
870 BytesRead Specifies how much info is read
871 BytesNeeded In case the buffer is smaller than what we need, tell them how much is needed
875 Return code from the NdisRequest below.
879 PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
882 Status = NDIS_STATUS_FAILURE;
887 // The Set Power should not be sent to the miniport below the Passthru, but is handled internally
889 if (Oid == OID_PNP_SET_POWER)
891 MPProcessSetPowerOid(&Status,
894 InformationBufferLength,
902 // If the miniport below is unbinding, fail the request
904 NdisAcquireSpinLock(&pAdapt->Lock);
905 if (pAdapt->UnbindingInProcess == TRUE)
907 NdisReleaseSpinLock(&pAdapt->Lock);
908 Status = NDIS_STATUS_FAILURE;
911 NdisReleaseSpinLock(&pAdapt->Lock);
913 // All other Set Information requests are failed, if the miniport is
914 // not at D0 or is transitioning to a device state greater than D0.
916 if (pAdapt->MPDeviceState > NdisDeviceStateD0)
918 Status = NDIS_STATUS_FAILURE;
922 // Set up the Request and return the result
923 pAdapt->Request.RequestType = NdisRequestSetInformation;
924 pAdapt->Request.DATA.SET_INFORMATION.Oid = Oid;
925 pAdapt->Request.DATA.SET_INFORMATION.InformationBuffer = InformationBuffer;
926 pAdapt->Request.DATA.SET_INFORMATION.InformationBufferLength = InformationBufferLength;
927 pAdapt->BytesNeeded = BytesNeeded;
928 pAdapt->BytesReadOrWritten = BytesRead;
931 // If the miniport below is unbinding, fail the request
933 NdisAcquireSpinLock(&pAdapt->Lock);
934 if (pAdapt->UnbindingInProcess == TRUE)
936 NdisReleaseSpinLock(&pAdapt->Lock);
937 Status = NDIS_STATUS_FAILURE;
942 // If the device below is at a low power state, we cannot send it the
943 // request now, and must pend it.
945 if ((pAdapt->PTDeviceState > NdisDeviceStateD0)
946 && (pAdapt->StandingBy == FALSE))
948 pAdapt->QueuedRequest = TRUE;
949 NdisReleaseSpinLock(&pAdapt->Lock);
950 Status = NDIS_STATUS_PENDING;
954 // This is in the process of powering down the system, always fail the request
956 if (pAdapt->StandingBy == TRUE)
958 NdisReleaseSpinLock(&pAdapt->Lock);
959 Status = NDIS_STATUS_FAILURE;
962 pAdapt->OutstandingRequests = TRUE;
964 NdisReleaseSpinLock(&pAdapt->Lock);
966 // Forward the request to the device below.
969 pAdapt->BindingHandle,
972 if (Status != NDIS_STATUS_PENDING)
974 *BytesRead = pAdapt->Request.DATA.SET_INFORMATION.BytesRead;
975 *BytesNeeded = pAdapt->Request.DATA.SET_INFORMATION.BytesNeeded;
976 pAdapt->OutstandingRequests = FALSE;
986 MPProcessSetPowerOid(
987 IN OUT PNDIS_STATUS pNdisStatus,
989 __in_bcount(InformationBufferLength) IN PVOID InformationBuffer,
990 IN ULONG InformationBufferLength,
991 OUT PULONG BytesRead,
992 OUT PULONG BytesNeeded
997 This routine does all the procssing for a request with a SetPower Oid
998 The miniport shoud accept the Set Power and transition to the new state
1000 The Set Power should not be passed to the miniport below
1002 If the IM miniport is going into a low power state, then there is no guarantee if it will ever
1003 be asked go back to D0, before getting halted. No requests should be pended or queued.
1007 pNdisStatus - Status of the operation
1008 pAdapt - The Adapter structure
1009 InformationBuffer - The New DeviceState
1010 InformationBufferLength
1011 BytesRead - No of bytes read
1012 BytesNeeded - No of bytes needed
1016 Status - NDIS_STATUS_SUCCESS if all the wait events succeed.
1022 NDIS_DEVICE_POWER_STATE NewDeviceState;
1024 DBGPRINT(("==>MPProcessSetPowerOid: Adapt %p\n", pAdapt));
1026 ASSERT (InformationBuffer != NULL);
1028 *pNdisStatus = NDIS_STATUS_FAILURE;
1033 // Check for invalid length
1035 if (InformationBufferLength < sizeof(NDIS_DEVICE_POWER_STATE))
1037 *pNdisStatus = NDIS_STATUS_INVALID_LENGTH;
1041 NewDeviceState = (*(PNDIS_DEVICE_POWER_STATE)InformationBuffer);
1044 // Check for invalid device state
1046 if ((pAdapt->MPDeviceState > NdisDeviceStateD0) && (NewDeviceState != NdisDeviceStateD0))
1049 // If the miniport is in a non-D0 state, the miniport can only receive a Set Power to D0
1051 ASSERT (!(pAdapt->MPDeviceState > NdisDeviceStateD0) && (NewDeviceState != NdisDeviceStateD0));
1053 *pNdisStatus = NDIS_STATUS_FAILURE;
1058 // Is the miniport transitioning from an On (D0) state to an Low Power State (>D0)
1059 // If so, then set the StandingBy Flag - (Block all incoming requests)
1061 if (pAdapt->MPDeviceState == NdisDeviceStateD0 && NewDeviceState > NdisDeviceStateD0)
1063 pAdapt->StandingBy = TRUE;
1067 // If the miniport is transitioning from a low power state to ON (D0), then clear the StandingBy flag
1068 // All incoming requests will be pended until the physical miniport turns ON.
1070 if (pAdapt->MPDeviceState > NdisDeviceStateD0 && NewDeviceState == NdisDeviceStateD0)
1072 pAdapt->StandingBy = FALSE;
1076 // Now update the state in the pAdapt structure;
1078 pAdapt->MPDeviceState = NewDeviceState;
1080 *pNdisStatus = NDIS_STATUS_SUCCESS;
1085 if (*pNdisStatus == NDIS_STATUS_SUCCESS)
1088 // The miniport resume from low power state
1090 if (pAdapt->StandingBy == FALSE)
1093 // If we need to indicate the media connect state
1095 if (pAdapt->LastIndicatedStatus != pAdapt->LatestUnIndicateStatus)
1097 if (pAdapt->MiniportHandle != NULL)
1099 NdisMIndicateStatus(pAdapt->MiniportHandle,
1100 pAdapt->LatestUnIndicateStatus,
1103 NdisMIndicateStatusComplete(pAdapt->MiniportHandle);
1104 pAdapt->LastIndicatedStatus = pAdapt->LatestUnIndicateStatus;
1111 // Initialize LatestUnIndicatedStatus
1113 pAdapt->LatestUnIndicateStatus = pAdapt->LastIndicatedStatus;
1115 *BytesRead = sizeof(NDIS_DEVICE_POWER_STATE);
1121 *BytesNeeded = sizeof (NDIS_DEVICE_POWER_STATE);
1124 DBGPRINT(("<==MPProcessSetPowerOid: Adapt %p\n", pAdapt));
1130 IN NDIS_HANDLE MiniportAdapterContext,
1131 IN PNDIS_PACKET Packet
1135 Routine Description:
1137 NDIS Miniport entry point called whenever protocols are done with
1138 a packet that we had indicated up and they had queued up for returning
1143 MiniportAdapterContext - pointer to ADAPT structure
1144 Packet - packet being returned.
1152 PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
1156 // Packet stacking: Check if this packet belongs to us.
1158 if (NdisGetPoolFromPacket(Packet) != pAdapt->RecvPacketPoolHandle)
1161 // We reused the original packet in a receive indication.
1162 // Simply return it to the miniport below us.
1164 NdisReturnPackets(&Packet, 1);
1170 // This is a packet allocated from this IM's receive packet pool.
1171 // Reclaim our packet, and return the original to the driver below.
1174 PNDIS_PACKET MyPacket;
1175 PRECV_RSVD RecvRsvd;
1177 RecvRsvd = (PRECV_RSVD)(Packet->MiniportReserved);
1178 MyPacket = RecvRsvd->OriginalPkt;
1180 NdisFreePacket(Packet);
1181 NdisReturnPackets(&MyPacket, 1);
1188 OUT PNDIS_PACKET Packet,
1189 OUT PUINT BytesTransferred,
1190 IN NDIS_HANDLE MiniportAdapterContext,
1191 IN NDIS_HANDLE MiniportReceiveContext,
1193 IN UINT BytesToTransfer
1197 Routine Description:
1199 Miniport's transfer data handler.
1203 Packet Destination packet
1204 BytesTransferred Place-holder for how much data was copied
1205 MiniportAdapterContext Pointer to the adapter structure
1206 MiniportReceiveContext Context
1207 ByteOffset Offset into the packet for copying data
1208 BytesToTransfer How much to copy.
1216 PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
1220 // Return, if the device is OFF
1223 if (IsIMDeviceStateOn(pAdapt) == FALSE)
1225 return NDIS_STATUS_FAILURE;
1228 NdisTransferData(&Status,
1229 pAdapt->BindingHandle,
1230 MiniportReceiveContext,
1241 IN NDIS_HANDLE MiniportAdapterContext
1245 Routine Description:
1247 Halt handler. All the hard-work for clean-up is done here.
1251 MiniportAdapterContext Pointer to the Adapter
1259 PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
1263 DBGPRINT(("==>MiniportHalt: Adapt %p\n", pAdapt));
1265 pAdapt->MiniportHandle = NULL;
1266 pAdapt->MiniportIsHalted = TRUE;
1269 // Remove this adapter from the global list
1271 NdisAcquireSpinLock(&GlobalLock);
1273 for (ppCursor = &pAdaptList; *ppCursor != NULL; ppCursor = &(*ppCursor)->Next)
1275 if (*ppCursor == pAdapt)
1277 *ppCursor = pAdapt->Next;
1282 NdisReleaseSpinLock(&GlobalLock);
1285 // Delete the ioctl interface that was created when the miniport
1288 (VOID)PtDeregisterDevice();
1291 // If we have a valid bind, close the miniport below the protocol
1293 #pragma prefast(suppress: __WARNING_DEREF_NULL_PTR, "pAdapt cannot be NULL")
1294 if (pAdapt->BindingHandle != NULL)
1297 // Close the binding below. and wait for it to complete
1299 NdisResetEvent(&pAdapt->Event);
1301 NdisCloseAdapter(&Status, pAdapt->BindingHandle);
1303 if (Status == NDIS_STATUS_PENDING)
1305 NdisWaitEvent(&pAdapt->Event, 0);
1306 Status = pAdapt->Status;
1309 ASSERT (Status == NDIS_STATUS_SUCCESS);
1311 pAdapt->BindingHandle = NULL;
1313 PtDereferenceAdapt(pAdapt);
1316 if (PtDereferenceAdapt(pAdapt))
1322 DBGPRINT(("<== MiniportHalt: pAdapt %p\n", pAdapt));
1326 #ifdef NDIS51_MINIPORT
1329 MPCancelSendPackets(
1330 IN NDIS_HANDLE MiniportAdapterContext,
1335 Routine Description:
1337 The miniport entry point to handle cancellation of all send packets
1338 that match the given CancelId. If we have queued any packets that match
1339 this, then we should dequeue them and call NdisMSendComplete for all
1340 such packets, with a status of NDIS_STATUS_REQUEST_ABORTED.
1342 We should also call NdisCancelSendPackets in turn, on each lower binding
1343 that this adapter corresponds to. This is to let miniports below cancel
1344 any matching packets.
1348 MiniportAdapterContext - pointer to ADAPT structure
1349 CancelId - ID of packets to be cancelled.
1357 PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
1360 // If we queue packets on our adapter structure, this would be
1361 // the place to acquire a spinlock to it, unlink any packets whose
1362 // Id matches CancelId, release the spinlock and call NdisMSendComplete
1363 // with NDIS_STATUS_REQUEST_ABORTED for all unlinked packets.
1367 // Next, pass this down so that we let the miniport(s) below cancel
1368 // any packets that they might have queued.
1370 NdisCancelSendPackets(pAdapt->BindingHandle, CancelId);
1377 IN NDIS_HANDLE MiniportAdapterContext,
1378 IN NDIS_DEVICE_PNP_EVENT DevicePnPEvent,
1379 IN PVOID InformationBuffer,
1380 IN ULONG InformationBufferLength
1384 Routine Description:
1386 This handler is called to notify us of PnP events directed to
1387 our miniport device object.
1391 MiniportAdapterContext - pointer to ADAPT structure
1392 DevicePnPEvent - the event
1393 InformationBuffer - Points to additional event-specific information
1394 InformationBufferLength - length of above
1401 // TBD - add code/comments about processing this.
1403 UNREFERENCED_PARAMETER(MiniportAdapterContext);
1404 UNREFERENCED_PARAMETER(DevicePnPEvent);
1405 UNREFERENCED_PARAMETER(InformationBuffer);
1406 UNREFERENCED_PARAMETER(InformationBufferLength);
1413 IN NDIS_HANDLE MiniportAdapterContext
1417 Routine Description:
1419 This handler is called to notify us of an impending system shutdown.
1423 MiniportAdapterContext - pointer to ADAPT structure
1430 UNREFERENCED_PARAMETER(MiniportAdapterContext);
1439 MPFreeAllPacketPools(
1444 Routine Description:
1446 Free all packet pools on the specified adapter.
1450 pAdapt - pointer to ADAPT structure
1458 if (pAdapt->RecvPacketPoolHandle != NULL)
1461 // Free the packet pool that is used to indicate receives
1463 NdisFreePacketPool(pAdapt->RecvPacketPoolHandle);
1465 pAdapt->RecvPacketPoolHandle = NULL;
1468 if (pAdapt->SendPacketPoolHandle != NULL)
1472 // Free the packet pool that is used to send packets below
1475 NdisFreePacketPool(pAdapt->SendPacketPoolHandle);
1477 pAdapt->SendPacketPoolHandle = NULL;