e23432375c568aef68999aa022444754b4a91611
[reactos.git] / drivers / network / ndisuio / misc.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS NDIS User I/O driver
4 * FILE: misc.c
5 * PURPOSE: Helper functions
6 * PROGRAMMERS: Cameron Gutman (cameron.gutman@reactos.org)
7 */
8
9 #include "ndisuio.h"
10
11 #define NDEBUG
12 #include <debug.h>
13
14 PNDISUIO_ADAPTER_CONTEXT
15 FindAdapterContextByName(PNDIS_STRING DeviceName)
16 {
17 KIRQL OldIrql;
18 PLIST_ENTRY CurrentEntry;
19 PNDISUIO_ADAPTER_CONTEXT AdapterContext;
20
21 KeAcquireSpinLock(&GlobalAdapterListLock, &OldIrql);
22 CurrentEntry = GlobalAdapterList.Flink;
23 while (CurrentEntry != &GlobalAdapterList)
24 {
25 AdapterContext = CONTAINING_RECORD(CurrentEntry, NDISUIO_ADAPTER_CONTEXT, ListEntry);
26
27 /* Check if the device name matches */
28 if (RtlEqualUnicodeString(&AdapterContext->DeviceName, DeviceName, TRUE))
29 {
30 KeAcquireSpinLockAtDpcLevel(&AdapterContext->Spinlock);
31
32 /* Check that it's not being destroyed */
33 if (AdapterContext->OpenCount > 0)
34 {
35 KeReleaseSpinLockFromDpcLevel(&AdapterContext->Spinlock);
36 KeReleaseSpinLock(&GlobalAdapterListLock, OldIrql);
37 return AdapterContext;
38 }
39 else
40 {
41 KeReleaseSpinLockFromDpcLevel(&Adaptercontext->Spinlock);
42 }
43 }
44
45 CurrentEntry = CurrentEntry->Flink;
46 }
47 KeReleaseSpinLock(&GlobalAdapterListLock, OldIrql);
48
49 return NULL;
50 }
51
52 VOID
53 ReferenceAdapterContext(PNDISUIO_ADAPTER_CONTEXT AdapterContext, BOOLEAN Locked)
54 {
55 KIRQL OldIrql;
56
57 /* Lock if needed */
58 if (!Locked)
59 {
60 KeAcquireSpinLock(&AdapterContext->Spinlock, &OldIrql);
61 }
62
63 /* Increment the open count */
64 AdapterContext->OpenCount++;
65
66 /* Unlock if needed */
67 if (!Locked)
68 {
69 KeReleaseSpinLock(&AdapterContext->Spinlock, OldIrql);
70 }
71 }
72
73 VOID
74 DereferenceAdapterContextWithOpenEntry(PNDISUIO_ADAPTER_CONTEXT AdapterContext,
75 PNDISUIO_OPEN_ENTRY OpenEntry)
76 {
77 KIRQL OldIrql;
78
79 /* Lock the adapter context */
80 KeAcquireSpinLock(&AdapterContext->Spinlock, &OldIrql);
81
82 /* Decrement the open count */
83 AdapterContext->OpenCount--;
84
85 /* Cleanup the open entry if we were given one */
86 if (OpenEntry != NULL)
87 {
88 /* Remove the open entry */
89 RemoveEntryList(&OpenEntry->ListEntry);
90
91 /* Invalidate the FO */
92 OpenEntry->FileObject->FsContext = NULL;
93 OpenEntry->FileObject->FsContext2 = NULL;
94
95 /* Free the open entry */
96 ExFreePool(OpenEntry);
97 }
98
99 /* See if this binding can be destroyed */
100 if (AdapterContext->OpenCount == 0)
101 {
102 /* Unlock the context */
103 KeReleaseSpinLock(&AdapterContext->Spinlock, OldIrql);
104
105 /* Destroy the adapter context */
106 UnbindAdapterByContext(AdapterContext);
107 }
108 else
109 {
110 /* Still more references on it */
111 KeReleaseSpinLock(&AdapterContext->Spinlock, OldIrql);
112 }
113 }