xref: /reactos/drivers/network/ndisuio/main.c (revision c2c66aff)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS NDIS User I/O driver
4  * FILE:        main.c
5  * PURPOSE:     Driver entry point and protocol initialization
6  * PROGRAMMERS: Cameron Gutman (cameron.gutman@reactos.org)
7  */
8 
9 #include "ndisuio.h"
10 
11 #define NDEBUG
12 #include <debug.h>
13 
14 PDEVICE_OBJECT GlobalDeviceObject;
15 NDIS_HANDLE GlobalProtocolHandle;
16 KSPIN_LOCK GlobalAdapterListLock;
17 LIST_ENTRY GlobalAdapterList;
18 
19 NDIS_STRING ProtocolName = RTL_CONSTANT_STRING(L"NDISUIO");
20 
21 VOID NTAPI NduUnload(PDRIVER_OBJECT DriverObject)
22 {
23     DPRINT("NDISUIO: Unloaded\n");
24 }
25 
26 NTSTATUS
27 NTAPI
28 DriverEntry(PDRIVER_OBJECT DriverObject,
29             PUNICODE_STRING RegistryPath)
30 {
31     NDIS_STATUS Status;
32     NDIS_PROTOCOL_CHARACTERISTICS Chars;
33     UNICODE_STRING NtDeviceName = RTL_CONSTANT_STRING(NDISUIO_DEVICE_NAME_NT);
34     UNICODE_STRING DosDeviceName = RTL_CONSTANT_STRING(NDISUIO_DEVICE_NAME_DOS);
35 
36     /* Setup dispatch functions */
37     DriverObject->MajorFunction[IRP_MJ_CREATE] = NduDispatchCreate;
38     DriverObject->MajorFunction[IRP_MJ_CLOSE] = NduDispatchClose;
39     DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = NduDispatchDeviceControl;
40     DriverObject->MajorFunction[IRP_MJ_READ] = NduDispatchRead;
41     DriverObject->MajorFunction[IRP_MJ_WRITE] = NduDispatchWrite;
42     DriverObject->DriverUnload = NduUnload;
43 
44     /* Setup global state */
45     InitializeListHead(&GlobalAdapterList);
46     KeInitializeSpinLock(&GlobalAdapterListLock);
47 
48     /* Create the NDISUIO device object */
49     Status = IoCreateDevice(DriverObject,
50                             0,
51                             &NtDeviceName,
52                             FILE_DEVICE_SECURE_OPEN,
53                             0,
54                             FALSE,
55                             &GlobalDeviceObject);
56     if (!NT_SUCCESS(Status))
57     {
58         DPRINT1("Failed to create device object with status 0x%x\n", Status);
59         return Status;
60     }
61 
62     /* Create a symbolic link into the DOS devices namespace */
63     Status = IoCreateSymbolicLink(&DosDeviceName, &NtDeviceName);
64     if (!NT_SUCCESS(Status))
65     {
66         DPRINT1("Failed to create symbolic link with status 0x%x\n", Status);
67         IoDeleteDevice(GlobalDeviceObject);
68         return Status;
69     }
70 
71     /* Register the protocol with NDIS */
72     RtlZeroMemory(&Chars, sizeof(Chars));
73     Chars.MajorNdisVersion = NDIS_MAJOR_VERSION;
74     Chars.MinorNdisVersion = NDIS_MINOR_VERSION;
75     Chars.OpenAdapterCompleteHandler = NduOpenAdapterComplete;
76     Chars.CloseAdapterCompleteHandler = NduCloseAdapterComplete;
77     Chars.PnPEventHandler = NduNetPnPEvent;
78     Chars.SendCompleteHandler = NduSendComplete;
79     Chars.TransferDataCompleteHandler = NduTransferDataComplete;
80     Chars.ResetCompleteHandler = NduResetComplete;
81     Chars.RequestCompleteHandler = NduRequestComplete;
82     Chars.ReceiveHandler = NduReceive;
83     Chars.ReceiveCompleteHandler = NduReceiveComplete;
84     Chars.StatusHandler = NduStatus;
85     Chars.StatusCompleteHandler = NduStatusComplete;
86     Chars.Name = ProtocolName;
87     Chars.BindAdapterHandler = NduBindAdapter;
88     Chars.UnbindAdapterHandler = NduUnbindAdapter;
89 
90     NdisRegisterProtocol(&Status,
91                          &GlobalProtocolHandle,
92                          &Chars,
93                          sizeof(Chars));
94     if (Status != NDIS_STATUS_SUCCESS)
95     {
96         DPRINT1("Failed to register protocol with status 0x%x\n", Status);
97         IoDeleteSymbolicLink(&DosDeviceName);
98         IoDeleteDevice(GlobalDeviceObject);
99         return Status;
100     }
101 
102     DPRINT("NDISUIO: Loaded\n");
103 
104     return STATUS_SUCCESS;
105 }
106