Debian Codegraph @

src/win64/rwp/rwports.c

raw symbols SCIP
1 /******************************************************************************
2 * rwports.c: ioports hardware access under Windows NT/2000/XP and 64-bits
3 * Developped from MPlayer's DhaHelper driver and modified by:
4 * Copyright (c) 2007 Romain Lievin <[email protected]>.
5 * Patched to compile with MinGW by Kevin Kofler:
6 * Copyright (c) 2007 Kevin Kofler
7 *
8 * This file is part of the TiLP/TiEmu projects.
9 *
10 * rwports is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * rwports is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with rwports; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 *****************************************************************************/
25
26
27 #if defined(_MSC_VER)
28 # include <ntddk.h>
29 # ifndef STDCALL
30 # define STDCALL /* nothing */
31 # endif
32 #elif defined(__MINGW32__)
33 # include <ddk/ntddk.h>
34 #else
35 # error Unsupported compiler. This driver requires MSVC+DDK or MinGW to build.
36 #endif
37
38 #include "rwports.h"
39
40 #define OutputDebugString DbgPrint
41
42 static STDCALL NTSTATUS RwPortsDispatch(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp);
43 static STDCALL void RwPortsUnload(IN PDRIVER_OBJECT DriverObject);
44
45 //
46 // Driver entry point
47 //
48 STDCALL NTSTATUS DriverEntry (IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath)
49 {
50 UNICODE_STRING DeviceNameUnicodeString;
51 UNICODE_STRING DeviceLinkUnicodeString;
52 NTSTATUS ntStatus;
53 PDEVICE_OBJECT DeviceObject = NULL;
54
55 OutputDebugString ("rwports: entering DriverEntry");
56
57 RtlInitUnicodeString (&DeviceNameUnicodeString, L"\\Device\\rwports");
58
59 // Create an EXCLUSIVE device object (only 1 thread at a time
60 // can make requests to this device).
61
62 ntStatus = IoCreateDevice(DriverObject,0,&DeviceNameUnicodeString,FILE_DEVICE_RWPORTS,0,TRUE,&DeviceObject);
63
64 if (NT_SUCCESS(ntStatus))
65 {
66 // Create dispatch points for device control, create, close.
67 DriverObject->MajorFunction[IRP_MJ_CREATE] =
68 DriverObject->MajorFunction[IRP_MJ_CLOSE] =
69 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = RwPortsDispatch;
70 DriverObject->DriverUnload = RwPortsUnload;
71
72 // Create a symbolic link, e.g. a name that a Win32 app can specify
73 // to open the device.
74
75 RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\rwports");
76
77 ntStatus = IoCreateSymbolicLink(&DeviceLinkUnicodeString,&DeviceNameUnicodeString);
78
79 if (!NT_SUCCESS(ntStatus)){
80 // Symbolic link creation failed- note this & then delete the
81 // device object (it's useless if a Win32 app can't get at it).
82 OutputDebugString ("rwports: IoCreateSymbolicLink failed");
83 IoDeleteDevice (DeviceObject);
84 }
85 }
86 else
87 {
88 OutputDebugString ("rwports: IoCreateDevice failed");
89 }
90
91 OutputDebugString ("rwports: leaving DriverEntry");
92 return ntStatus;
93 }
94
95 //
96 // Dispatch: process the IRPs sent to this device
97 //
98 static STDCALL NTSTATUS RwPortsDispatch(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
99 {
100 PIO_STACK_LOCATION IrpStack;
101 ULONG dwInputBufferLength;
102 ULONG dwOutputBufferLength;
103 ULONG dwIoControlCode;
104 PVOID pvIoBuffer;
105 NTSTATUS ntStatus;
106
107 PUCHAR p8IoBuffer;
108 PUSHORT p16IoBuffer;
109 PULONG p32IoBuffer;
110
111 OutputDebugString ("rwports: entering RwPortsDispatch");
112
113 // Init to default settings
114
115 Irp->IoStatus.Status = STATUS_SUCCESS;
116 Irp->IoStatus.Information = 0;
117
118 IrpStack = IoGetCurrentIrpStackLocation(Irp);
119
120 // Get the pointer to the input/output buffer and it's length
121
122 pvIoBuffer = Irp->AssociatedIrp.SystemBuffer;
123 dwInputBufferLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
124 dwOutputBufferLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
125
126 p8IoBuffer = (PUCHAR)pvIoBuffer;
127 p16IoBuffer = (PUSHORT)pvIoBuffer;
128 p32IoBuffer = (PULONG)pvIoBuffer;
129
130 switch (IrpStack->MajorFunction)
131 {
132 case IRP_MJ_CREATE:
133 OutputDebugString("rwports: IRP_MJ_CREATE");
134 break;
135 case IRP_MJ_CLOSE:
136 OutputDebugString("rwports: IRP_MJ_CLOSE");
137 break;
138 case IRP_MJ_DEVICE_CONTROL:
139 OutputDebugString("rwports: IRP_MJ_DEVICE_CONTROL");
140 dwIoControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
141
142 switch (dwIoControlCode)
143 {
144 case IOCTL_RWPORTS_READ_BYTE:
145 OutputDebugString("rwports: IOCTL_RWPORTS_READ_BYTE");
146
147 if ((dwInputBufferLength >= 2) && (dwOutputBufferLength >= 1))
148 {
149 OutputDebugString("rwports: read at 0x%x", p16IoBuffer[0]);
150
151 *p8IoBuffer = READ_PORT_UCHAR((PUCHAR)p16IoBuffer[0]);
152
153 Irp->IoStatus.Information = 1; // 1 byte written
154 }
155 else
156 ntStatus = STATUS_BUFFER_TOO_SMALL;
157 break;
158
159 case IOCTL_RWPORTS_WRITE_BYTE:
160 OutputDebugString("rwports: IOCTL_RWPORTS_WRITE_BYTE");
161
162 if (dwInputBufferLength >= 3)
163 {
164 OutputDebugString("rwports: write at 0x%x", p16IoBuffer[0]);
165
166 WRITE_PORT_UCHAR((PUCHAR)p16IoBuffer[0], p8IoBuffer[2]);
167
168 Irp->IoStatus.Information = 0; // 0 bytes written
169 }
170 else
171 ntStatus = STATUS_BUFFER_TOO_SMALL;
172 break;
173
174 default:
175 OutputDebugString("rwports: unknown IRP_MJ_DEVICE_CONTROL");
176 Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
177 break;
178 }
179 break;
180 }
181
182 // DON'T get cute and try to use the status field of the irp in the
183 // return status. That IRP IS GONE as soon as you call IoCompleteRequest.
184
185 ntStatus = Irp->IoStatus.Status;
186 IoCompleteRequest (Irp, IO_NO_INCREMENT);
187
188 // We never have pending operation so always return the status code.
189
190 OutputDebugString("rwports: leaving RwPortsDispatch");
191
192 return ntStatus;
193 }
194
195 //
196 // Delete the associated device and return
197 //
198 static STDCALL void RwPortsUnload(IN PDRIVER_OBJECT DriverObject)
199 {
200 UNICODE_STRING DeviceLinkUnicodeString;
201 NTSTATUS ntStatus=STATUS_SUCCESS;
202 OutputDebugString ("rwports: entering RwPortsUnload");
203 OutputDebugString ("rwports: unmapping remaining memory");
204
205 RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\rwports");
206 ntStatus = IoDeleteSymbolicLink (&DeviceLinkUnicodeString);
207
208 if (NT_SUCCESS(ntStatus))
209 {
210 IoDeleteDevice (DriverObject->DeviceObject);
211 }
212 else
213 {
214 OutputDebugString ("rwports: IoDeleteSymbolicLink failed");
215 }
216 OutputDebugString ("rwports: leaving RwPortsUnload");
217 }
218