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 Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 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 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; see the file COPYING.LIB.
18 * If not, write to the Free Software Foundation,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 /* EXTERNAL FUNCTIONS *********************************************************/
27 VOID NTAPI
HalAcquireDisplayOwnership(IN PHAL_RESET_DISPLAY_PARAMETERS ResetDisplayParameters
);
28 VOID NTAPI
HalReleaseDisplayOwnership();
30 /* GLOBAL VARIABLES ***********************************************************/
32 PVIDEO_PORT_DEVICE_EXTENSION ResetDisplayParametersDeviceExtension
= NULL
;
34 /* PRIVATE FUNCTIONS **********************************************************/
37 * Reset display to blue screen
41 IntVideoPortResetDisplayParameters(ULONG Columns
, ULONG Rows
)
43 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
45 if (ResetDisplayParametersDeviceExtension
== NULL
)
48 DriverExtension
= ResetDisplayParametersDeviceExtension
->DriverExtension
;
50 if (DriverExtension
->InitializationData
.HwResetHw
!= NULL
)
52 if (DriverExtension
->InitializationData
.HwResetHw(
53 &ResetDisplayParametersDeviceExtension
->MiniPortDeviceExtension
,
56 ResetDisplayParametersDeviceExtension
= NULL
;
61 ResetDisplayParametersDeviceExtension
= NULL
;
66 IntVideoPortAddDevice(
67 IN PDRIVER_OBJECT DriverObject
,
68 IN PDEVICE_OBJECT PhysicalDeviceObject
)
70 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
73 * Get the initialization data we saved in VideoPortInitialize.
76 DriverExtension
= IoGetDriverObjectExtension(DriverObject
, DriverObject
);
79 * Create adapter device object.
82 return IntVideoPortCreateAdapterDeviceObject(
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 DPRINT("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 DPRINT("Referencing CSRSS\n");
116 Csrss
= (PKPROCESS
)PsGetCurrentProcess();
117 DPRINT("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 DPRINT("IntVideoPortDispatchClose\n");
165 DeviceExtension
= (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
166 if (DeviceExtension
->DeviceOpened
>= 1 &&
167 InterlockedDecrement((PLONG
)&DeviceExtension
->DeviceOpened
) == 0)
169 ResetDisplayParametersDeviceExtension
= DeviceExtension
;
170 HalReleaseDisplayOwnership();
173 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
174 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
176 return STATUS_SUCCESS
;
180 * IntVideoPortDispatchDeviceControl
182 * Answer requests for device control calls.
189 IntVideoPortDispatchDeviceControl(
190 IN PDEVICE_OBJECT DeviceObject
,
193 PIO_STACK_LOCATION IrpStack
;
194 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
195 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
196 PVIDEO_REQUEST_PACKET vrp
;
199 DPRINT("IntVideoPortDispatchDeviceControl\n");
201 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
202 DeviceExtension
= DeviceObject
->DeviceExtension
;
203 DriverExtension
= DeviceExtension
->DriverExtension
;
205 /* Translate the IRP to a VRP */
206 vrp
= ExAllocatePool(NonPagedPool
, sizeof(VIDEO_REQUEST_PACKET
));
209 return STATUS_NO_MEMORY
;
212 vrp
->StatusBlock
= (PSTATUS_BLOCK
)&(Irp
->IoStatus
);
213 vrp
->IoControlCode
= IrpStack
->Parameters
.DeviceIoControl
.IoControlCode
;
215 DPRINT("- IoControlCode: %x\n", vrp
->IoControlCode
);
217 /* We're assuming METHOD_BUFFERED */
218 vrp
->InputBuffer
= Irp
->AssociatedIrp
.SystemBuffer
;
219 vrp
->InputBufferLength
= IrpStack
->Parameters
.DeviceIoControl
.InputBufferLength
;
220 vrp
->OutputBuffer
= Irp
->AssociatedIrp
.SystemBuffer
;
221 vrp
->OutputBufferLength
= IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
;
223 /* Call the Miniport Driver with the VRP */
224 DriverExtension
->InitializationData
.HwStartIO(
225 &DeviceExtension
->MiniPortDeviceExtension
,
231 DPRINT("- Returned status: %x\n", Irp
->IoStatus
.Status
);
233 if (Irp
->IoStatus
.Status
!= STATUS_SUCCESS
)
235 /* Map from win32 error codes to NT status values. */
236 switch (Irp
->IoStatus
.Status
)
238 case ERROR_NOT_ENOUGH_MEMORY
: Irp
->IoStatus
.Status
= STATUS_INSUFFICIENT_RESOURCES
; break;
239 case ERROR_MORE_DATA
: Irp
->IoStatus
.Status
= STATUS_BUFFER_OVERFLOW
; break;
240 case ERROR_INVALID_FUNCTION
: Irp
->IoStatus
.Status
= STATUS_NOT_IMPLEMENTED
; break;
241 case ERROR_INVALID_PARAMETER
: Irp
->IoStatus
.Status
= STATUS_INVALID_PARAMETER
; break;
242 case ERROR_INSUFFICIENT_BUFFER
: Irp
->IoStatus
.Status
= STATUS_BUFFER_TOO_SMALL
; break;
243 case ERROR_DEV_NOT_EXIST
: Irp
->IoStatus
.Status
= STATUS_DEVICE_DOES_NOT_EXIST
; break;
244 case ERROR_IO_PENDING
: Irp
->IoStatus
.Status
= STATUS_PENDING
; break;
248 Status
= Irp
->IoStatus
.Status
;
249 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
257 * This is a bit of a hack. We want to take ownership of the display as late
258 * as possible, just before the switch to graphics mode. Win32k knows when
259 * this happens, we don't. So we need Win32k to inform us. This could be done
260 * using an IOCTL, but there's no way of knowing which IOCTL codes are unused
261 * in the communication between GDI driver and miniport driver. So we use
262 * IRP_MJ_WRITE as the signal that win32k is ready to switch to graphics mode,
263 * since we know for certain that there is no read/write activity going on
264 * between GDI and miniport drivers.
265 * We don't actually need the data that is passed, we just trigger on the fact
266 * that an IRP_MJ_WRITE was sent.
273 IntVideoPortDispatchWrite(
274 IN PDEVICE_OBJECT DeviceObject
,
277 PIO_STACK_LOCATION piosStack
= IoGetCurrentIrpStackLocation(Irp
);
278 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
281 DeviceExtension
= (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
284 * Storing the device extension pointer in a static variable is an
285 * ugly hack. Unfortunately, we need it in VideoPortResetDisplayParameters
286 * and HalAcquireDisplayOwnership doesn't allow us to pass a userdata
287 * parameter. On the bright side, the DISPLAY device is opened
288 * exclusively, so there can be only one device extension active at
292 ResetDisplayParametersDeviceExtension
= DeviceExtension
;
293 HalAcquireDisplayOwnership(IntVideoPortResetDisplayParameters
);
295 nErrCode
= STATUS_SUCCESS
;
296 Irp
->IoStatus
.Information
= piosStack
->Parameters
.Write
.Length
;
297 Irp
->IoStatus
.Status
= nErrCode
;
298 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
305 IntVideoPortPnPStartDevice(
306 IN PDEVICE_OBJECT DeviceObject
,
309 PIO_STACK_LOCATION Stack
= IoGetCurrentIrpStackLocation(Irp
);
310 PDRIVER_OBJECT DriverObject
;
311 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
312 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
313 PCM_RESOURCE_LIST AllocatedResources
;
316 * Get the initialization data we saved in VideoPortInitialize.
319 DriverObject
= DeviceObject
->DriverObject
;
320 DriverExtension
= IoGetDriverObjectExtension(DriverObject
, DriverObject
);
321 DeviceExtension
= (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
324 * Store some resources in the DeviceExtension.
327 AllocatedResources
= Stack
->Parameters
.StartDevice
.AllocatedResources
;
328 if (AllocatedResources
!= NULL
)
330 CM_FULL_RESOURCE_DESCRIPTOR
*FullList
;
331 CM_PARTIAL_RESOURCE_DESCRIPTOR
*Descriptor
;
333 ULONG ResourceListSize
;
335 /* Save the resource list */
336 ResourceCount
= AllocatedResources
->List
[0].PartialResourceList
.Count
;
338 FIELD_OFFSET(CM_RESOURCE_LIST
, List
[0].PartialResourceList
.
339 PartialDescriptors
[ResourceCount
]);
340 DeviceExtension
->AllocatedResources
= ExAllocatePool(PagedPool
, ResourceListSize
);
341 if (DeviceExtension
->AllocatedResources
== NULL
)
343 return STATUS_INSUFFICIENT_RESOURCES
;
346 RtlCopyMemory(DeviceExtension
->AllocatedResources
,
350 /* Get the interrupt level/vector - needed by HwFindAdapter sometimes */
351 for (FullList
= AllocatedResources
->List
;
352 FullList
< AllocatedResources
->List
+ AllocatedResources
->Count
;
355 /* FIXME: Is this ASSERT ok for resources from the PNP manager? */
356 ASSERT(FullList
->InterfaceType
== PCIBus
&&
357 FullList
->BusNumber
== DeviceExtension
->SystemIoBusNumber
&&
358 1 == FullList
->PartialResourceList
.Version
&&
359 1 == FullList
->PartialResourceList
.Revision
);
360 for (Descriptor
= FullList
->PartialResourceList
.PartialDescriptors
;
361 Descriptor
< FullList
->PartialResourceList
.PartialDescriptors
+ FullList
->PartialResourceList
.Count
;
364 if (Descriptor
->Type
== CmResourceTypeInterrupt
)
366 DeviceExtension
->InterruptLevel
= Descriptor
->u
.Interrupt
.Level
;
367 DeviceExtension
->InterruptVector
= Descriptor
->u
.Interrupt
.Vector
;
368 if (Descriptor
->ShareDisposition
== CmResourceShareShared
)
369 DeviceExtension
->InterruptShared
= TRUE
;
371 DeviceExtension
->InterruptShared
= FALSE
;
376 DPRINT("Interrupt level: 0x%x Interrupt Vector: 0x%x\n",
377 DeviceExtension
->InterruptLevel
,
378 DeviceExtension
->InterruptVector
);
381 * Create adapter device object.
384 return IntVideoPortFindAdapter(
393 IntVideoPortForwardIrpAndWaitCompletionRoutine(
398 PKEVENT Event
= Context
;
400 if (Irp
->PendingReturned
)
401 KeSetEvent(Event
, IO_NO_INCREMENT
, FALSE
);
403 return STATUS_MORE_PROCESSING_REQUIRED
;
409 IntVideoPortForwardIrpAndWait(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
413 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
=
414 (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
416 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
417 IoCopyCurrentIrpStackLocationToNext(Irp
);
418 IoSetCompletionRoutine(Irp
, IntVideoPortForwardIrpAndWaitCompletionRoutine
,
419 &Event
, TRUE
, TRUE
, TRUE
);
420 Status
= IoCallDriver(DeviceExtension
->NextDeviceObject
, Irp
);
421 if (Status
== STATUS_PENDING
)
423 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
424 Status
= Irp
->IoStatus
.Status
;
431 IntVideoPortDispatchPnp(
432 IN PDEVICE_OBJECT DeviceObject
,
435 PIO_STACK_LOCATION IrpSp
;
438 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
440 switch (IrpSp
->MinorFunction
)
442 case IRP_MN_START_DEVICE
:
443 Status
= IntVideoPortForwardIrpAndWait(DeviceObject
, Irp
);
444 if (NT_SUCCESS(Status
) && NT_SUCCESS(Irp
->IoStatus
.Status
))
445 Status
= IntVideoPortPnPStartDevice(DeviceObject
, Irp
);
446 Irp
->IoStatus
.Status
= Status
;
447 Irp
->IoStatus
.Information
= 0;
448 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
452 case IRP_MN_REMOVE_DEVICE
:
453 case IRP_MN_QUERY_REMOVE_DEVICE
:
454 case IRP_MN_CANCEL_REMOVE_DEVICE
:
455 case IRP_MN_SURPRISE_REMOVAL
:
457 case IRP_MN_STOP_DEVICE
:
458 Status
= IntVideoPortForwardIrpAndWait(DeviceObject
, Irp
);
459 if (NT_SUCCESS(Status
) && NT_SUCCESS(Irp
->IoStatus
.Status
))
460 Status
= STATUS_SUCCESS
;
461 Irp
->IoStatus
.Status
= Status
;
462 Irp
->IoStatus
.Information
= 0;
463 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
466 case IRP_MN_QUERY_STOP_DEVICE
:
467 case IRP_MN_CANCEL_STOP_DEVICE
:
468 Status
= STATUS_SUCCESS
;
469 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
470 Irp
->IoStatus
.Information
= 0;
471 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
475 return STATUS_NOT_IMPLEMENTED
;
483 IntVideoPortDispatchCleanup(
484 IN PDEVICE_OBJECT DeviceObject
,
487 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
489 DeviceExtension
= DeviceObject
->DeviceExtension
;
490 RtlFreeUnicodeString(&DeviceExtension
->RegistryPath
);
492 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
493 Irp
->IoStatus
.Information
= 0;
494 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
496 return STATUS_SUCCESS
;
500 IntVideoPortDispatchPower(
501 IN PDEVICE_OBJECT DeviceObject
,
504 return STATUS_NOT_IMPLEMENTED
;
508 IntVideoPortUnload(PDRIVER_OBJECT DriverObject
)