PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/west-chamber-windows/WestChamberWindows/WestChamberWindows/protocol.c

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