Moved and renamed some ReactOS specific macros
[reactos.git] / reactos / drivers / dd / debugout / debugout.c
1 /* $Id: debugout.c,v 1.2 2003/11/17 02:12:49 hyperion Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: drivers/dd/debugout.c
6 * PURPOSE: Debug output device driver
7 * PROGRAMMER: Ge van Geldorp (ge@gse.nl)
8 * UPDATE HISTORY:
9 * 2003/05/22: Created
10 * NOTES:
11 * In your usermode application, do something like this:
12 *
13 * DebugHandle = CreateFile("\\\\.\\DebugOut",
14 * GENERIC_WRITE,
15 * 0,
16 * NULL,
17 * OPEN_EXISTING,
18 * FILE_ATTRIBUTE_NORMAL,
19 * NULL);
20 *
21 * and write to your hearts content to DebugHandle.
22 */
23
24 /* INCLUDES */
25 #include <ddk/ntddk.h>
26 #include <rosrtl/string.h>
27
28 /* FUNCTIONS */
29 NTSTATUS STDCALL_FUNC
30 DebugOutDispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
31 {
32 PIO_STACK_LOCATION piosStack = IoGetCurrentIrpStackLocation(Irp);
33 NTSTATUS nErrCode;
34 char *Start;
35 char Buf[513];
36 unsigned Remaining;
37 unsigned Length;
38
39 nErrCode = STATUS_SUCCESS;
40
41 switch(piosStack->MajorFunction)
42 {
43 /* opening and closing handles to the device */
44 case IRP_MJ_CREATE:
45 case IRP_MJ_CLOSE:
46 break;
47
48 /* write data */
49 case IRP_MJ_WRITE:
50 Remaining = piosStack->Parameters.Write.Length;
51 Start = Irp->AssociatedIrp.SystemBuffer;
52 while (0 < Remaining)
53 {
54 Length = Remaining;
55 if (sizeof(Buf) - 1 < Length)
56 {
57 Length = sizeof(Buf) - 1;
58 }
59 RtlCopyMemory(Buf, Start, Length);
60 Buf[Length] = '\0';
61 DbgPrint("%s", Buf);
62 Remaining -= Length;
63 Start += Length;
64 }
65
66 Irp->IoStatus.Information = piosStack->Parameters.Write.Length;
67 break;
68
69 /* read data */
70 case IRP_MJ_READ:
71 Irp->IoStatus.Information = 0;
72 nErrCode = STATUS_END_OF_FILE;
73 break;
74
75 /* unsupported operations */
76 default:
77 nErrCode = STATUS_NOT_IMPLEMENTED;
78 }
79
80 Irp->IoStatus.Status = nErrCode;
81 IoCompleteRequest(Irp, IO_NO_INCREMENT);
82
83 return nErrCode;
84 }
85
86 NTSTATUS STDCALL
87 DebugOutUnload(PDRIVER_OBJECT DriverObject)
88 {
89 return STATUS_SUCCESS;
90 }
91
92 NTSTATUS STDCALL
93 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
94 {
95 PDEVICE_OBJECT DebugOutDevice;
96 UNICODE_STRING DeviceName;
97 UNICODE_STRING DosName;
98 NTSTATUS Status;
99
100 /* register driver routines */
101 DriverObject->MajorFunction[IRP_MJ_CLOSE] = (PDRIVER_DISPATCH) DebugOutDispatch;
102 DriverObject->MajorFunction[IRP_MJ_CREATE] = (PDRIVER_DISPATCH) DebugOutDispatch;
103 DriverObject->MajorFunction[IRP_MJ_WRITE] = (PDRIVER_DISPATCH) DebugOutDispatch;
104 DriverObject->MajorFunction[IRP_MJ_READ] = (PDRIVER_DISPATCH) DebugOutDispatch;
105 /* DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] = (PDRIVER_DISPATCH) DebugOutDispatch; */
106 DriverObject->DriverUnload = (PDRIVER_UNLOAD) DebugOutUnload;
107
108 /* create device */
109 RtlRosInitUnicodeStringFromLiteral(&DeviceName, L"\\Device\\DebugOut");
110
111 Status = IoCreateDevice(DriverObject, 0, &DeviceName, FILE_DEVICE_NULL,
112 0, FALSE, &DebugOutDevice);
113 if (! NT_SUCCESS(Status))
114 {
115 return Status;
116 }
117
118 RtlRosInitUnicodeStringFromLiteral(&DosName, L"\\DosDevices\\DebugOut");
119 Status = IoCreateSymbolicLink(&DosName, &DeviceName);
120 if (! NT_SUCCESS(Status))
121 {
122 IoDeleteDevice(DebugOutDevice);
123 return Status;
124 }
125
126 DebugOutDevice->Flags |= DO_BUFFERED_IO;
127
128 return Status;
129 }
130
131 /* EOF */