4 * Copyright (C) 2002, 2003, 2004 ReactOS Team
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 /* GLOBAL VARIABLES ***********************************************************/
26 PVIDEO_PORT_DEVICE_EXTENSION ResetDisplayParametersDeviceExtension
= NULL
;
28 /* PRIVATE FUNCTIONS **********************************************************/
31 * Reset display to blue screen
35 IntVideoPortResetDisplayParameters(ULONG Columns
, ULONG Rows
)
37 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
39 if (ResetDisplayParametersDeviceExtension
== NULL
)
42 DriverExtension
= ResetDisplayParametersDeviceExtension
->DriverExtension
;
44 if (DriverExtension
->InitializationData
.HwResetHw
!= NULL
)
46 if (DriverExtension
->InitializationData
.HwResetHw(
47 &ResetDisplayParametersDeviceExtension
->MiniPortDeviceExtension
,
50 ResetDisplayParametersDeviceExtension
= NULL
;
55 ResetDisplayParametersDeviceExtension
= NULL
;
60 IntVideoPortAddDevice(
61 IN PDRIVER_OBJECT DriverObject
,
62 IN PDEVICE_OBJECT PhysicalDeviceObject
)
64 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
65 PDEVICE_OBJECT DeviceObject
;
69 * Get the initialization data we saved in VideoPortInitialize.
72 DriverExtension
= IoGetDriverObjectExtension(DriverObject
, DriverObject
);
75 * Create adapter device object.
78 Status
= IntVideoPortCreateAdapterDeviceObject(
83 if (NT_SUCCESS(Status
))
84 VideoPortDeviceNumber
++;
90 * IntVideoPortDispatchOpen
92 * Answer requests for Open calls.
99 IntVideoPortDispatchOpen(
100 IN PDEVICE_OBJECT DeviceObject
,
103 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
104 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
106 TRACE_(VIDEOPRT
, "IntVideoPortDispatchOpen\n");
108 if (CsrssInitialized
== FALSE
)
111 * We know the first open call will be from the CSRSS process
112 * to let us know its handle.
115 INFO_(VIDEOPRT
, "Referencing CSRSS\n");
116 Csrss
= (PKPROCESS
)PsGetCurrentProcess();
117 INFO_(VIDEOPRT
, "Csrss %p\n", Csrss
);
119 CsrssInitialized
= TRUE
;
121 Irp
->IoStatus
.Information
= FILE_OPENED
;
122 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
124 return STATUS_SUCCESS
;
127 DeviceExtension
= (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
128 DriverExtension
= DeviceExtension
->DriverExtension
;
130 if (DriverExtension
->InitializationData
.HwInitialize(&DeviceExtension
->MiniPortDeviceExtension
))
132 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
134 InterlockedIncrement((PLONG
)&DeviceExtension
->DeviceOpened
);
138 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
141 Irp
->IoStatus
.Information
= FILE_OPENED
;
142 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
144 return STATUS_SUCCESS
;
148 * IntVideoPortDispatchClose
150 * Answer requests for Close calls.
157 IntVideoPortDispatchClose(
158 IN PDEVICE_OBJECT DeviceObject
,
161 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
163 TRACE_(VIDEOPRT
, "IntVideoPortDispatchClose\n");
165 DeviceExtension
= (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
166 if (DeviceExtension
->DeviceOpened
>= 1 &&
167 InterlockedDecrement((PLONG
)&DeviceExtension
->DeviceOpened
) == 0)
169 ResetDisplayParametersDeviceExtension
= NULL
;
170 InbvNotifyDisplayOwnershipLost(NULL
);
171 ResetDisplayParametersDeviceExtension
= DeviceExtension
;
172 IntVideoPortResetDisplayParameters(80, 50);
175 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
176 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
178 return STATUS_SUCCESS
;
182 * IntVideoPortDispatchDeviceControl
184 * Answer requests for device control calls.
191 IntVideoPortDispatchDeviceControl(
192 IN PDEVICE_OBJECT DeviceObject
,
195 PIO_STACK_LOCATION IrpStack
;
196 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
197 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
198 PVIDEO_REQUEST_PACKET vrp
;
201 TRACE_(VIDEOPRT
, "IntVideoPortDispatchDeviceControl\n");
203 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
204 DeviceExtension
= DeviceObject
->DeviceExtension
;
205 DriverExtension
= DeviceExtension
->DriverExtension
;
207 /* Translate the IRP to a VRP */
208 vrp
= ExAllocatePool(NonPagedPool
, sizeof(VIDEO_REQUEST_PACKET
));
211 return STATUS_NO_MEMORY
;
214 vrp
->StatusBlock
= (PSTATUS_BLOCK
)&(Irp
->IoStatus
);
215 vrp
->IoControlCode
= IrpStack
->Parameters
.DeviceIoControl
.IoControlCode
;
217 INFO_(VIDEOPRT
, "- IoControlCode: %x\n", vrp
->IoControlCode
);
219 /* We're assuming METHOD_BUFFERED */
220 vrp
->InputBuffer
= Irp
->AssociatedIrp
.SystemBuffer
;
221 vrp
->InputBufferLength
= IrpStack
->Parameters
.DeviceIoControl
.InputBufferLength
;
222 vrp
->OutputBuffer
= Irp
->AssociatedIrp
.SystemBuffer
;
223 vrp
->OutputBufferLength
= IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
;
225 /* Call the Miniport Driver with the VRP */
226 DriverExtension
->InitializationData
.HwStartIO(
227 &DeviceExtension
->MiniPortDeviceExtension
,
233 INFO_(VIDEOPRT
, "- Returned status: %x\n", Irp
->IoStatus
.Status
);
235 if (Irp
->IoStatus
.Status
!= STATUS_SUCCESS
)
237 /* Map from win32 error codes to NT status values. */
238 switch (Irp
->IoStatus
.Status
)
240 case ERROR_NOT_ENOUGH_MEMORY
: Irp
->IoStatus
.Status
= STATUS_INSUFFICIENT_RESOURCES
; break;
241 case ERROR_MORE_DATA
: Irp
->IoStatus
.Status
= STATUS_BUFFER_OVERFLOW
; break;
242 case ERROR_INVALID_FUNCTION
: Irp
->IoStatus
.Status
= STATUS_NOT_IMPLEMENTED
; break;
243 case ERROR_INVALID_PARAMETER
: Irp
->IoStatus
.Status
= STATUS_INVALID_PARAMETER
; break;
244 case ERROR_INSUFFICIENT_BUFFER
: Irp
->IoStatus
.Status
= STATUS_BUFFER_TOO_SMALL
; break;
245 case ERROR_DEV_NOT_EXIST
: Irp
->IoStatus
.Status
= STATUS_DEVICE_DOES_NOT_EXIST
; break;
246 case ERROR_IO_PENDING
: Irp
->IoStatus
.Status
= STATUS_PENDING
; break;
250 Status
= Irp
->IoStatus
.Status
;
251 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
259 * This is a bit of a hack. We want to take ownership of the display as late
260 * as possible, just before the switch to graphics mode. Win32k knows when
261 * this happens, we don't. So we need Win32k to inform us. This could be done
262 * using an IOCTL, but there's no way of knowing which IOCTL codes are unused
263 * in the communication between GDI driver and miniport driver. So we use
264 * IRP_MJ_WRITE as the signal that win32k is ready to switch to graphics mode,
265 * since we know for certain that there is no read/write activity going on
266 * between GDI and miniport drivers.
267 * We don't actually need the data that is passed, we just trigger on the fact
268 * that an IRP_MJ_WRITE was sent.
275 IntVideoPortDispatchWrite(
276 IN PDEVICE_OBJECT DeviceObject
,
279 PIO_STACK_LOCATION piosStack
= IoGetCurrentIrpStackLocation(Irp
);
280 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
283 DeviceExtension
= (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
286 * Storing the device extension pointer in a static variable is an
287 * ugly hack. Unfortunately, we need it in IntVideoPortResetDisplayParameters
288 * and InbvNotifyDisplayOwnershipLost doesn't allow us to pass a userdata
289 * parameter. On the bright side, the DISPLAY device is opened
290 * exclusively, so there can be only one device extension active at
293 * FIXME: We should process all opened display devices in
294 * IntVideoPortResetDisplayParameters.
297 ResetDisplayParametersDeviceExtension
= DeviceExtension
;
298 InbvNotifyDisplayOwnershipLost(IntVideoPortResetDisplayParameters
);
300 nErrCode
= STATUS_SUCCESS
;
301 Irp
->IoStatus
.Information
= piosStack
->Parameters
.Write
.Length
;
302 Irp
->IoStatus
.Status
= nErrCode
;
303 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
310 IntVideoPortPnPStartDevice(
311 IN PDEVICE_OBJECT DeviceObject
,
314 PIO_STACK_LOCATION Stack
= IoGetCurrentIrpStackLocation(Irp
);
315 PDRIVER_OBJECT DriverObject
;
316 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
317 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
318 PCM_RESOURCE_LIST AllocatedResources
;
321 * Get the initialization data we saved in VideoPortInitialize.
324 DriverObject
= DeviceObject
->DriverObject
;
325 DriverExtension
= IoGetDriverObjectExtension(DriverObject
, DriverObject
);
326 DeviceExtension
= (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
329 * Store some resources in the DeviceExtension.
332 AllocatedResources
= Stack
->Parameters
.StartDevice
.AllocatedResources
;
333 if (AllocatedResources
!= NULL
)
335 CM_FULL_RESOURCE_DESCRIPTOR
*FullList
;
336 CM_PARTIAL_RESOURCE_DESCRIPTOR
*Descriptor
;
338 ULONG ResourceListSize
;
340 /* Save the resource list */
341 ResourceCount
= AllocatedResources
->List
[0].PartialResourceList
.Count
;
343 FIELD_OFFSET(CM_RESOURCE_LIST
, List
[0].PartialResourceList
.
344 PartialDescriptors
[ResourceCount
]);
345 DeviceExtension
->AllocatedResources
= ExAllocatePool(PagedPool
, ResourceListSize
);
346 if (DeviceExtension
->AllocatedResources
== NULL
)
348 return STATUS_INSUFFICIENT_RESOURCES
;
351 RtlCopyMemory(DeviceExtension
->AllocatedResources
,
355 /* Get the interrupt level/vector - needed by HwFindAdapter sometimes */
356 for (FullList
= AllocatedResources
->List
;
357 FullList
< AllocatedResources
->List
+ AllocatedResources
->Count
;
360 INFO_(VIDEOPRT
, "InterfaceType %u BusNumber List %u Device BusNumber %u Version %u Revision %u\n",
361 FullList
->InterfaceType
, FullList
->BusNumber
, DeviceExtension
->SystemIoBusNumber
, FullList
->PartialResourceList
.Version
, FullList
->PartialResourceList
.Revision
);
363 /* FIXME: Is this ASSERT ok for resources from the PNP manager? */
364 ASSERT(FullList
->InterfaceType
== PCIBus
);
365 ASSERT(FullList
->BusNumber
== DeviceExtension
->SystemIoBusNumber
);
366 ASSERT(1 == FullList
->PartialResourceList
.Version
);
367 ASSERT(1 == FullList
->PartialResourceList
.Revision
);
368 for (Descriptor
= FullList
->PartialResourceList
.PartialDescriptors
;
369 Descriptor
< FullList
->PartialResourceList
.PartialDescriptors
+ FullList
->PartialResourceList
.Count
;
372 if (Descriptor
->Type
== CmResourceTypeInterrupt
)
374 DeviceExtension
->InterruptLevel
= Descriptor
->u
.Interrupt
.Level
;
375 DeviceExtension
->InterruptVector
= Descriptor
->u
.Interrupt
.Vector
;
376 if (Descriptor
->ShareDisposition
== CmResourceShareShared
)
377 DeviceExtension
->InterruptShared
= TRUE
;
379 DeviceExtension
->InterruptShared
= FALSE
;
384 INFO_(VIDEOPRT
, "Interrupt level: 0x%x Interrupt Vector: 0x%x\n",
385 DeviceExtension
->InterruptLevel
,
386 DeviceExtension
->InterruptVector
);
389 * Create adapter device object.
392 return IntVideoPortFindAdapter(
401 IntVideoPortForwardIrpAndWaitCompletionRoutine(
406 PKEVENT Event
= Context
;
408 if (Irp
->PendingReturned
)
409 KeSetEvent(Event
, IO_NO_INCREMENT
, FALSE
);
411 return STATUS_MORE_PROCESSING_REQUIRED
;
417 IntVideoPortForwardIrpAndWait(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
421 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
=
422 (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
424 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
425 IoCopyCurrentIrpStackLocationToNext(Irp
);
426 IoSetCompletionRoutine(Irp
, IntVideoPortForwardIrpAndWaitCompletionRoutine
,
427 &Event
, TRUE
, TRUE
, TRUE
);
428 Status
= IoCallDriver(DeviceExtension
->NextDeviceObject
, Irp
);
429 if (Status
== STATUS_PENDING
)
431 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
432 Status
= Irp
->IoStatus
.Status
;
439 IntVideoPortDispatchPnp(
440 IN PDEVICE_OBJECT DeviceObject
,
443 PIO_STACK_LOCATION IrpSp
;
446 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
448 switch (IrpSp
->MinorFunction
)
450 case IRP_MN_START_DEVICE
:
451 Status
= IntVideoPortForwardIrpAndWait(DeviceObject
, Irp
);
452 if (NT_SUCCESS(Status
) && NT_SUCCESS(Irp
->IoStatus
.Status
))
453 Status
= IntVideoPortPnPStartDevice(DeviceObject
, Irp
);
454 Irp
->IoStatus
.Status
= Status
;
455 Irp
->IoStatus
.Information
= 0;
456 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
460 case IRP_MN_REMOVE_DEVICE
:
461 case IRP_MN_QUERY_REMOVE_DEVICE
:
462 case IRP_MN_CANCEL_REMOVE_DEVICE
:
463 case IRP_MN_SURPRISE_REMOVAL
:
465 case IRP_MN_STOP_DEVICE
:
466 Status
= IntVideoPortForwardIrpAndWait(DeviceObject
, Irp
);
467 if (NT_SUCCESS(Status
) && NT_SUCCESS(Irp
->IoStatus
.Status
))
468 Status
= STATUS_SUCCESS
;
469 Irp
->IoStatus
.Status
= Status
;
470 Irp
->IoStatus
.Information
= 0;
471 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
474 case IRP_MN_QUERY_STOP_DEVICE
:
475 case IRP_MN_CANCEL_STOP_DEVICE
:
476 Status
= STATUS_SUCCESS
;
477 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
478 Irp
->IoStatus
.Information
= 0;
479 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
483 Status
= Irp
->IoStatus
.Status
;
484 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
492 IntVideoPortDispatchCleanup(
493 IN PDEVICE_OBJECT DeviceObject
,
496 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
498 DeviceExtension
= DeviceObject
->DeviceExtension
;
499 RtlFreeUnicodeString(&DeviceExtension
->RegistryPath
);
501 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
502 Irp
->IoStatus
.Information
= 0;
503 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
505 return STATUS_SUCCESS
;
509 IntVideoPortDispatchPower(
510 IN PDEVICE_OBJECT DeviceObject
,
513 return STATUS_NOT_IMPLEMENTED
;
517 IntVideoPortDispatchSystemControl(
518 IN PDEVICE_OBJECT DeviceObject
,
521 return STATUS_NOT_IMPLEMENTED
;
525 IntVideoPortUnload(PDRIVER_OBJECT DriverObject
)