[NDISUIO]
[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 KeReleaseSpinLock(&GlobalAdapterListLock, OldIrql);
31 return AdapterContext;
32 }
33
34 CurrentEntry = CurrentEntry->Flink;
35 }
36 KeReleaseSpinLock(&GlobalAdapterListLock, OldIrql);
37
38 return NULL;
39 }
40
41 VOID
42 ReferenceAdapterContext(PNDISUIO_ADAPTER_CONTEXT AdapterContext)
43 {
44 /* Increment the open count */
45 AdapterContext->OpenCount++;
46 }
47
48 VOID
49 DereferenceAdapterContextWithOpenEntry(PNDISUIO_ADAPTER_CONTEXT AdapterContext,
50 PNDISUIO_OPEN_ENTRY OpenEntry)
51 {
52 KIRQL OldIrql;
53
54 /* Lock the adapter context */
55 KeAcquireSpinLock(&AdapterContext->Spinlock, &OldIrql);
56
57 /* Decrement the open count */
58 AdapterContext->OpenCount--;
59
60 /* Cleanup the open entry if we were given one */
61 if (OpenEntry != NULL)
62 {
63 /* Remove the open entry */
64 RemoveEntryList(&OpenEntry->ListEntry);
65
66 /* Invalidate the FO */
67 OpenEntry->FileObject->FsContext = NULL;
68 OpenEntry->FileObject->FsContext2 = NULL;
69
70 /* Free the open entry */
71 ExFreePool(OpenEntry);
72 }
73
74 /* See if this binding can be destroyed */
75 if (AdapterContext->OpenCount == 0)
76 {
77 /* Unlock the context */
78 KeReleaseSpinLock(&AdapterContext->Spinlock, OldIrql);
79
80 /* Destroy the adapter context */
81 UnbindAdapterByContext(AdapterContext);
82 }
83 else
84 {
85 /* Still more references on it */
86 KeReleaseSpinLock(&AdapterContext->Spinlock, OldIrql);
87 }
88 }