Moved and renamed some ReactOS specific macros
[reactos.git] / reactos / drivers / dd / null / null.c
1 /* $Id: null.c,v 1.12 2003/11/17 02:12:49 hyperion Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: services/null/null.c
6 * PURPOSE: NULL device driver
7 * PROGRAMMER: David Welch (welch@mcmail.com)
8 * UPDATE HISTORY:
9 * 13/08/1998: Created
10 * 29/04/2002: Fixed bugs, added zero-stream device
11 */
12
13 /* INCLUDES */
14 #include <ddk/ntddk.h>
15 #include <rosrtl/string.h>
16 #include "null.h"
17
18 /* OBJECTS */
19 static const NULL_EXTENSION nxNull = NullBitBucket;
20 static const NULL_EXTENSION nxZero = NullZeroStream;
21
22 /* FUNCTIONS */
23 NTSTATUS STDCALL
24 NullDispatch(PDEVICE_OBJECT DeviceObject, PIRP Irp)
25 {
26 PIO_STACK_LOCATION piosStack = IoGetCurrentIrpStackLocation(Irp);
27 NTSTATUS nErrCode;
28
29 nErrCode = STATUS_SUCCESS;
30
31 switch(piosStack->MajorFunction)
32 {
33 /* opening and closing handles to the device */
34 case IRP_MJ_CREATE:
35 case IRP_MJ_CLOSE:
36 {
37 break;
38 }
39
40 /* write data */
41 case IRP_MJ_WRITE:
42 {
43 switch(NULL_DEVICE_TYPE(DeviceObject))
44 {
45 case NullBitBucket:
46 Irp->IoStatus.Information = piosStack->Parameters.Write.Length;
47 break;
48
49 case NullZeroStream:
50 default:
51 Irp->IoStatus.Information = 0;
52 nErrCode = STATUS_NOT_IMPLEMENTED;
53 }
54
55 break;
56 }
57
58 /* read data */
59 case IRP_MJ_READ:
60 {
61 switch(NULL_DEVICE_TYPE(DeviceObject))
62 {
63 case NullBitBucket:
64 Irp->IoStatus.Information = 0;
65 nErrCode = STATUS_END_OF_FILE;
66 break;
67
68 case NullZeroStream:
69 RtlZeroMemory(Irp->AssociatedIrp.SystemBuffer, piosStack->Parameters.Read.Length);
70 Irp->IoStatus.Information = piosStack->Parameters.Read.Length;
71 break;
72
73 default:
74 Irp->IoStatus.Information = 0;
75 nErrCode = STATUS_NOT_IMPLEMENTED;
76 }
77
78 break;
79 }
80
81 /* unsupported operations */
82 default:
83 {
84 nErrCode = STATUS_NOT_IMPLEMENTED;
85 }
86 }
87
88 Irp->IoStatus.Status = nErrCode;
89 IoCompleteRequest(Irp, IO_NO_INCREMENT);
90
91 return (nErrCode);
92 }
93
94 NTSTATUS STDCALL
95 NullUnload(PDRIVER_OBJECT DriverObject)
96 {
97 return(STATUS_SUCCESS);
98 }
99
100 NTSTATUS STDCALL
101 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
102 {
103 PDEVICE_OBJECT pdoNullDevice;
104 PDEVICE_OBJECT pdoZeroDevice;
105 UNICODE_STRING wstrDeviceName;
106 NTSTATUS nErrCode;
107
108 /* register driver routines */
109 DriverObject->MajorFunction[IRP_MJ_CLOSE] = (PDRIVER_DISPATCH)NullDispatch;
110 DriverObject->MajorFunction[IRP_MJ_CREATE] = (PDRIVER_DISPATCH)NullDispatch;
111 DriverObject->MajorFunction[IRP_MJ_WRITE] = (PDRIVER_DISPATCH)NullDispatch;
112 DriverObject->MajorFunction[IRP_MJ_READ] = (PDRIVER_DISPATCH)NullDispatch;
113 /* DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] = (PDRIVER_DISPATCH)NullDispatch; */
114 DriverObject->DriverUnload = (PDRIVER_UNLOAD)NullUnload;
115
116 /* create null device */
117 RtlRosInitUnicodeStringFromLiteral(&wstrDeviceName, L"\\Device\\Null");
118
119 nErrCode = IoCreateDevice
120 (
121 DriverObject,
122 sizeof(NULL_EXTENSION),
123 &wstrDeviceName,
124 FILE_DEVICE_NULL,
125 0,
126 FALSE,
127 &pdoNullDevice
128 );
129
130 /* failure */
131 if(!NT_SUCCESS(nErrCode))
132 {
133 return (nErrCode);
134 }
135
136 pdoNullDevice->DeviceExtension = (PVOID)&nxNull;
137
138 /* create zero device */
139 RtlRosInitUnicodeStringFromLiteral(&wstrDeviceName, L"\\Device\\Zero");
140
141 nErrCode = IoCreateDevice
142 (
143 DriverObject,
144 sizeof(NULL_EXTENSION),
145 &wstrDeviceName,
146 FILE_DEVICE_NULL,
147 FILE_READ_ONLY_DEVICE, /* zero device is read-only */
148 FALSE,
149 &pdoZeroDevice
150 );
151
152 /* failure */
153 if(!NT_SUCCESS(nErrCode))
154 {
155 IoDeleteDevice(pdoNullDevice);
156 return (nErrCode);
157 }
158
159 pdoZeroDevice->DeviceExtension = (PVOID)&nxZero;
160 pdoZeroDevice->Flags |= DO_BUFFERED_IO;
161
162 return (nErrCode);
163 }
164
165 /* EOF */