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.
26 /* EXTERNAL FUNCTIONS *********************************************************/
28 VOID NTAPI
HalAcquireDisplayOwnership(IN PHAL_RESET_DISPLAY_PARAMETERS ResetDisplayParameters
);
29 VOID NTAPI
HalReleaseDisplayOwnership();
31 /* GLOBAL VARIABLES ***********************************************************/
33 PVIDEO_PORT_DEVICE_EXTENSION ResetDisplayParametersDeviceExtension
= NULL
;
35 /* PRIVATE FUNCTIONS **********************************************************/
38 * Reset display to blue screen
42 IntVideoPortResetDisplayParameters(ULONG Columns
, ULONG Rows
)
44 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
46 if (ResetDisplayParametersDeviceExtension
== NULL
)
49 DriverExtension
= ResetDisplayParametersDeviceExtension
->DriverExtension
;
51 if (DriverExtension
->InitializationData
.HwResetHw
!= NULL
)
53 if (DriverExtension
->InitializationData
.HwResetHw(
54 &ResetDisplayParametersDeviceExtension
->MiniPortDeviceExtension
,
57 ResetDisplayParametersDeviceExtension
= NULL
;
62 ResetDisplayParametersDeviceExtension
= NULL
;
67 IntVideoPortAddDevice(
68 IN PDRIVER_OBJECT DriverObject
,
69 IN PDEVICE_OBJECT PhysicalDeviceObject
)
71 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
74 * Get the initialization data we saved in VideoPortInitialize.
77 DriverExtension
= IoGetDriverObjectExtension(DriverObject
, DriverObject
);
80 * Create adapter device object.
83 return IntVideoPortCreateAdapterDeviceObject(
91 * IntVideoPortDispatchOpen
93 * Answer requests for Open calls.
100 IntVideoPortDispatchOpen(
101 IN PDEVICE_OBJECT DeviceObject
,
104 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
105 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
107 DPRINT("IntVideoPortDispatchOpen\n");
109 if (CsrssInitialized
== FALSE
)
112 * We know the first open call will be from the CSRSS process
113 * to let us know its handle.
116 DPRINT("Referencing CSRSS\n");
117 Csrss
= (PKPROCESS
)PsGetCurrentProcess();
118 DPRINT("Csrss %p\n", Csrss
);
120 CsrssInitialized
= TRUE
;
122 Irp
->IoStatus
.Information
= FILE_OPENED
;
123 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
125 return STATUS_SUCCESS
;
128 DeviceExtension
= (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
129 DriverExtension
= DeviceExtension
->DriverExtension
;
131 if (DriverExtension
->InitializationData
.HwInitialize(&DeviceExtension
->MiniPortDeviceExtension
))
133 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
135 InterlockedIncrement((PLONG
)&DeviceExtension
->DeviceOpened
);
138 * Storing the device extension pointer in a static variable is an
139 * ugly hack. Unfortunately, we need it in VideoPortResetDisplayParameters
140 * and HalAcquireDisplayOwnership doesn't allow us to pass a userdata
141 * parameter. On the bright side, the DISPLAY device is opened
142 * exclusively, so there can be only one device extension active at
146 ResetDisplayParametersDeviceExtension
= DeviceExtension
;
147 HalAcquireDisplayOwnership(IntVideoPortResetDisplayParameters
);
151 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
154 Irp
->IoStatus
.Information
= FILE_OPENED
;
155 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
157 return STATUS_SUCCESS
;
161 * IntVideoPortDispatchClose
163 * Answer requests for Close calls.
170 IntVideoPortDispatchClose(
171 IN PDEVICE_OBJECT DeviceObject
,
174 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
176 DPRINT("IntVideoPortDispatchClose\n");
178 DeviceExtension
= (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
179 if (DeviceExtension
->DeviceOpened
>= 1 &&
180 InterlockedDecrement((PLONG
)&DeviceExtension
->DeviceOpened
) == 0)
182 ResetDisplayParametersDeviceExtension
= DeviceExtension
;
183 HalReleaseDisplayOwnership();
186 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
187 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
189 return STATUS_SUCCESS
;
193 * IntVideoPortDispatchDeviceControl
195 * Answer requests for device control calls.
202 IntVideoPortDispatchDeviceControl(
203 IN PDEVICE_OBJECT DeviceObject
,
206 PIO_STACK_LOCATION IrpStack
;
207 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
208 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
209 PVIDEO_REQUEST_PACKET vrp
;
212 DPRINT("IntVideoPortDispatchDeviceControl\n");
214 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
215 DeviceExtension
= DeviceObject
->DeviceExtension
;
216 DriverExtension
= DeviceExtension
->DriverExtension
;
218 /* Translate the IRP to a VRP */
219 vrp
= ExAllocatePool(NonPagedPool
, sizeof(VIDEO_REQUEST_PACKET
));
222 return STATUS_NO_MEMORY
;
225 vrp
->StatusBlock
= (PSTATUS_BLOCK
)&(Irp
->IoStatus
);
226 vrp
->IoControlCode
= IrpStack
->Parameters
.DeviceIoControl
.IoControlCode
;
228 DPRINT("- IoControlCode: %x\n", vrp
->IoControlCode
);
230 /* We're assuming METHOD_BUFFERED */
231 vrp
->InputBuffer
= Irp
->AssociatedIrp
.SystemBuffer
;
232 vrp
->InputBufferLength
= IrpStack
->Parameters
.DeviceIoControl
.InputBufferLength
;
233 vrp
->OutputBuffer
= Irp
->AssociatedIrp
.SystemBuffer
;
234 vrp
->OutputBufferLength
= IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
;
236 /* Call the Miniport Driver with the VRP */
237 DriverExtension
->InitializationData
.HwStartIO(
238 &DeviceExtension
->MiniPortDeviceExtension
,
244 DPRINT("- Returned status: %x\n", Irp
->IoStatus
.Status
);
246 if (Irp
->IoStatus
.Status
!= STATUS_SUCCESS
)
248 /* Map from win32 error codes to NT status values. */
249 switch (Irp
->IoStatus
.Status
)
251 case ERROR_NOT_ENOUGH_MEMORY
: Irp
->IoStatus
.Status
= STATUS_INSUFFICIENT_RESOURCES
; break;
252 case ERROR_MORE_DATA
: Irp
->IoStatus
.Status
= STATUS_BUFFER_OVERFLOW
; break;
253 case ERROR_INVALID_FUNCTION
: Irp
->IoStatus
.Status
= STATUS_NOT_IMPLEMENTED
; break;
254 case ERROR_INVALID_PARAMETER
: Irp
->IoStatus
.Status
= STATUS_INVALID_PARAMETER
; break;
255 case ERROR_INSUFFICIENT_BUFFER
: Irp
->IoStatus
.Status
= STATUS_BUFFER_TOO_SMALL
; break;
256 case ERROR_DEV_NOT_EXIST
: Irp
->IoStatus
.Status
= STATUS_DEVICE_DOES_NOT_EXIST
; break;
257 case ERROR_IO_PENDING
: Irp
->IoStatus
.Status
= STATUS_PENDING
; break;
261 Status
= Irp
->IoStatus
.Status
;
262 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
268 IntVideoPortPnPStartDevice(
269 IN PDEVICE_OBJECT DeviceObject
,
272 PIO_STACK_LOCATION Stack
= IoGetCurrentIrpStackLocation(Irp
);
273 PDRIVER_OBJECT DriverObject
;
274 PVIDEO_PORT_DRIVER_EXTENSION DriverExtension
;
275 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
276 PCM_RESOURCE_LIST AllocatedResources
;
279 * Get the initialization data we saved in VideoPortInitialize.
282 DriverObject
= DeviceObject
->DriverObject
;
283 DriverExtension
= IoGetDriverObjectExtension(DriverObject
, DriverObject
);
284 DeviceExtension
= (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
287 * Store some resources in the DeviceExtension.
290 AllocatedResources
= Stack
->Parameters
.StartDevice
.AllocatedResources
;
291 if (AllocatedResources
!= NULL
)
293 CM_FULL_RESOURCE_DESCRIPTOR
*FullList
;
294 CM_PARTIAL_RESOURCE_DESCRIPTOR
*Descriptor
;
296 ULONG ResourceListSize
;
298 /* Save the resource list */
299 ResourceCount
= AllocatedResources
->List
[0].PartialResourceList
.Count
;
301 FIELD_OFFSET(CM_RESOURCE_LIST
, List
[0].PartialResourceList
.
302 PartialDescriptors
[ResourceCount
]);
303 DeviceExtension
->AllocatedResources
= ExAllocatePool(PagedPool
, ResourceListSize
);
304 if (DeviceExtension
->AllocatedResources
== NULL
)
306 return STATUS_INSUFFICIENT_RESOURCES
;
309 RtlCopyMemory(DeviceExtension
->AllocatedResources
,
313 /* Get the interrupt level/vector - needed by HwFindAdapter sometimes */
314 for (FullList
= AllocatedResources
->List
;
315 FullList
< AllocatedResources
->List
+ AllocatedResources
->Count
;
318 /* FIXME: Is this ASSERT ok for resources from the PNP manager? */
319 ASSERT(FullList
->InterfaceType
== PCIBus
&&
320 FullList
->BusNumber
== DeviceExtension
->SystemIoBusNumber
&&
321 1 == FullList
->PartialResourceList
.Version
&&
322 1 == FullList
->PartialResourceList
.Revision
);
323 for (Descriptor
= FullList
->PartialResourceList
.PartialDescriptors
;
324 Descriptor
< FullList
->PartialResourceList
.PartialDescriptors
+ FullList
->PartialResourceList
.Count
;
327 if (Descriptor
->Type
== CmResourceTypeInterrupt
)
329 DeviceExtension
->InterruptLevel
= Descriptor
->u
.Interrupt
.Level
;
330 DeviceExtension
->InterruptVector
= Descriptor
->u
.Interrupt
.Vector
;
331 if (Descriptor
->ShareDisposition
== CmResourceShareShared
)
332 DeviceExtension
->InterruptShared
= TRUE
;
334 DeviceExtension
->InterruptShared
= FALSE
;
339 DPRINT("Interrupt level: 0x%x Interrupt Vector: 0x%x\n",
340 DeviceExtension
->InterruptLevel
,
341 DeviceExtension
->InterruptVector
);
344 * Create adapter device object.
347 return IntVideoPortFindAdapter(
356 IntVideoPortForwardIrpAndWaitCompletionRoutine(
361 PKEVENT Event
= Context
;
363 if (Irp
->PendingReturned
)
364 KeSetEvent(Event
, IO_NO_INCREMENT
, FALSE
);
366 return STATUS_MORE_PROCESSING_REQUIRED
;
372 IntVideoPortForwardIrpAndWait(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
376 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
=
377 (PVIDEO_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
379 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
380 IoCopyCurrentIrpStackLocationToNext(Irp
);
381 IoSetCompletionRoutine(Irp
, IntVideoPortForwardIrpAndWaitCompletionRoutine
,
382 &Event
, TRUE
, TRUE
, TRUE
);
383 Status
= IoCallDriver(DeviceExtension
->NextDeviceObject
, Irp
);
384 if (Status
== STATUS_PENDING
)
386 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
387 Status
= Irp
->IoStatus
.Status
;
394 IntVideoPortDispatchPnp(
395 IN PDEVICE_OBJECT DeviceObject
,
398 PIO_STACK_LOCATION IrpSp
;
401 IrpSp
= IoGetCurrentIrpStackLocation(Irp
);
403 switch (IrpSp
->MinorFunction
)
405 case IRP_MN_START_DEVICE
:
406 Status
= IntVideoPortForwardIrpAndWait(DeviceObject
, Irp
);
407 if (NT_SUCCESS(Status
) && NT_SUCCESS(Irp
->IoStatus
.Status
))
408 Status
= IntVideoPortPnPStartDevice(DeviceObject
, Irp
);
409 Irp
->IoStatus
.Status
= Status
;
410 Irp
->IoStatus
.Information
= 0;
411 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
415 case IRP_MN_REMOVE_DEVICE
:
416 case IRP_MN_QUERY_REMOVE_DEVICE
:
417 case IRP_MN_CANCEL_REMOVE_DEVICE
:
418 case IRP_MN_SURPRISE_REMOVAL
:
420 case IRP_MN_STOP_DEVICE
:
421 Status
= IntVideoPortForwardIrpAndWait(DeviceObject
, Irp
);
422 if (NT_SUCCESS(Status
) && NT_SUCCESS(Irp
->IoStatus
.Status
))
423 Status
= STATUS_SUCCESS
;
424 Irp
->IoStatus
.Status
= Status
;
425 Irp
->IoStatus
.Information
= 0;
426 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
429 case IRP_MN_QUERY_STOP_DEVICE
:
430 case IRP_MN_CANCEL_STOP_DEVICE
:
431 Status
= STATUS_SUCCESS
;
432 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
433 Irp
->IoStatus
.Information
= 0;
434 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
438 return STATUS_NOT_IMPLEMENTED
;
446 IntVideoPortDispatchCleanup(
447 IN PDEVICE_OBJECT DeviceObject
,
450 PVIDEO_PORT_DEVICE_EXTENSION DeviceExtension
;
452 DeviceExtension
= DeviceObject
->DeviceExtension
;
453 RtlFreeUnicodeString(&DeviceExtension
->RegistryPath
);
455 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
456 Irp
->IoStatus
.Information
= 0;
457 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
459 return STATUS_SUCCESS
;
463 IntVideoPortDispatchPower(
464 IN PDEVICE_OBJECT DeviceObject
,
467 return STATUS_NOT_IMPLEMENTED
;
471 IntVideoPortUnload(PDRIVER_OBJECT DriverObject
)