PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/win/protocol.c

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