2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: GDI Driver Device Functions
5 * FILE: win32ss/gdi/eng/device.c
6 * PROGRAMER: Jason Filby
11 DBG_DEFAULT_CHANNEL(EngDev
)
13 PGRAPHICS_DEVICE gpPrimaryGraphicsDevice
;
14 PGRAPHICS_DEVICE gpVgaGraphicsDevice
;
16 static PGRAPHICS_DEVICE gpGraphicsDeviceFirst
= NULL
;
17 static PGRAPHICS_DEVICE gpGraphicsDeviceLast
= NULL
;
18 static HSEMAPHORE ghsemGraphicsDeviceList
;
19 static ULONG giDevNum
= 1;
26 ghsemGraphicsDeviceList
= EngCreateSemaphore();
27 if (!ghsemGraphicsDeviceList
)
28 return STATUS_INSUFFICIENT_RESOURCES
;
30 return STATUS_SUCCESS
;
34 EngpPopulateDeviceModeList(
35 _Inout_ PGRAPHICS_DEVICE pGraphicsDevice
,
36 _In_ PDEVMODEW pdmDefault
)
41 PDEVMODEW pdm
, pdmEnd
;
43 BOOLEAN bModeMatch
= FALSE
;
45 ASSERT(pGraphicsDevice
->pdevmodeInfo
== NULL
);
46 ASSERT(pGraphicsDevice
->pDevModeList
== NULL
);
48 pwsz
= pGraphicsDevice
->pDiplayDrivers
;
50 /* Loop through the driver names
51 * This is a REG_MULTI_SZ string */
52 for (; *pwsz
; pwsz
+= wcslen(pwsz
) + 1)
54 TRACE("trying driver: %ls\n", pwsz
);
55 /* Try to load the display driver */
56 pldev
= EngLoadImageEx(pwsz
, LDEV_DEVICE_DISPLAY
);
59 ERR("Could not load driver: '%ls'\n", pwsz
);
63 /* Get the mode list from the driver */
64 pdminfo
= LDEVOBJ_pdmiGetModes(pldev
, pGraphicsDevice
->DeviceObject
);
67 ERR("Could not get mode list for '%ls'\n", pwsz
);
71 /* Attach the mode info to the device */
72 pdminfo
->pdmiNext
= pGraphicsDevice
->pdevmodeInfo
;
73 pGraphicsDevice
->pdevmodeInfo
= pdminfo
;
75 /* Loop all DEVMODEs */
76 pdmEnd
= (DEVMODEW
*)((PCHAR
)pdminfo
->adevmode
+ pdminfo
->cbdevmode
);
77 for (pdm
= pdminfo
->adevmode
;
78 (pdm
+ 1 <= pdmEnd
) && (pdm
->dmSize
!= 0);
79 pdm
= (DEVMODEW
*)((PCHAR
)pdm
+ pdm
->dmSize
+ pdm
->dmDriverExtra
))
81 /* Count this DEVMODE */
84 /* Some drivers like the VBox driver don't fill the dmDeviceName
85 with the name of the display driver. So fix that here. */
86 wcsncpy(pdm
->dmDeviceName
, pwsz
, CCHDEVICENAME
);
87 pdm
->dmDeviceName
[CCHDEVICENAME
- 1] = 0;
90 // FIXME: release the driver again until it's used?
93 if (!pGraphicsDevice
->pdevmodeInfo
|| cModes
== 0)
99 /* Allocate an index buffer */
100 pGraphicsDevice
->cDevModes
= cModes
;
101 pGraphicsDevice
->pDevModeList
= ExAllocatePoolWithTag(PagedPool
,
102 cModes
* sizeof(DEVMODEENTRY
),
104 if (!pGraphicsDevice
->pDevModeList
)
106 ERR("No devmode list\n");
110 TRACE("Looking for mode %lux%lux%lu(%lu Hz)\n",
111 pdmDefault
->dmPelsWidth
,
112 pdmDefault
->dmPelsHeight
,
113 pdmDefault
->dmBitsPerPel
,
114 pdmDefault
->dmDisplayFrequency
);
116 /* Loop through all DEVMODEINFOs */
117 for (pdminfo
= pGraphicsDevice
->pdevmodeInfo
, i
= 0;
119 pdminfo
= pdminfo
->pdmiNext
)
121 /* Calculate End of the DEVMODEs */
122 pdmEnd
= (DEVMODEW
*)((PCHAR
)pdminfo
->adevmode
+ pdminfo
->cbdevmode
);
124 /* Loop through the DEVMODEs */
125 for (pdm
= pdminfo
->adevmode
;
126 (pdm
+ 1 <= pdmEnd
) && (pdm
->dmSize
!= 0);
127 pdm
= (PDEVMODEW
)((PCHAR
)pdm
+ pdm
->dmSize
+ pdm
->dmDriverExtra
))
129 TRACE(" %S has mode %lux%lux%lu(%lu Hz)\n",
134 pdm
->dmDisplayFrequency
);
135 /* Compare with the default entry */
137 pdm
->dmBitsPerPel
== pdmDefault
->dmBitsPerPel
&&
138 pdm
->dmPelsWidth
== pdmDefault
->dmPelsWidth
&&
139 pdm
->dmPelsHeight
== pdmDefault
->dmPelsHeight
)
141 pGraphicsDevice
->iDefaultMode
= i
;
142 pGraphicsDevice
->iCurrentMode
= i
;
143 TRACE("Found default entry: %lu '%ls'\n", i
, pdm
->dmDeviceName
);
144 if (pdm
->dmDisplayFrequency
== pdmDefault
->dmDisplayFrequency
)
146 /* Uh oh, even the display frequency matches. */
151 /* Initialize the entry */
152 pGraphicsDevice
->pDevModeList
[i
].dwFlags
= 0;
153 pGraphicsDevice
->pDevModeList
[i
].pdm
= pdm
;
162 EngpRegisterGraphicsDevice(
163 _In_ PUNICODE_STRING pustrDeviceName
,
164 _In_ PUNICODE_STRING pustrDiplayDrivers
,
165 _In_ PUNICODE_STRING pustrDescription
,
166 _In_ PDEVMODEW pdmDefault
)
168 PGRAPHICS_DEVICE pGraphicsDevice
;
169 PDEVICE_OBJECT pDeviceObject
;
170 PFILE_OBJECT pFileObject
;
177 TRACE("EngpRegisterGraphicsDevice(%wZ)\n", pustrDeviceName
);
179 /* Allocate a GRAPHICS_DEVICE structure */
180 pGraphicsDevice
= ExAllocatePoolWithTag(PagedPool
,
181 sizeof(GRAPHICS_DEVICE
),
183 if (!pGraphicsDevice
)
185 ERR("ExAllocatePoolWithTag failed\n");
189 /* Try to open the driver */
190 Status
= IoGetDeviceObjectPointer(pustrDeviceName
,
191 FILE_READ_DATA
| FILE_WRITE_DATA
,
194 if (!NT_SUCCESS(Status
))
196 ERR("Could not open driver %wZ, 0x%lx\n", pustrDeviceName
, Status
);
197 ExFreePoolWithTag(pGraphicsDevice
, GDITAG_GDEVICE
);
201 /* Enable the device */
202 EngFileWrite(pFileObject
, &bEnable
, sizeof(BOOL
), &cjWritten
);
204 /* Copy the device and file object pointers */
205 pGraphicsDevice
->DeviceObject
= pDeviceObject
;
206 pGraphicsDevice
->FileObject
= pFileObject
;
208 /* Copy device name */
209 RtlStringCbCopyNW(pGraphicsDevice
->szNtDeviceName
,
210 sizeof(pGraphicsDevice
->szNtDeviceName
),
211 pustrDeviceName
->Buffer
,
212 pustrDeviceName
->Length
);
214 /* Create a win device name (FIXME: virtual devices!) */
215 swprintf(pGraphicsDevice
->szWinDeviceName
, L
"\\\\.\\DISPLAY%d", (int)giDevNum
);
217 /* Allocate a buffer for the strings */
218 cj
= pustrDiplayDrivers
->Length
+ pustrDescription
->Length
+ sizeof(WCHAR
);
219 pwsz
= ExAllocatePoolWithTag(PagedPool
, cj
, GDITAG_DRVSUP
);
222 ERR("Could not allocate string buffer\n");
223 ASSERT(FALSE
); // FIXME
224 ExFreePoolWithTag(pGraphicsDevice
, GDITAG_GDEVICE
);
228 /* Copy display driver names */
229 pGraphicsDevice
->pDiplayDrivers
= pwsz
;
230 RtlCopyMemory(pGraphicsDevice
->pDiplayDrivers
,
231 pustrDiplayDrivers
->Buffer
,
232 pustrDiplayDrivers
->Length
);
234 /* Copy description */
235 pGraphicsDevice
->pwszDescription
= pwsz
+ pustrDiplayDrivers
->Length
/ sizeof(WCHAR
);
236 RtlCopyMemory(pGraphicsDevice
->pwszDescription
,
237 pustrDescription
->Buffer
,
238 pustrDescription
->Length
);
239 pGraphicsDevice
->pwszDescription
[pustrDescription
->Length
/sizeof(WCHAR
)] = 0;
241 /* Initialize the pdevmodeInfo list and default index */
242 pGraphicsDevice
->pdevmodeInfo
= NULL
;
243 pGraphicsDevice
->iDefaultMode
= 0;
244 pGraphicsDevice
->iCurrentMode
= 0;
246 // FIXME: initialize state flags
247 pGraphicsDevice
->StateFlags
= 0;
249 /* Create the mode list */
250 pGraphicsDevice
->pDevModeList
= NULL
;
251 if (!EngpPopulateDeviceModeList(pGraphicsDevice
, pdmDefault
))
253 ExFreePoolWithTag(pGraphicsDevice
, GDITAG_GDEVICE
);
258 EngAcquireSemaphore(ghsemGraphicsDeviceList
);
260 /* Insert the device into the global list */
261 pGraphicsDevice
->pNextGraphicsDevice
= NULL
;
262 if (gpGraphicsDeviceLast
)
263 gpGraphicsDeviceLast
->pNextGraphicsDevice
= pGraphicsDevice
;
264 gpGraphicsDeviceLast
= pGraphicsDevice
;
265 if (!gpGraphicsDeviceFirst
)
266 gpGraphicsDeviceFirst
= pGraphicsDevice
;
268 /* Increment device number */
272 EngReleaseSemaphore(ghsemGraphicsDeviceList
);
273 TRACE("Prepared %lu modes for %ls\n", pGraphicsDevice
->cDevModes
, pGraphicsDevice
->pwszDescription
);
275 return pGraphicsDevice
;
281 EngpFindGraphicsDevice(
282 _In_opt_ PUNICODE_STRING pustrDevice
,
286 UNICODE_STRING ustrCurrent
;
287 PGRAPHICS_DEVICE pGraphicsDevice
;
289 TRACE("EngpFindGraphicsDevice('%wZ', %lu, 0x%lx)\n",
290 pustrDevice
, iDevNum
, dwFlags
);
293 EngAcquireSemaphore(ghsemGraphicsDeviceList
);
295 if (pustrDevice
&& pustrDevice
->Buffer
)
297 /* Loop through the list of devices */
298 for (pGraphicsDevice
= gpGraphicsDeviceFirst
;
300 pGraphicsDevice
= pGraphicsDevice
->pNextGraphicsDevice
)
302 /* Compare the device name */
303 RtlInitUnicodeString(&ustrCurrent
, pGraphicsDevice
->szWinDeviceName
);
304 if (RtlEqualUnicodeString(&ustrCurrent
, pustrDevice
, FALSE
))
312 /* Loop through the list of devices */
313 for (pGraphicsDevice
= gpGraphicsDeviceFirst
, i
= 0;
314 pGraphicsDevice
&& i
< iDevNum
;
315 pGraphicsDevice
= pGraphicsDevice
->pNextGraphicsDevice
, i
++);
319 EngReleaseSemaphore(ghsemGraphicsDeviceList
);
321 return pGraphicsDevice
;
328 _In_ PFILE_OBJECT pFileObject
,
329 _In_ ULONG ulMajorFunction
,
330 _In_reads_(nBufferSize
) PVOID lpBuffer
,
331 _In_ SIZE_T nBufferSize
,
332 _In_ ULONGLONG ullStartOffset
,
333 _Out_ PULONG_PTR lpInformation
)
335 PDEVICE_OBJECT pDeviceObject
;
338 IO_STATUS_BLOCK Iosb
;
340 LARGE_INTEGER liStartOffset
;
342 /* Get corresponding device object */
343 pDeviceObject
= IoGetRelatedDeviceObject(pFileObject
);
346 return STATUS_INVALID_PARAMETER
;
349 /* Initialize an event */
350 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
353 liStartOffset
.QuadPart
= ullStartOffset
;
354 pIrp
= IoBuildSynchronousFsdRequest(ulMajorFunction
,
363 return STATUS_INSUFFICIENT_RESOURCES
;
366 /* Call the driver */
367 Status
= IoCallDriver(pDeviceObject
, pIrp
);
369 /* Wait if neccessary */
370 if (STATUS_PENDING
== Status
)
372 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, TRUE
, 0);
373 Status
= Iosb
.Status
;
376 /* Return information to the caller about the operation. */
377 *lpInformation
= Iosb
.Information
;
379 /* Return NTSTATUS */
386 _In_ PFILE_OBJECT pFileObject
,
387 _In_reads_(nLength
) PVOID lpBuffer
,
389 _Out_ PSIZE_T lpBytesWritten
)
393 status
= EngpFileIoRequest(pFileObject
,
399 if (!NT_SUCCESS(status
))
409 _In_ PFILE_OBJECT pFileObject
,
410 _In_ DWORD dwIoControlCode
,
411 _In_reads_(nInBufferSize
) PVOID lpInBuffer
,
412 _In_ SIZE_T nInBufferSize
,
413 _Out_writes_(nOutBufferSize
) PVOID lpOutBuffer
,
414 _In_ SIZE_T nOutBufferSize
,
415 _Out_ PULONG_PTR lpInformation
)
417 PDEVICE_OBJECT pDeviceObject
;
420 IO_STATUS_BLOCK Iosb
;
423 /* Get corresponding device object */
424 pDeviceObject
= IoGetRelatedDeviceObject(pFileObject
);
427 return STATUS_INVALID_PARAMETER
;
430 /* Initialize an event */
431 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
433 /* Build IO control IRP */
434 pIrp
= IoBuildDeviceIoControlRequest(dwIoControlCode
,
437 (ULONG
)nInBufferSize
,
439 (ULONG
)nOutBufferSize
,
445 return STATUS_INSUFFICIENT_RESOURCES
;
448 /* Call the driver */
449 Status
= IoCallDriver(pDeviceObject
, pIrp
);
451 /* Wait if neccessary */
452 if (Status
== STATUS_PENDING
)
454 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, TRUE
, 0);
455 Status
= Iosb
.Status
;
458 /* Return information to the caller about the operation. */
459 *lpInformation
= Iosb
.Information
;
461 /* This function returns NTSTATUS */
473 _In_ DWORD dwIoControlCode
,
474 _In_reads_bytes_opt_(cjInBufferSize
) LPVOID lpInBuffer
,
475 _In_ DWORD cjInBufferSize
,
476 _Out_writes_bytes_opt_(cjOutBufferSize
) LPVOID lpOutBuffer
,
477 _In_ DWORD cjOutBufferSize
,
478 _Out_ LPDWORD lpBytesReturned
)
483 IO_STATUS_BLOCK Iosb
;
484 PDEVICE_OBJECT DeviceObject
;
486 TRACE("EngDeviceIoControl() called\n");
488 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
490 DeviceObject
= (PDEVICE_OBJECT
) hDevice
;
492 Irp
= IoBuildDeviceIoControlRequest(dwIoControlCode
,
501 if (!Irp
) return ERROR_NOT_ENOUGH_MEMORY
;
503 Status
= IoCallDriver(DeviceObject
, Irp
);
505 if (Status
== STATUS_PENDING
)
507 (VOID
)KeWaitForSingleObject(&Event
, Executive
, KernelMode
, TRUE
, 0);
508 Status
= Iosb
.Status
;
511 TRACE("EngDeviceIoControl(): Returning %X/%X\n", Iosb
.Status
,
514 /* Return information to the caller about the operation. */
515 *lpBytesReturned
= (DWORD
)Iosb
.Information
;
517 /* Convert NT status values to win32 error codes. */
520 case STATUS_INSUFFICIENT_RESOURCES
:
521 return ERROR_NOT_ENOUGH_MEMORY
;
523 case STATUS_BUFFER_OVERFLOW
:
524 return ERROR_MORE_DATA
;
526 case STATUS_NOT_IMPLEMENTED
:
527 return ERROR_INVALID_FUNCTION
;
529 case STATUS_INVALID_PARAMETER
:
530 return ERROR_INVALID_PARAMETER
;
532 case STATUS_BUFFER_TOO_SMALL
:
533 return ERROR_INSUFFICIENT_BUFFER
;
535 case STATUS_DEVICE_DOES_NOT_EXIST
:
536 return ERROR_DEV_NOT_EXIST
;
539 return ERROR_IO_PENDING
;