2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS NDIS User I/O driver
5 * PURPOSE: Helper functions
6 * PROGRAMMERS: Cameron Gutman (cameron.gutman@reactos.org)
14 PNDISUIO_ADAPTER_CONTEXT
15 FindAdapterContextByName(PNDIS_STRING DeviceName
)
18 PLIST_ENTRY CurrentEntry
;
19 PNDISUIO_ADAPTER_CONTEXT AdapterContext
;
21 KeAcquireSpinLock(&GlobalAdapterListLock
, &OldIrql
);
22 CurrentEntry
= GlobalAdapterList
.Flink
;
23 while (CurrentEntry
!= &GlobalAdapterList
)
25 AdapterContext
= CONTAINING_RECORD(CurrentEntry
, NDISUIO_ADAPTER_CONTEXT
, ListEntry
);
27 /* Check if the device name matches */
28 if (RtlEqualUnicodeString(&AdapterContext
->DeviceName
, DeviceName
, TRUE
))
30 KeAcquireSpinLockAtDpcLevel(&AdapterContext
->Spinlock
);
32 /* Check that it's not being destroyed */
33 if (AdapterContext
->OpenCount
> 0)
35 KeReleaseSpinLockFromDpcLevel(&AdapterContext
->Spinlock
);
36 KeReleaseSpinLock(&GlobalAdapterListLock
, OldIrql
);
37 return AdapterContext
;
41 KeReleaseSpinLockFromDpcLevel(&Adaptercontext
->Spinlock
);
45 CurrentEntry
= CurrentEntry
->Flink
;
47 KeReleaseSpinLock(&GlobalAdapterListLock
, OldIrql
);
53 ReferenceAdapterContext(PNDISUIO_ADAPTER_CONTEXT AdapterContext
, BOOLEAN Locked
)
60 KeAcquireSpinLock(&AdapterContext
->Spinlock
, &OldIrql
);
63 /* Increment the open count */
64 AdapterContext
->OpenCount
++;
66 /* Unlock if needed */
69 KeReleaseSpinLock(&AdapterContext
->Spinlock
, OldIrql
);
74 DereferenceAdapterContextWithOpenEntry(PNDISUIO_ADAPTER_CONTEXT AdapterContext
,
75 PNDISUIO_OPEN_ENTRY OpenEntry
)
79 /* Lock the adapter context */
80 KeAcquireSpinLock(&AdapterContext
->Spinlock
, &OldIrql
);
82 /* Decrement the open count */
83 AdapterContext
->OpenCount
--;
85 /* Cleanup the open entry if we were given one */
86 if (OpenEntry
!= NULL
)
88 /* Remove the open entry */
89 RemoveEntryList(&OpenEntry
->ListEntry
);
91 /* Invalidate the FO */
92 OpenEntry
->FileObject
->FsContext
= NULL
;
93 OpenEntry
->FileObject
->FsContext2
= NULL
;
95 /* Free the open entry */
96 ExFreePool(OpenEntry
);
99 /* See if this binding can be destroyed */
100 if (AdapterContext
->OpenCount
== 0)
102 /* Unlock the context */
103 KeReleaseSpinLock(&AdapterContext
->Spinlock
, OldIrql
);
105 /* Destroy the adapter context */
106 UnbindAdapterByContext(AdapterContext
);
110 /* Still more references on it */
111 KeReleaseSpinLock(&AdapterContext
->Spinlock
, OldIrql
);