update for HEAD-2003091401
[reactos.git] / drivers / dd / debugout / debugout.c
1 /* $Id$
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
27 /* FUNCTIONS */
28 NTSTATUS STDCALL_FUNC
29 DebugOutDispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
30 {
31   PIO_STACK_LOCATION piosStack = IoGetCurrentIrpStackLocation(Irp);
32   NTSTATUS nErrCode;
33   char *Start;
34   char Buf[513];
35   unsigned Remaining;
36   unsigned Length;
37  
38   nErrCode = STATUS_SUCCESS;
39
40   switch(piosStack->MajorFunction)
41     {
42       /* opening and closing handles to the device */
43       case IRP_MJ_CREATE:
44       case IRP_MJ_CLOSE:
45         break;
46
47       /* write data */
48       case IRP_MJ_WRITE:
49         Remaining = piosStack->Parameters.Write.Length;
50         Start = Irp->AssociatedIrp.SystemBuffer;
51         while (0 < Remaining)
52           {
53             Length = Remaining;
54             if (sizeof(Buf) - 1 < Length)
55               {
56                 Length = sizeof(Buf) - 1;
57               }
58             RtlCopyMemory(Buf, Start, Length);
59             Buf[Length] = '\0';
60             DbgPrint("%s", Buf);
61             Remaining -= Length;
62             Start += Length;
63           }
64
65         Irp->IoStatus.Information = piosStack->Parameters.Write.Length;
66         break;
67
68       /* read data */
69       case IRP_MJ_READ:
70         Irp->IoStatus.Information = 0;
71         nErrCode = STATUS_END_OF_FILE;
72         break;
73
74       /* unsupported operations */
75       default:
76         nErrCode = STATUS_NOT_IMPLEMENTED;
77     }
78
79   Irp->IoStatus.Status = nErrCode;
80   IoCompleteRequest(Irp, IO_NO_INCREMENT);
81
82   return nErrCode;
83 }
84
85 NTSTATUS STDCALL
86 DebugOutUnload(PDRIVER_OBJECT DriverObject)
87 {
88   return STATUS_SUCCESS;
89 }
90
91 NTSTATUS STDCALL 
92 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
93 {
94   PDEVICE_OBJECT DebugOutDevice;
95   UNICODE_STRING DeviceName;
96   UNICODE_STRING DosName;
97   NTSTATUS Status;
98
99   /* register driver routines */
100   DriverObject->MajorFunction[IRP_MJ_CLOSE] = (PDRIVER_DISPATCH) DebugOutDispatch;
101   DriverObject->MajorFunction[IRP_MJ_CREATE] = (PDRIVER_DISPATCH) DebugOutDispatch;
102   DriverObject->MajorFunction[IRP_MJ_WRITE] = (PDRIVER_DISPATCH) DebugOutDispatch;
103   DriverObject->MajorFunction[IRP_MJ_READ] = (PDRIVER_DISPATCH) DebugOutDispatch;
104   /* DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] = (PDRIVER_DISPATCH) DebugOutDispatch; */
105   DriverObject->DriverUnload = (PDRIVER_UNLOAD) DebugOutUnload;
106
107   /* create device */
108   RtlInitUnicodeStringFromLiteral(&DeviceName, L"\\Device\\DebugOut");
109
110   Status = IoCreateDevice(DriverObject, 0, &DeviceName, FILE_DEVICE_NULL,
111                             0, FALSE, &DebugOutDevice);
112   if (! NT_SUCCESS(Status))
113     {
114       return Status;
115     }
116
117   RtlInitUnicodeStringFromLiteral(&DosName, L"\\DosDevices\\DebugOut");
118   Status = IoCreateSymbolicLink(&DosName, &DeviceName);
119   if (! NT_SUCCESS(Status))
120     {
121     IoDeleteDevice(DebugOutDevice);
122     return Status;
123     }
124
125   DebugOutDevice->Flags |= DO_BUFFERED_IO;
126
127   return Status;
128 }
129
130 /* EOF */