Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1994 lines
54 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. ndisbind.c
  5. Abstract:
  6. NDIS protocol entry points and utility routines to handle binding
  7. and unbinding from adapters.
  8. Environment:
  9. Kernel mode only.
  10. Revision History:
  11. arvindm 4/5/2000 Created
  12. --*/
  13. #include "precomp.h"
  14. #define __FILENUMBER 'DNIB'
  15. NDIS_OID ndisuioSupportedSetOids[] =
  16. {
  17. OID_802_11_INFRASTRUCTURE_MODE,
  18. OID_802_11_AUTHENTICATION_MODE,
  19. OID_802_11_RELOAD_DEFAULTS,
  20. OID_802_11_REMOVE_WEP,
  21. OID_802_11_WEP_STATUS,
  22. OID_802_11_BSSID_LIST_SCAN,
  23. OID_802_11_ADD_WEP,
  24. OID_802_11_SSID,
  25. OID_802_11_BSSID,
  26. OID_802_11_BSSID_LIST,
  27. OID_802_11_DISASSOCIATE,
  28. OID_802_11_STATISTICS, // Later used by power management
  29. OID_802_11_POWER_MODE, // Later used by power management
  30. OID_802_11_NETWORK_TYPE_IN_USE,
  31. OID_802_11_RSSI,
  32. OID_802_11_SUPPORTED_RATES,
  33. OID_802_11_CONFIGURATION,
  34. OID_802_3_MULTICAST_LIST
  35. };
  36. VOID
  37. NdisuioBindAdapter(
  38. OUT PNDIS_STATUS pStatus,
  39. IN NDIS_HANDLE BindContext,
  40. IN PNDIS_STRING pDeviceName,
  41. IN PVOID SystemSpecific1,
  42. IN PVOID SystemSpecific2
  43. )
  44. /*++
  45. Routine Description:
  46. Protocol Bind Handler entry point called when NDIS wants us
  47. to bind to an adapter. We go ahead and set up a binding.
  48. An OPEN_CONTEXT structure is allocated to keep state about
  49. this binding.
  50. Arguments:
  51. pStatus - place to return bind status
  52. BindContext - handle to use with NdisCompleteBindAdapter
  53. DeviceName - adapter to bind to
  54. SystemSpecific1 - used to access protocol-specific registry
  55. key for this binding
  56. SystemSpecific2 - unused
  57. Return Value:
  58. None
  59. --*/
  60. {
  61. PNDISUIO_OPEN_CONTEXT pOpenContext;
  62. NDIS_STATUS Status, ConfigStatus;
  63. NDIS_HANDLE ConfigHandle;
  64. UNREFERENCED_PARAMETER(BindContext);
  65. UNREFERENCED_PARAMETER(SystemSpecific2);
  66. do
  67. {
  68. //
  69. // Allocate our context for this open.
  70. //
  71. NUIO_ALLOC_MEM(pOpenContext, sizeof(NDISUIO_OPEN_CONTEXT));
  72. if (pOpenContext == NULL)
  73. {
  74. Status = NDIS_STATUS_RESOURCES;
  75. break;
  76. }
  77. //
  78. // Initialize it.
  79. //
  80. NUIO_ZERO_MEM(pOpenContext, sizeof(NDISUIO_OPEN_CONTEXT));
  81. NUIO_SET_SIGNATURE(pOpenContext, oc);
  82. NUIO_INIT_LOCK(&pOpenContext->Lock);
  83. NUIO_INIT_LIST_HEAD(&pOpenContext->PendedReads);
  84. NUIO_INIT_LIST_HEAD(&pOpenContext->PendedWrites);
  85. NUIO_INIT_LIST_HEAD(&pOpenContext->RecvPktQueue);
  86. NUIO_INIT_EVENT(&pOpenContext->PoweredUpEvent);
  87. //
  88. // Start off by assuming that the device below is powered up.
  89. //
  90. NUIO_SIGNAL_EVENT(&pOpenContext->PoweredUpEvent);
  91. //
  92. // Determine the platform we are running on.
  93. //
  94. pOpenContext->bRunningOnWin9x = TRUE;
  95. NdisOpenProtocolConfiguration(
  96. &ConfigStatus,
  97. &ConfigHandle,
  98. (PNDIS_STRING)SystemSpecific1);
  99. if (ConfigStatus == NDIS_STATUS_SUCCESS)
  100. {
  101. PNDIS_CONFIGURATION_PARAMETER pParameter;
  102. NDIS_STRING VersionKey = NDIS_STRING_CONST("Environment");
  103. NdisReadConfiguration(
  104. &ConfigStatus,
  105. &pParameter,
  106. ConfigHandle,
  107. &VersionKey,
  108. NdisParameterInteger);
  109. if ((ConfigStatus == NDIS_STATUS_SUCCESS) &&
  110. ((pParameter->ParameterType == NdisParameterInteger) ||
  111. (pParameter->ParameterType == NdisParameterHexInteger)))
  112. {
  113. pOpenContext->bRunningOnWin9x =
  114. (pParameter->ParameterData.IntegerData == NdisEnvironmentWindows);
  115. }
  116. NdisCloseConfiguration(ConfigHandle);
  117. }
  118. NUIO_REF_OPEN(pOpenContext); // Bind
  119. //
  120. // Add it to the global list.
  121. //
  122. NUIO_ACQUIRE_LOCK(&Globals.GlobalLock);
  123. NUIO_INSERT_TAIL_LIST(&Globals.OpenList,
  124. &pOpenContext->Link);
  125. NUIO_RELEASE_LOCK(&Globals.GlobalLock);
  126. //
  127. // Set up the NDIS binding.
  128. //
  129. Status = ndisuioCreateBinding(
  130. pOpenContext,
  131. (PUCHAR)pDeviceName->Buffer,
  132. pDeviceName->Length);
  133. if (Status != NDIS_STATUS_SUCCESS)
  134. {
  135. break;
  136. }
  137. }
  138. while (FALSE);
  139. *pStatus = Status;
  140. return;
  141. }
  142. VOID
  143. NdisuioOpenAdapterComplete(
  144. IN NDIS_HANDLE ProtocolBindingContext,
  145. IN NDIS_STATUS Status,
  146. IN NDIS_STATUS OpenErrorCode
  147. )
  148. /*++
  149. Routine Description:
  150. Completion routine called by NDIS if our call to NdisOpenAdapter
  151. pends. Wake up the thread that called NdisOpenAdapter.
  152. Arguments:
  153. ProtocolBindingContext - pointer to open context structure
  154. Status - status of the open
  155. OpenErrorCode - if unsuccessful, additional information
  156. Return Value:
  157. None
  158. --*/
  159. {
  160. PNDISUIO_OPEN_CONTEXT pOpenContext;
  161. UNREFERENCED_PARAMETER(OpenErrorCode);
  162. pOpenContext = (PNDISUIO_OPEN_CONTEXT)ProtocolBindingContext;
  163. NUIO_STRUCT_ASSERT(pOpenContext, oc);
  164. pOpenContext->BindStatus = Status;
  165. NUIO_SIGNAL_EVENT(&pOpenContext->BindEvent);
  166. }
  167. VOID
  168. NdisuioUnbindAdapter(
  169. OUT PNDIS_STATUS pStatus,
  170. IN NDIS_HANDLE ProtocolBindingContext,
  171. IN NDIS_HANDLE UnbindContext
  172. )
  173. /*++
  174. Routine Description:
  175. NDIS calls this when it wants us to close the binding to an adapter.
  176. Arguments:
  177. pStatus - place to return status of Unbind
  178. ProtocolBindingContext - pointer to open context structure
  179. UnbindContext - to use in NdisCompleteUnbindAdapter if we return pending
  180. Return Value:
  181. None
  182. --*/
  183. {
  184. PNDISUIO_OPEN_CONTEXT pOpenContext;
  185. UNREFERENCED_PARAMETER(UnbindContext);
  186. pOpenContext = (PNDISUIO_OPEN_CONTEXT)ProtocolBindingContext;
  187. NUIO_STRUCT_ASSERT(pOpenContext, oc);
  188. //
  189. // Mark this open as having seen an Unbind.
  190. //
  191. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  192. NUIO_SET_FLAGS(pOpenContext->Flags, NUIOO_UNBIND_FLAGS, NUIOO_UNBIND_RECEIVED);
  193. //
  194. // In case we had threads blocked for the device below to be powered
  195. // up, wake them up.
  196. //
  197. NUIO_SIGNAL_EVENT(&pOpenContext->PoweredUpEvent);
  198. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  199. ndisuioShutdownBinding(pOpenContext);
  200. *pStatus = NDIS_STATUS_SUCCESS;
  201. return;
  202. }
  203. VOID
  204. NdisuioCloseAdapterComplete(
  205. IN NDIS_HANDLE ProtocolBindingContext,
  206. IN NDIS_STATUS Status
  207. )
  208. /*++
  209. Routine Description:
  210. Called by NDIS to complete a pended call to NdisCloseAdapter.
  211. We wake up the thread waiting for this completion.
  212. Arguments:
  213. ProtocolBindingContext - pointer to open context structure
  214. Status - Completion status of NdisCloseAdapter
  215. Return Value:
  216. None
  217. --*/
  218. {
  219. PNDISUIO_OPEN_CONTEXT pOpenContext;
  220. pOpenContext = (PNDISUIO_OPEN_CONTEXT)ProtocolBindingContext;
  221. NUIO_STRUCT_ASSERT(pOpenContext, oc);
  222. pOpenContext->BindStatus = Status;
  223. NUIO_SIGNAL_EVENT(&pOpenContext->BindEvent);
  224. }
  225. NDIS_STATUS
  226. NdisuioPnPEventHandler(
  227. IN NDIS_HANDLE ProtocolBindingContext,
  228. IN PNET_PNP_EVENT pNetPnPEvent
  229. )
  230. /*++
  231. Routine Description:
  232. Called by NDIS to notify us of a PNP event. The most significant
  233. one for us is power state change.
  234. Arguments:
  235. ProtocolBindingContext - pointer to open context structure
  236. this is NULL for global reconfig events.
  237. pNetPnPEvent - pointer to the PNP event
  238. Return Value:
  239. Our processing status for the PNP event.
  240. --*/
  241. {
  242. PNDISUIO_OPEN_CONTEXT pOpenContext;
  243. NDIS_STATUS Status;
  244. pOpenContext = (PNDISUIO_OPEN_CONTEXT)ProtocolBindingContext;
  245. switch (pNetPnPEvent->NetEvent)
  246. {
  247. case NetEventSetPower:
  248. NUIO_STRUCT_ASSERT(pOpenContext, oc);
  249. pOpenContext->PowerState = *(PNET_DEVICE_POWER_STATE)pNetPnPEvent->Buffer;
  250. if (pOpenContext->PowerState > NetDeviceStateD0)
  251. {
  252. //
  253. // The device below is transitioning to a low power state.
  254. // Block any threads attempting to query the device while
  255. // in this state.
  256. //
  257. NUIO_INIT_EVENT(&pOpenContext->PoweredUpEvent);
  258. //
  259. // Wait for any I/O in progress to complete.
  260. //
  261. ndisuioWaitForPendingIO(pOpenContext, FALSE);
  262. //
  263. // Return any receives that we had queued up.
  264. //
  265. ndisuioFlushReceiveQueue(pOpenContext);
  266. DEBUGP(DL_INFO, ("PnPEvent: Open %p, SetPower to %d\n",
  267. pOpenContext, pOpenContext->PowerState));
  268. }
  269. else
  270. {
  271. //
  272. // The device below is powered up.
  273. //
  274. DEBUGP(DL_INFO, ("PnPEvent: Open %p, SetPower ON: %d\n",
  275. pOpenContext, pOpenContext->PowerState));
  276. NUIO_SIGNAL_EVENT(&pOpenContext->PoweredUpEvent);
  277. }
  278. Status = NDIS_STATUS_SUCCESS;
  279. break;
  280. case NetEventQueryPower:
  281. Status = NDIS_STATUS_SUCCESS;
  282. break;
  283. case NetEventBindsComplete:
  284. NUIO_SIGNAL_EVENT(&Globals.BindsComplete);
  285. Status = NDIS_STATUS_SUCCESS;
  286. break;
  287. case NetEventQueryRemoveDevice:
  288. case NetEventCancelRemoveDevice:
  289. case NetEventReconfigure:
  290. case NetEventBindList:
  291. case NetEventPnPCapabilities:
  292. Status = NDIS_STATUS_SUCCESS;
  293. break;
  294. default:
  295. Status = NDIS_STATUS_NOT_SUPPORTED;
  296. break;
  297. }
  298. DEBUGP(DL_INFO, ("PnPEvent: Open %p, Event %d, Status %x\n",
  299. pOpenContext, pNetPnPEvent->NetEvent, Status));
  300. return (Status);
  301. }
  302. VOID
  303. NdisuioProtocolUnloadHandler(
  304. VOID
  305. )
  306. /*++
  307. Routine Description:
  308. NDIS calls this on a usermode request to uninstall us.
  309. Arguments:
  310. None
  311. Return Value:
  312. None
  313. --*/
  314. {
  315. ndisuioDoProtocolUnload();
  316. }
  317. NDIS_STATUS
  318. ndisuioCreateBinding(
  319. IN PNDISUIO_OPEN_CONTEXT pOpenContext,
  320. IN PUCHAR pBindingInfo,
  321. IN ULONG BindingInfoLength
  322. )
  323. /*++
  324. Routine Description:
  325. Utility function to create an NDIS binding to the indicated device,
  326. if no such binding exists.
  327. Here is where we also allocate additional resources (e.g. packet pool)
  328. for the binding.
  329. Things to take care of:
  330. 1. Is another thread doing this (or has finished binding) already?
  331. 2. Is the binding being closed at this time?
  332. 3. NDIS calls our Unbind handler while we are doing this.
  333. These precautions are not needed if this routine is only called from
  334. the context of our BindAdapter handler, but they are here in case
  335. we initiate binding from elsewhere (e.g. on processing a user command).
  336. NOTE: this function blocks and finishes synchronously.
  337. Arguments:
  338. pOpenContext - pointer to open context block
  339. pBindingInfo - pointer to unicode device name string
  340. BindingInfoLength - length in bytes of the above.
  341. Return Value:
  342. NDIS_STATUS_SUCCESS if a binding was successfully set up.
  343. NDIS_STATUS_XXX error code on any failure.
  344. --*/
  345. {
  346. NDIS_STATUS Status;
  347. NDIS_STATUS OpenErrorCode;
  348. NDIS_MEDIUM MediumArray[1] = {NdisMedium802_3};
  349. UINT SelectedMediumIndex;
  350. PNDISUIO_OPEN_CONTEXT pTmpOpenContext;
  351. BOOLEAN fDoNotDisturb = FALSE;
  352. BOOLEAN fOpenComplete = FALSE;
  353. ULONG BytesProcessed;
  354. ULONG GenericUlong = 0;
  355. DEBUGP(DL_LOUD, ("CreateBinding: open %p/%x, device [%ws]\n",
  356. pOpenContext, pOpenContext->Flags, pBindingInfo));
  357. Status = NDIS_STATUS_SUCCESS;
  358. do
  359. {
  360. //
  361. // Check if we already have a binding to this device.
  362. //
  363. pTmpOpenContext = ndisuioLookupDevice(pBindingInfo, BindingInfoLength);
  364. if (pTmpOpenContext != NULL)
  365. {
  366. DEBUGP(DL_WARN,
  367. ("CreateBinding: Binding to device %ws already exists on open %p\n",
  368. pTmpOpenContext->DeviceName.Buffer, pTmpOpenContext));
  369. NUIO_DEREF_OPEN(pTmpOpenContext); // temp ref added by Lookup
  370. Status = NDIS_STATUS_FAILURE;
  371. break;
  372. }
  373. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  374. //
  375. // Check if this open context is already bound/binding/closing.
  376. //
  377. if (!NUIO_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_IDLE) ||
  378. NUIO_TEST_FLAGS(pOpenContext->Flags, NUIOO_UNBIND_FLAGS, NUIOO_UNBIND_RECEIVED))
  379. {
  380. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  381. Status = NDIS_STATUS_NOT_ACCEPTED;
  382. //
  383. // Make sure we don't abort this binding on failure cleanup.
  384. //
  385. fDoNotDisturb = TRUE;
  386. break;
  387. }
  388. NUIO_SET_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_OPENING);
  389. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  390. //
  391. // Copy in the device name. Add room for a NULL terminator.
  392. //
  393. NUIO_ALLOC_MEM(pOpenContext->DeviceName.Buffer, BindingInfoLength + sizeof(WCHAR));
  394. if (pOpenContext->DeviceName.Buffer == NULL)
  395. {
  396. DEBUGP(DL_WARN, ("CreateBinding: failed to alloc device name buf (%d bytes)\n",
  397. BindingInfoLength + sizeof(WCHAR)));
  398. Status = NDIS_STATUS_RESOURCES;
  399. break;
  400. }
  401. NUIO_COPY_MEM(pOpenContext->DeviceName.Buffer, pBindingInfo, BindingInfoLength);
  402. *(PWCHAR)((PUCHAR)pOpenContext->DeviceName.Buffer + BindingInfoLength) = L'\0';
  403. NdisInitUnicodeString(&pOpenContext->DeviceName, pOpenContext->DeviceName.Buffer);
  404. //
  405. // Allocate packet pools.
  406. //
  407. NdisAllocatePacketPoolEx(
  408. &Status,
  409. &pOpenContext->SendPacketPool,
  410. MIN_SEND_PACKET_POOL_SIZE,
  411. MAX_SEND_PACKET_POOL_SIZE - MIN_SEND_PACKET_POOL_SIZE,
  412. sizeof(NUIO_SEND_PACKET_RSVD));
  413. if (Status != NDIS_STATUS_SUCCESS)
  414. {
  415. DEBUGP(DL_WARN, ("CreateBinding: failed to alloc"
  416. " send packet pool: %x\n", Status));
  417. break;
  418. }
  419. NdisAllocatePacketPoolEx(
  420. &Status,
  421. &pOpenContext->RecvPacketPool,
  422. MIN_RECV_PACKET_POOL_SIZE,
  423. MAX_RECV_PACKET_POOL_SIZE - MIN_RECV_PACKET_POOL_SIZE,
  424. sizeof(NUIO_RECV_PACKET_RSVD));
  425. if (Status != NDIS_STATUS_SUCCESS)
  426. {
  427. DEBUGP(DL_WARN, ("CreateBinding: failed to alloc"
  428. " recv packet pool: %x\n", Status));
  429. break;
  430. }
  431. //
  432. // Buffer pool for receives.
  433. //
  434. NdisAllocateBufferPool(
  435. &Status,
  436. &pOpenContext->RecvBufferPool,
  437. MAX_RECV_PACKET_POOL_SIZE);
  438. if (Status != NDIS_STATUS_SUCCESS)
  439. {
  440. DEBUGP(DL_WARN, ("CreateBinding: failed to alloc"
  441. " recv buffer pool: %x\n", Status));
  442. break;
  443. }
  444. //
  445. // If we are running on Win9X, allocate a buffer pool for sends
  446. // as well, since we can't simply cast MDLs to NDIS_BUFFERs.
  447. //
  448. if (pOpenContext->bRunningOnWin9x)
  449. {
  450. NdisAllocateBufferPool(
  451. &Status,
  452. &pOpenContext->SendBufferPool,
  453. MAX_SEND_PACKET_POOL_SIZE);
  454. if (Status != NDIS_STATUS_SUCCESS)
  455. {
  456. DEBUGP(DL_WARN, ("CreateBinding: failed to alloc"
  457. " send buffer pool: %x\n", Status));
  458. break;
  459. }
  460. }
  461. //
  462. // Assume that the device is powered up.
  463. //
  464. pOpenContext->PowerState = NetDeviceStateD0;
  465. //
  466. // Open the adapter.
  467. //
  468. NUIO_INIT_EVENT(&pOpenContext->BindEvent);
  469. NdisOpenAdapter(
  470. &Status,
  471. &OpenErrorCode,
  472. &pOpenContext->BindingHandle,
  473. &SelectedMediumIndex,
  474. &MediumArray[0],
  475. sizeof(MediumArray) / sizeof(NDIS_MEDIUM),
  476. Globals.NdisProtocolHandle,
  477. (NDIS_HANDLE)pOpenContext,
  478. &pOpenContext->DeviceName,
  479. 0,
  480. NULL);
  481. if (Status == NDIS_STATUS_PENDING)
  482. {
  483. NUIO_WAIT_EVENT(&pOpenContext->BindEvent, 0);
  484. Status = pOpenContext->BindStatus;
  485. }
  486. if (Status != NDIS_STATUS_SUCCESS)
  487. {
  488. DEBUGP(DL_WARN, ("CreateBinding: NdisOpenAdapter (%ws) failed: %x\n",
  489. pOpenContext->DeviceName.Buffer, Status));
  490. break;
  491. }
  492. //
  493. // Note down the fact that we have successfully bound.
  494. // We don't update the state on the open just yet - this
  495. // is to prevent other threads from shutting down the binding.
  496. //
  497. fOpenComplete = TRUE;
  498. //
  499. // Get the friendly name for the adapter. It is not fatal for this
  500. // to fail.
  501. //
  502. (VOID)NdisQueryAdapterInstanceName(
  503. &pOpenContext->DeviceDescr,
  504. pOpenContext->BindingHandle
  505. );
  506. //
  507. // Get Current address
  508. //
  509. Status = ndisuioDoRequest(
  510. pOpenContext,
  511. NdisRequestQueryInformation,
  512. OID_802_3_CURRENT_ADDRESS,
  513. &pOpenContext->CurrentAddress[0],
  514. NUIO_MAC_ADDR_LEN,
  515. &BytesProcessed
  516. );
  517. if (Status != NDIS_STATUS_SUCCESS)
  518. {
  519. DEBUGP(DL_WARN, ("CreateBinding: qry current address failed: %x\n",
  520. Status));
  521. break;
  522. }
  523. //
  524. // Get MAC options.
  525. //
  526. Status = ndisuioDoRequest(
  527. pOpenContext,
  528. NdisRequestQueryInformation,
  529. OID_GEN_MAC_OPTIONS,
  530. &pOpenContext->MacOptions,
  531. sizeof(pOpenContext->MacOptions),
  532. &BytesProcessed
  533. );
  534. if (Status != NDIS_STATUS_SUCCESS)
  535. {
  536. DEBUGP(DL_WARN, ("CreateBinding: qry MAC options failed: %x\n",
  537. Status));
  538. break;
  539. }
  540. //
  541. // Get the max frame size.
  542. //
  543. Status = ndisuioDoRequest(
  544. pOpenContext,
  545. NdisRequestQueryInformation,
  546. OID_GEN_MAXIMUM_FRAME_SIZE,
  547. &pOpenContext->MaxFrameSize,
  548. sizeof(pOpenContext->MaxFrameSize),
  549. &BytesProcessed
  550. );
  551. if (Status != NDIS_STATUS_SUCCESS)
  552. {
  553. DEBUGP(DL_WARN, ("CreateBinding: qry max frame failed: %x\n",
  554. Status));
  555. break;
  556. }
  557. //
  558. // Get the media connect status.
  559. //
  560. Status = ndisuioDoRequest(
  561. pOpenContext,
  562. NdisRequestQueryInformation,
  563. OID_GEN_MEDIA_CONNECT_STATUS,
  564. &GenericUlong,
  565. sizeof(GenericUlong),
  566. &BytesProcessed
  567. );
  568. if (Status != NDIS_STATUS_SUCCESS)
  569. {
  570. DEBUGP(DL_WARN, ("CreateBinding: qry media connect status failed: %x\n",
  571. Status));
  572. break;
  573. }
  574. if (GenericUlong == NdisMediaStateConnected)
  575. {
  576. NUIO_SET_FLAGS(pOpenContext->Flags, NUIOO_MEDIA_FLAGS, NUIOO_MEDIA_CONNECTED);
  577. }
  578. else
  579. {
  580. NUIO_SET_FLAGS(pOpenContext->Flags, NUIOO_MEDIA_FLAGS, NUIOO_MEDIA_DISCONNECTED);
  581. }
  582. //
  583. // Mark this open. Also check if we received an Unbind while
  584. // we were setting this up.
  585. //
  586. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  587. NUIO_SET_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE);
  588. //
  589. // Did an unbind happen in the meantime?
  590. //
  591. if (NUIO_TEST_FLAGS(pOpenContext->Flags, NUIOO_UNBIND_FLAGS, NUIOO_UNBIND_RECEIVED))
  592. {
  593. Status = NDIS_STATUS_FAILURE;
  594. }
  595. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  596. }
  597. while (FALSE);
  598. if ((Status != NDIS_STATUS_SUCCESS) && !fDoNotDisturb)
  599. {
  600. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  601. //
  602. // Check if we had actually finished opening the adapter.
  603. //
  604. if (fOpenComplete)
  605. {
  606. NUIO_SET_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE);
  607. }
  608. else if (NUIO_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_OPENING))
  609. {
  610. NUIO_SET_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_FAILED);
  611. }
  612. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  613. ndisuioShutdownBinding(pOpenContext);
  614. }
  615. DEBUGP(DL_INFO, ("CreateBinding: OpenContext %p, Status %x\n",
  616. pOpenContext, Status));
  617. return (Status);
  618. }
  619. VOID
  620. ndisuioShutdownBinding(
  621. IN PNDISUIO_OPEN_CONTEXT pOpenContext
  622. )
  623. /*++
  624. Routine Description:
  625. Utility function to shut down the NDIS binding, if one exists, on
  626. the specified open. This is written to be called from:
  627. ndisuioCreateBinding - on failure
  628. NdisuioUnbindAdapter
  629. We handle the case where a binding is in the process of being set up.
  630. This precaution is not needed if this routine is only called from
  631. the context of our UnbindAdapter handler, but they are here in case
  632. we initiate unbinding from elsewhere (e.g. on processing a user command).
  633. NOTE: this blocks and finishes synchronously.
  634. Arguments:
  635. pOpenContext - pointer to open context block
  636. Return Value:
  637. None
  638. --*/
  639. {
  640. NDIS_STATUS Status;
  641. BOOLEAN DoCloseBinding = FALSE;
  642. do
  643. {
  644. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  645. if (NUIO_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_OPENING))
  646. {
  647. //
  648. // We are still in the process of setting up this binding.
  649. //
  650. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  651. break;
  652. }
  653. if (NUIO_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE))
  654. {
  655. NUIO_SET_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_CLOSING);
  656. DoCloseBinding = TRUE;
  657. }
  658. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  659. if (DoCloseBinding)
  660. {
  661. ULONG PacketFilter = 0;
  662. ULONG BytesRead = 0;
  663. //
  664. // Set Packet filter to 0 before closing the binding
  665. //
  666. Status = ndisuioDoRequest(
  667. pOpenContext,
  668. NdisRequestSetInformation,
  669. OID_GEN_CURRENT_PACKET_FILTER,
  670. &PacketFilter,
  671. sizeof(PacketFilter),
  672. &BytesRead);
  673. if (Status != NDIS_STATUS_SUCCESS)
  674. {
  675. DEBUGP(DL_WARN, ("ShutDownBinding: set packet filter failed: %x\n", Status));
  676. }
  677. //
  678. // Set multicast list to null before closing the binding
  679. //
  680. Status = ndisuioDoRequest(
  681. pOpenContext,
  682. NdisRequestSetInformation,
  683. OID_802_3_MULTICAST_LIST,
  684. NULL,
  685. 0,
  686. &BytesRead);
  687. if (Status != NDIS_STATUS_SUCCESS)
  688. {
  689. DEBUGP(DL_WARN, ("ShutDownBinding: set multicast list failed: %x\n", Status));
  690. }
  691. //
  692. // Wait for any pending sends or requests on
  693. // the binding to complete.
  694. //
  695. ndisuioWaitForPendingIO(pOpenContext, TRUE);
  696. //
  697. // Discard any queued receives.
  698. //
  699. ndisuioFlushReceiveQueue(pOpenContext);
  700. //
  701. // Close the binding now.
  702. //
  703. NUIO_INIT_EVENT(&pOpenContext->BindEvent);
  704. DEBUGP(DL_INFO, ("ShutdownBinding: Closing OpenContext %p,"
  705. " BindingHandle %p\n",
  706. pOpenContext, pOpenContext->BindingHandle));
  707. NdisCloseAdapter(&Status, pOpenContext->BindingHandle);
  708. if (Status == NDIS_STATUS_PENDING)
  709. {
  710. NUIO_WAIT_EVENT(&pOpenContext->BindEvent, 0);
  711. Status = pOpenContext->BindStatus;
  712. }
  713. NUIO_ASSERT(Status == NDIS_STATUS_SUCCESS);
  714. pOpenContext->BindingHandle = NULL;
  715. }
  716. if (DoCloseBinding)
  717. {
  718. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  719. NUIO_SET_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_IDLE);
  720. NUIO_SET_FLAGS(pOpenContext->Flags, NUIOO_UNBIND_FLAGS, 0);
  721. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  722. }
  723. //
  724. // Remove it from the global list.
  725. //
  726. NUIO_ACQUIRE_LOCK(&Globals.GlobalLock);
  727. NUIO_REMOVE_ENTRY_LIST(&pOpenContext->Link);
  728. NUIO_RELEASE_LOCK(&Globals.GlobalLock);
  729. //
  730. // Free any other resources allocated for this bind.
  731. //
  732. ndisuioFreeBindResources(pOpenContext);
  733. NUIO_DEREF_OPEN(pOpenContext); // Shutdown binding
  734. }
  735. while (FALSE);
  736. }
  737. VOID
  738. ndisuioFreeBindResources(
  739. IN PNDISUIO_OPEN_CONTEXT pOpenContext
  740. )
  741. /*++
  742. Routine Description:
  743. Free any resources set up for an NDIS binding.
  744. Arguments:
  745. pOpenContext - pointer to open context block
  746. Return Value:
  747. None
  748. --*/
  749. {
  750. if (pOpenContext->SendPacketPool != NULL)
  751. {
  752. NdisFreePacketPool(pOpenContext->SendPacketPool);
  753. pOpenContext->SendPacketPool = NULL;
  754. }
  755. if (pOpenContext->RecvPacketPool != NULL)
  756. {
  757. NdisFreePacketPool(pOpenContext->RecvPacketPool);
  758. pOpenContext->RecvPacketPool = NULL;
  759. }
  760. if (pOpenContext->RecvBufferPool != NULL)
  761. {
  762. NdisFreeBufferPool(pOpenContext->RecvBufferPool);
  763. pOpenContext->RecvBufferPool = NULL;
  764. }
  765. if (pOpenContext->SendBufferPool != NULL)
  766. {
  767. NdisFreeBufferPool(pOpenContext->SendBufferPool);
  768. pOpenContext->SendBufferPool = NULL;
  769. }
  770. if (pOpenContext->DeviceName.Buffer != NULL)
  771. {
  772. NUIO_FREE_MEM(pOpenContext->DeviceName.Buffer);
  773. pOpenContext->DeviceName.Buffer = NULL;
  774. pOpenContext->DeviceName.Length =
  775. pOpenContext->DeviceName.MaximumLength = 0;
  776. }
  777. if (pOpenContext->DeviceDescr.Buffer != NULL)
  778. {
  779. //
  780. // this would have been allocated by NdisQueryAdpaterInstanceName.
  781. //
  782. NdisFreeMemory(pOpenContext->DeviceDescr.Buffer, 0, 0);
  783. pOpenContext->DeviceDescr.Buffer = NULL;
  784. }
  785. }
  786. VOID
  787. ndisuioWaitForPendingIO(
  788. IN PNDISUIO_OPEN_CONTEXT pOpenContext,
  789. IN BOOLEAN DoCancelReads
  790. )
  791. /*++
  792. Routine Description:
  793. Utility function to wait for all outstanding I/O to complete
  794. on an open context. It is assumed that the open context
  795. won't go away while we are in this routine.
  796. Arguments:
  797. pOpenContext - pointer to open context structure
  798. DoCancelReads - do we wait for pending reads to go away (and cancel them)?
  799. Return Value:
  800. None
  801. --*/
  802. {
  803. NDIS_STATUS Status;
  804. ULONG LoopCount;
  805. ULONG PendingCount;
  806. #ifdef NDIS51
  807. //
  808. // Wait for any pending sends or requests on the binding to complete.
  809. //
  810. for (LoopCount = 0; LoopCount < 60; LoopCount++)
  811. {
  812. Status = NdisQueryPendingIOCount(
  813. pOpenContext->BindingHandle,
  814. &PendingCount);
  815. if ((Status != NDIS_STATUS_SUCCESS) ||
  816. (PendingCount == 0))
  817. {
  818. break;
  819. }
  820. DEBUGP(DL_INFO, ("WaitForPendingIO: Open %p, %d pending I/O at NDIS\n",
  821. pOpenContext, PendingCount));
  822. NUIO_SLEEP(2);
  823. }
  824. NUIO_ASSERT(LoopCount < 60);
  825. #endif // NDIS51
  826. //
  827. // Make sure any threads trying to send have finished.
  828. //
  829. for (LoopCount = 0; LoopCount < 60; LoopCount++)
  830. {
  831. if (pOpenContext->PendedSendCount == 0)
  832. {
  833. break;
  834. }
  835. DEBUGP(DL_WARN, ("WaitForPendingIO: Open %p, %d pended sends\n",
  836. pOpenContext, pOpenContext->PendedSendCount));
  837. NUIO_SLEEP(1);
  838. }
  839. NUIO_ASSERT(LoopCount < 60);
  840. if (DoCancelReads)
  841. {
  842. //
  843. // Wait for any pended reads to complete/cancel.
  844. //
  845. while (pOpenContext->PendedReadCount != 0)
  846. {
  847. DEBUGP(DL_INFO, ("WaitForPendingIO: Open %p, %d pended reads\n",
  848. pOpenContext, pOpenContext->PendedReadCount));
  849. //
  850. // Cancel any pending reads.
  851. //
  852. ndisuioCancelPendingReads(pOpenContext);
  853. NUIO_SLEEP(1);
  854. }
  855. }
  856. }
  857. VOID
  858. ndisuioDoProtocolUnload(
  859. VOID
  860. )
  861. /*++
  862. Routine Description:
  863. Utility routine to handle unload from the NDIS protocol side.
  864. Arguments:
  865. None
  866. Return Value:
  867. None
  868. --*/
  869. {
  870. NDIS_HANDLE ProtocolHandle;
  871. NDIS_STATUS Status;
  872. DEBUGP(DL_INFO, ("ProtocolUnload: ProtocolHandle %lx\n",
  873. Globals.NdisProtocolHandle));
  874. if (Globals.NdisProtocolHandle != NULL)
  875. {
  876. ProtocolHandle = Globals.NdisProtocolHandle;
  877. Globals.NdisProtocolHandle = NULL;
  878. NdisDeregisterProtocol(
  879. &Status,
  880. ProtocolHandle
  881. );
  882. }
  883. }
  884. NDIS_STATUS
  885. ndisuioDoRequest(
  886. IN PNDISUIO_OPEN_CONTEXT pOpenContext,
  887. IN NDIS_REQUEST_TYPE RequestType,
  888. IN NDIS_OID Oid,
  889. IN PVOID InformationBuffer,
  890. IN ULONG InformationBufferLength,
  891. OUT PULONG pBytesProcessed
  892. )
  893. /*++
  894. Routine Description:
  895. Utility routine that forms and sends an NDIS_REQUEST to the
  896. miniport, waits for it to complete, and returns status
  897. to the caller.
  898. NOTE: this assumes that the calling routine ensures validity
  899. of the binding handle until this returns.
  900. Arguments:
  901. pOpenContext - pointer to our open context
  902. RequestType - NdisRequest[Set|Query]Information
  903. Oid - the object being set/queried
  904. InformationBuffer - data for the request
  905. InformationBufferLength - length of the above
  906. pBytesProcessed - place to return bytes read/written
  907. Return Value:
  908. Status of the set/query request
  909. --*/
  910. {
  911. NDISUIO_REQUEST ReqContext;
  912. PNDIS_REQUEST pNdisRequest = &ReqContext.Request;
  913. NDIS_STATUS Status;
  914. NUIO_INIT_EVENT(&ReqContext.ReqEvent);
  915. pNdisRequest->RequestType = RequestType;
  916. switch (RequestType)
  917. {
  918. case NdisRequestQueryInformation:
  919. pNdisRequest->DATA.QUERY_INFORMATION.Oid = Oid;
  920. pNdisRequest->DATA.QUERY_INFORMATION.InformationBuffer =
  921. InformationBuffer;
  922. pNdisRequest->DATA.QUERY_INFORMATION.InformationBufferLength =
  923. InformationBufferLength;
  924. break;
  925. case NdisRequestSetInformation:
  926. pNdisRequest->DATA.SET_INFORMATION.Oid = Oid;
  927. pNdisRequest->DATA.SET_INFORMATION.InformationBuffer =
  928. InformationBuffer;
  929. pNdisRequest->DATA.SET_INFORMATION.InformationBufferLength =
  930. InformationBufferLength;
  931. break;
  932. default:
  933. NUIO_ASSERT(FALSE);
  934. break;
  935. }
  936. NdisRequest(&Status,
  937. pOpenContext->BindingHandle,
  938. pNdisRequest);
  939. if (Status == NDIS_STATUS_PENDING)
  940. {
  941. NUIO_WAIT_EVENT(&ReqContext.ReqEvent, 0);
  942. Status = ReqContext.Status;
  943. }
  944. if (Status == NDIS_STATUS_SUCCESS)
  945. {
  946. *pBytesProcessed = (RequestType == NdisRequestQueryInformation)?
  947. pNdisRequest->DATA.QUERY_INFORMATION.BytesWritten:
  948. pNdisRequest->DATA.SET_INFORMATION.BytesRead;
  949. //
  950. // The driver below should set the correct value to BytesWritten
  951. // or BytesRead. But now, we just truncate the value to InformationBufferLength
  952. //
  953. if (*pBytesProcessed > InformationBufferLength)
  954. {
  955. *pBytesProcessed = InformationBufferLength;
  956. }
  957. }
  958. return (Status);
  959. }
  960. NDIS_STATUS
  961. ndisuioValidateOpenAndDoRequest(
  962. IN PNDISUIO_OPEN_CONTEXT pOpenContext,
  963. IN NDIS_REQUEST_TYPE RequestType,
  964. IN NDIS_OID Oid,
  965. IN PVOID InformationBuffer,
  966. IN ULONG InformationBufferLength,
  967. OUT PULONG pBytesProcessed,
  968. IN BOOLEAN bWaitForPowerOn
  969. )
  970. /*++
  971. Routine Description:
  972. Utility routine to prevalidate and reference an open context
  973. before calling ndisuioDoRequest. This routine makes sure
  974. we have a valid binding.
  975. Arguments:
  976. pOpenContext - pointer to our open context
  977. RequestType - NdisRequest[Set|Query]Information
  978. Oid - the object being set/queried
  979. InformationBuffer - data for the request
  980. InformationBufferLength - length of the above
  981. pBytesProcessed - place to return bytes read/written
  982. bWaitForPowerOn - Wait for the device to be powered on if it isn't already.
  983. Return Value:
  984. Status of the set/query request
  985. --*/
  986. {
  987. NDIS_STATUS Status;
  988. do
  989. {
  990. if (pOpenContext == NULL)
  991. {
  992. DEBUGP(DL_WARN, ("ValidateOpenAndDoRequest: request on unassociated file object!\n"));
  993. Status = NDIS_STATUS_INVALID_DATA;
  994. break;
  995. }
  996. NUIO_STRUCT_ASSERT(pOpenContext, oc);
  997. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  998. //
  999. // Proceed only if we have a binding.
  1000. //
  1001. if (!NUIO_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE))
  1002. {
  1003. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1004. Status = NDIS_STATUS_INVALID_DATA;
  1005. break;
  1006. }
  1007. NUIO_ASSERT(pOpenContext->BindingHandle != NULL);
  1008. //
  1009. // Make sure that the binding does not go away until we
  1010. // are finished with the request.
  1011. //
  1012. NdisInterlockedIncrement((PLONG)&pOpenContext->PendedSendCount);
  1013. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1014. if (bWaitForPowerOn)
  1015. {
  1016. //
  1017. // Wait for the device below to be powered up.
  1018. // We don't wait indefinitely here - this is to avoid
  1019. // a PROCESS_HAS_LOCKED_PAGES bugcheck that could happen
  1020. // if the calling process terminates, and this IRP doesn't
  1021. // complete within a reasonable time. An alternative would
  1022. // be to explicitly handle cancellation of this IRP.
  1023. //
  1024. NUIO_WAIT_EVENT(&pOpenContext->PoweredUpEvent, 4500);
  1025. }
  1026. Status = ndisuioDoRequest(
  1027. pOpenContext,
  1028. RequestType,
  1029. Oid,
  1030. InformationBuffer,
  1031. InformationBufferLength,
  1032. pBytesProcessed);
  1033. //
  1034. // Let go of the binding.
  1035. //
  1036. NdisInterlockedDecrement((PLONG)&pOpenContext->PendedSendCount);
  1037. }
  1038. while (FALSE);
  1039. DEBUGP(DL_LOUD, ("ValidateOpenAndDoReq: Open %p/%x, OID %x, Status %x\n",
  1040. pOpenContext, pOpenContext->Flags, Oid, Status));
  1041. return (Status);
  1042. }
  1043. VOID
  1044. NdisuioResetComplete(
  1045. IN NDIS_HANDLE ProtocolBindingContext,
  1046. IN NDIS_STATUS Status
  1047. )
  1048. /*++
  1049. Routine Description:
  1050. NDIS entry point indicating that a protocol initiated reset
  1051. has completed. Since we never call NdisReset(), this should
  1052. never be called.
  1053. Arguments:
  1054. ProtocolBindingContext - pointer to open context
  1055. Status - status of reset completion
  1056. Return Value:
  1057. None
  1058. --*/
  1059. {
  1060. UNREFERENCED_PARAMETER(ProtocolBindingContext);
  1061. UNREFERENCED_PARAMETER(Status);
  1062. ASSERT(FALSE);
  1063. return;
  1064. }
  1065. VOID
  1066. NdisuioRequestComplete(
  1067. IN NDIS_HANDLE ProtocolBindingContext,
  1068. IN PNDIS_REQUEST pNdisRequest,
  1069. IN NDIS_STATUS Status
  1070. )
  1071. /*++
  1072. Routine Description:
  1073. NDIS entry point indicating completion of a pended NDIS_REQUEST.
  1074. Arguments:
  1075. ProtocolBindingContext - pointer to open context
  1076. pNdisRequest - pointer to NDIS request
  1077. Status - status of reset completion
  1078. Return Value:
  1079. None
  1080. --*/
  1081. {
  1082. PNDISUIO_OPEN_CONTEXT pOpenContext;
  1083. PNDISUIO_REQUEST pReqContext;
  1084. pOpenContext = (PNDISUIO_OPEN_CONTEXT)ProtocolBindingContext;
  1085. NUIO_STRUCT_ASSERT(pOpenContext, oc);
  1086. //
  1087. // Get at the request context.
  1088. //
  1089. pReqContext = CONTAINING_RECORD(pNdisRequest, NDISUIO_REQUEST, Request);
  1090. //
  1091. // Save away the completion status.
  1092. //
  1093. pReqContext->Status = Status;
  1094. //
  1095. // Wake up the thread blocked for this request to complete.
  1096. //
  1097. NUIO_SIGNAL_EVENT(&pReqContext->ReqEvent);
  1098. }
  1099. VOID
  1100. NdisuioStatus(
  1101. IN NDIS_HANDLE ProtocolBindingContext,
  1102. IN NDIS_STATUS GeneralStatus,
  1103. IN PVOID StatusBuffer,
  1104. IN UINT StatusBufferSize
  1105. )
  1106. /*++
  1107. Routine Description:
  1108. Protocol entry point called by NDIS to indicate a change
  1109. in status at the miniport.
  1110. We make note of reset and media connect status indications.
  1111. Arguments:
  1112. ProtocolBindingContext - pointer to open context
  1113. GeneralStatus - status code
  1114. StatusBuffer - status-specific additional information
  1115. StatusBufferSize - size of the above
  1116. Return Value:
  1117. None
  1118. --*/
  1119. {
  1120. PNDISUIO_OPEN_CONTEXT pOpenContext;
  1121. UNREFERENCED_PARAMETER(StatusBuffer);
  1122. UNREFERENCED_PARAMETER(StatusBufferSize);
  1123. pOpenContext = (PNDISUIO_OPEN_CONTEXT)ProtocolBindingContext;
  1124. NUIO_STRUCT_ASSERT(pOpenContext, oc);
  1125. DEBUGP(DL_INFO, ("Status: Open %p, Status %x\n",
  1126. pOpenContext, GeneralStatus));
  1127. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  1128. do
  1129. {
  1130. if (pOpenContext->PowerState != NetDeviceStateD0)
  1131. {
  1132. //
  1133. // The device is in a low power state.
  1134. //
  1135. DEBUGP(DL_INFO, ("Status: Open %p in power state %d,"
  1136. " Status %x ignored\n", pOpenContext,
  1137. pOpenContext->PowerState, GeneralStatus));
  1138. //
  1139. // We continue and make note of status indications
  1140. //
  1141. // break;
  1142. //
  1143. //
  1144. // NOTE that any actions we take based on these
  1145. // status indications should take into account
  1146. // the current device power state.
  1147. //
  1148. }
  1149. switch(GeneralStatus)
  1150. {
  1151. case NDIS_STATUS_RESET_START:
  1152. NUIO_ASSERT(!NUIO_TEST_FLAGS(pOpenContext->Flags,
  1153. NUIOO_RESET_FLAGS,
  1154. NUIOO_RESET_IN_PROGRESS));
  1155. NUIO_SET_FLAGS(pOpenContext->Flags,
  1156. NUIOO_RESET_FLAGS,
  1157. NUIOO_RESET_IN_PROGRESS);
  1158. break;
  1159. case NDIS_STATUS_RESET_END:
  1160. NUIO_ASSERT(NUIO_TEST_FLAGS(pOpenContext->Flags,
  1161. NUIOO_RESET_FLAGS,
  1162. NUIOO_RESET_IN_PROGRESS));
  1163. NUIO_SET_FLAGS(pOpenContext->Flags,
  1164. NUIOO_RESET_FLAGS,
  1165. NUIOO_NOT_RESETTING);
  1166. break;
  1167. case NDIS_STATUS_MEDIA_CONNECT:
  1168. NUIO_SET_FLAGS(pOpenContext->Flags,
  1169. NUIOO_MEDIA_FLAGS,
  1170. NUIOO_MEDIA_CONNECTED);
  1171. break;
  1172. case NDIS_STATUS_MEDIA_DISCONNECT:
  1173. NUIO_SET_FLAGS(pOpenContext->Flags,
  1174. NUIOO_MEDIA_FLAGS,
  1175. NUIOO_MEDIA_DISCONNECTED);
  1176. break;
  1177. default:
  1178. break;
  1179. }
  1180. }
  1181. while (FALSE);
  1182. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1183. }
  1184. VOID
  1185. NdisuioStatusComplete(
  1186. IN NDIS_HANDLE ProtocolBindingContext
  1187. )
  1188. /*++
  1189. Routine Description:
  1190. Protocol entry point called by NDIS. We ignore this.
  1191. Arguments:
  1192. ProtocolBindingContext - pointer to open context
  1193. Return Value:
  1194. None
  1195. --*/
  1196. {
  1197. PNDISUIO_OPEN_CONTEXT pOpenContext;
  1198. pOpenContext = (PNDISUIO_OPEN_CONTEXT)ProtocolBindingContext;
  1199. NUIO_STRUCT_ASSERT(pOpenContext, oc);
  1200. return;
  1201. }
  1202. NDIS_STATUS
  1203. ndisuioQueryBinding(
  1204. IN PUCHAR pBuffer,
  1205. IN ULONG InputLength,
  1206. IN ULONG OutputLength,
  1207. OUT PULONG pBytesReturned
  1208. )
  1209. /*++
  1210. Routine Description:
  1211. Return information about the specified binding.
  1212. Arguments:
  1213. pBuffer - pointer to NDISUIO_QUERY_BINDING
  1214. InputLength - input buffer size
  1215. OutputLength - output buffer size
  1216. pBytesReturned - place to return copied byte count.
  1217. Return Value:
  1218. NDIS_STATUS_SUCCESS if successful, failure code otherwise.
  1219. --*/
  1220. {
  1221. PNDISUIO_QUERY_BINDING pQueryBinding;
  1222. PNDISUIO_OPEN_CONTEXT pOpenContext;
  1223. PLIST_ENTRY pEnt;
  1224. ULONG Remaining;
  1225. ULONG BindingIndex;
  1226. NDIS_STATUS Status;
  1227. do
  1228. {
  1229. if (InputLength < sizeof(NDISUIO_QUERY_BINDING))
  1230. {
  1231. Status = NDIS_STATUS_RESOURCES;
  1232. break;
  1233. }
  1234. if (OutputLength < sizeof(NDISUIO_QUERY_BINDING))
  1235. {
  1236. Status = NDIS_STATUS_BUFFER_OVERFLOW;
  1237. break;
  1238. }
  1239. Remaining = OutputLength - sizeof(NDISUIO_QUERY_BINDING);
  1240. pQueryBinding = (PNDISUIO_QUERY_BINDING)pBuffer;
  1241. BindingIndex = pQueryBinding->BindingIndex;
  1242. Status = NDIS_STATUS_ADAPTER_NOT_FOUND;
  1243. pOpenContext = NULL;
  1244. NUIO_ACQUIRE_LOCK(&Globals.GlobalLock);
  1245. for (pEnt = Globals.OpenList.Flink;
  1246. pEnt != &Globals.OpenList;
  1247. pEnt = pEnt->Flink)
  1248. {
  1249. pOpenContext = CONTAINING_RECORD(pEnt, NDISUIO_OPEN_CONTEXT, Link);
  1250. NUIO_STRUCT_ASSERT(pOpenContext, oc);
  1251. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  1252. //
  1253. // Skip if not bound.
  1254. //
  1255. if (!NUIO_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE))
  1256. {
  1257. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1258. continue;
  1259. }
  1260. if (BindingIndex == 0)
  1261. {
  1262. //
  1263. // Got the binding we are looking for. Copy the device
  1264. // name and description strings to the output buffer.
  1265. //
  1266. DEBUGP(DL_INFO,
  1267. ("QueryBinding: found open %p\n", pOpenContext));
  1268. pQueryBinding->DeviceNameLength = pOpenContext->DeviceName.Length + sizeof(WCHAR);
  1269. pQueryBinding->DeviceDescrLength = pOpenContext->DeviceDescr.Length + sizeof(WCHAR);
  1270. if (Remaining < pQueryBinding->DeviceNameLength +
  1271. pQueryBinding->DeviceDescrLength)
  1272. {
  1273. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1274. Status = NDIS_STATUS_BUFFER_OVERFLOW;
  1275. break;
  1276. }
  1277. NUIO_ZERO_MEM((PUCHAR)pBuffer + sizeof(NDISUIO_QUERY_BINDING),
  1278. pQueryBinding->DeviceNameLength +
  1279. pQueryBinding->DeviceDescrLength);
  1280. pQueryBinding->DeviceNameOffset = sizeof(NDISUIO_QUERY_BINDING);
  1281. NUIO_COPY_MEM((PUCHAR)pBuffer + pQueryBinding->DeviceNameOffset,
  1282. pOpenContext->DeviceName.Buffer,
  1283. pOpenContext->DeviceName.Length);
  1284. pQueryBinding->DeviceDescrOffset = pQueryBinding->DeviceNameOffset +
  1285. pQueryBinding->DeviceNameLength;
  1286. NUIO_COPY_MEM((PUCHAR)pBuffer + pQueryBinding->DeviceDescrOffset,
  1287. pOpenContext->DeviceDescr.Buffer,
  1288. pOpenContext->DeviceDescr.Length);
  1289. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1290. *pBytesReturned = pQueryBinding->DeviceDescrOffset + pQueryBinding->DeviceDescrLength;
  1291. Status = NDIS_STATUS_SUCCESS;
  1292. break;
  1293. }
  1294. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1295. BindingIndex--;
  1296. }
  1297. NUIO_RELEASE_LOCK(&Globals.GlobalLock);
  1298. }
  1299. while (FALSE);
  1300. return (Status);
  1301. }
  1302. PNDISUIO_OPEN_CONTEXT
  1303. ndisuioLookupDevice(
  1304. IN PUCHAR pBindingInfo,
  1305. IN ULONG BindingInfoLength
  1306. )
  1307. /*++
  1308. Routine Description:
  1309. Search our global list for an open context structure that
  1310. has a binding to the specified device, and return a pointer
  1311. to it.
  1312. NOTE: we reference the open that we return.
  1313. Arguments:
  1314. pBindingInfo - pointer to unicode device name string
  1315. BindingInfoLength - length in bytes of the above.
  1316. Return Value:
  1317. Pointer to the matching open context if found, else NULL
  1318. --*/
  1319. {
  1320. PNDISUIO_OPEN_CONTEXT pOpenContext;
  1321. PLIST_ENTRY pEnt;
  1322. pOpenContext = NULL;
  1323. NUIO_ACQUIRE_LOCK(&Globals.GlobalLock);
  1324. for (pEnt = Globals.OpenList.Flink;
  1325. pEnt != &Globals.OpenList;
  1326. pEnt = pEnt->Flink)
  1327. {
  1328. pOpenContext = CONTAINING_RECORD(pEnt, NDISUIO_OPEN_CONTEXT, Link);
  1329. NUIO_STRUCT_ASSERT(pOpenContext, oc);
  1330. //
  1331. // Check if this has the name we are looking for.
  1332. //
  1333. if ((pOpenContext->DeviceName.Length == BindingInfoLength) &&
  1334. NUIO_MEM_CMP(pOpenContext->DeviceName.Buffer, pBindingInfo, BindingInfoLength))
  1335. {
  1336. NUIO_REF_OPEN(pOpenContext); // ref added by LookupDevice
  1337. break;
  1338. }
  1339. pOpenContext = NULL;
  1340. }
  1341. NUIO_RELEASE_LOCK(&Globals.GlobalLock);
  1342. return (pOpenContext);
  1343. }
  1344. NDIS_STATUS
  1345. ndisuioQueryOidValue(
  1346. IN PNDISUIO_OPEN_CONTEXT pOpenContext,
  1347. OUT PVOID pDataBuffer,
  1348. IN ULONG BufferLength,
  1349. OUT PULONG pBytesWritten
  1350. )
  1351. /*++
  1352. Routine Description:
  1353. Query an arbitrary OID value from the miniport.
  1354. Arguments:
  1355. pOpenContext - pointer to open context representing our binding to the miniport
  1356. pDataBuffer - place to store the returned value
  1357. BufferLength - length of the above
  1358. pBytesWritten - place to return length returned
  1359. Return Value:
  1360. NDIS_STATUS_SUCCESS if we successfully queried the OID.
  1361. NDIS_STATUS_XXX error code otherwise.
  1362. --*/
  1363. {
  1364. NDIS_STATUS Status;
  1365. PNDISUIO_QUERY_OID pQuery;
  1366. NDIS_OID Oid;
  1367. Oid = 0;
  1368. do
  1369. {
  1370. if (BufferLength < sizeof(NDISUIO_QUERY_OID))
  1371. {
  1372. Status = NDIS_STATUS_BUFFER_TOO_SHORT;
  1373. break;
  1374. }
  1375. pQuery = (PNDISUIO_QUERY_OID)pDataBuffer;
  1376. Oid = pQuery->Oid;
  1377. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  1378. if (!NUIO_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE))
  1379. {
  1380. DEBUGP(DL_WARN,
  1381. ("QueryOid: Open %p/%x is in invalid state\n",
  1382. pOpenContext, pOpenContext->Flags));
  1383. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1384. Status = NDIS_STATUS_FAILURE;
  1385. break;
  1386. }
  1387. //
  1388. // Make sure the binding doesn't go away.
  1389. //
  1390. NdisInterlockedIncrement((PLONG)&pOpenContext->PendedSendCount);
  1391. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1392. Status = ndisuioDoRequest(
  1393. pOpenContext,
  1394. NdisRequestQueryInformation,
  1395. Oid,
  1396. &pQuery->Data[0],
  1397. BufferLength - FIELD_OFFSET(NDISUIO_QUERY_OID, Data),
  1398. pBytesWritten);
  1399. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  1400. NdisInterlockedDecrement((PLONG)&pOpenContext->PendedSendCount);
  1401. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1402. if (Status == NDIS_STATUS_SUCCESS)
  1403. {
  1404. *pBytesWritten += FIELD_OFFSET(NDISUIO_QUERY_OID, Data);
  1405. }
  1406. }
  1407. while (FALSE);
  1408. DEBUGP(DL_LOUD, ("QueryOid: Open %p/%x, OID %x, Status %x\n",
  1409. pOpenContext, pOpenContext->Flags, Oid, Status));
  1410. return (Status);
  1411. }
  1412. NDIS_STATUS
  1413. ndisuioSetOidValue(
  1414. IN PNDISUIO_OPEN_CONTEXT pOpenContext,
  1415. OUT PVOID pDataBuffer,
  1416. IN ULONG BufferLength
  1417. )
  1418. /*++
  1419. Routine Description:
  1420. Set an arbitrary OID value to the miniport.
  1421. Arguments:
  1422. pOpenContext - pointer to open context representing our binding to the miniport
  1423. pDataBuffer - buffer that contains the value to be set
  1424. BufferLength - length of the above
  1425. Return Value:
  1426. NDIS_STATUS_SUCCESS if we successfully set the OID
  1427. NDIS_STATUS_XXX error code otherwise.
  1428. --*/
  1429. {
  1430. NDIS_STATUS Status;
  1431. PNDISUIO_SET_OID pSet;
  1432. NDIS_OID Oid;
  1433. ULONG BytesWritten;
  1434. Oid = 0;
  1435. do
  1436. {
  1437. if (BufferLength < sizeof(NDISUIO_SET_OID))
  1438. {
  1439. Status = NDIS_STATUS_BUFFER_TOO_SHORT;
  1440. break;
  1441. }
  1442. pSet = (PNDISUIO_SET_OID)pDataBuffer;
  1443. Oid = pSet->Oid;
  1444. //
  1445. // We should check the OID is settable by the user mode apps
  1446. //
  1447. if (!ndisuioValidOid(Oid))
  1448. {
  1449. DEBUGP(DL_WARN, ("SetOid: Oid %x cannot be set\n", Oid));
  1450. Status = NDIS_STATUS_INVALID_DATA;
  1451. break;
  1452. }
  1453. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  1454. if (!NUIO_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE))
  1455. {
  1456. DEBUGP(DL_WARN,
  1457. ("SetOid: Open %p/%x is in invalid state\n",
  1458. pOpenContext, pOpenContext->Flags));
  1459. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1460. Status = NDIS_STATUS_FAILURE;
  1461. break;
  1462. }
  1463. //
  1464. // Make sure the binding doesn't go away.
  1465. //
  1466. NdisInterlockedIncrement((PLONG)&pOpenContext->PendedSendCount);
  1467. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1468. Status = ndisuioDoRequest(
  1469. pOpenContext,
  1470. NdisRequestSetInformation,
  1471. Oid,
  1472. &pSet->Data[0],
  1473. BufferLength - FIELD_OFFSET(NDISUIO_SET_OID, Data),
  1474. &BytesWritten);
  1475. NUIO_ACQUIRE_LOCK(&pOpenContext->Lock);
  1476. NdisInterlockedDecrement((PLONG)&pOpenContext->PendedSendCount);
  1477. NUIO_RELEASE_LOCK(&pOpenContext->Lock);
  1478. }
  1479. while (FALSE);
  1480. DEBUGP(DL_LOUD, ("SetOid: Open %p/%x, OID %x, Status %x\n",
  1481. pOpenContext, pOpenContext->Flags, Oid, Status));
  1482. return (Status);
  1483. }
  1484. BOOLEAN
  1485. ndisuioValidOid(
  1486. IN NDIS_OID Oid
  1487. )
  1488. /*++
  1489. Routine Description:
  1490. Validate whether the given set OID is settable or not.
  1491. Arguments:
  1492. Oid - The OID which the user tries to set.
  1493. Return Value:
  1494. TRUE if the OID is allowed to set
  1495. FALSE otherwise.
  1496. --*/
  1497. {
  1498. UINT i;
  1499. UINT NumOids;
  1500. NumOids = sizeof(ndisuioSupportedSetOids) / sizeof(NDIS_OID);
  1501. for (i = 0; i < NumOids; i++)
  1502. {
  1503. if (ndisuioSupportedSetOids[i] == Oid)
  1504. {
  1505. break;
  1506. }
  1507. }
  1508. return (i < NumOids);
  1509. }