2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: GDI Driver Device Functions
5 * FILE: subsys/win32k/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
;
36 EngpRegisterGraphicsDevice(
37 PUNICODE_STRING pustrDeviceName
,
38 PUNICODE_STRING pustrDiplayDrivers
,
39 PUNICODE_STRING pustrDescription
,
42 PGRAPHICS_DEVICE pGraphicsDevice
;
43 PDEVICE_OBJECT pDeviceObject
;
44 PFILE_OBJECT pFileObject
;
47 ULONG i
, cj
, cModes
= 0;
51 PDEVMODEW pdm
, pdmEnd
;
53 BOOLEAN bModeMatch
= FALSE
;
55 TRACE("EngpRegisterGraphicsDevice(%wZ)\n", pustrDeviceName
);
57 /* Allocate a GRAPHICS_DEVICE structure */
58 pGraphicsDevice
= ExAllocatePoolWithTag(PagedPool
,
59 sizeof(GRAPHICS_DEVICE
),
63 ERR("ExAllocatePoolWithTag failed\n");
67 /* Try to open the driver */
68 Status
= IoGetDeviceObjectPointer(pustrDeviceName
,
69 FILE_READ_DATA
| FILE_WRITE_DATA
,
72 if (!NT_SUCCESS(Status
))
74 ERR("Could not open driver %wZ, 0x%lx\n", pustrDeviceName
, Status
);
75 ExFreePoolWithTag(pGraphicsDevice
, GDITAG_GDEVICE
);
79 /* Enable the device */
80 EngFileWrite(pFileObject
, &bEnable
, sizeof(BOOL
), &cjWritten
);
82 /* Copy the device and file object pointers */
83 pGraphicsDevice
->DeviceObject
= pDeviceObject
;
84 pGraphicsDevice
->FileObject
= pFileObject
;
86 /* Copy device name */
87 RtlStringCbCopyNW(pGraphicsDevice
->szNtDeviceName
,
88 sizeof(pGraphicsDevice
->szNtDeviceName
),
89 pustrDeviceName
->Buffer
,
90 pustrDeviceName
->Length
);
92 /* Create a win device name (FIXME: virtual devices!) */
93 swprintf(pGraphicsDevice
->szWinDeviceName
, L
"\\\\.\\DISPLAY%d", (int)giDevNum
);
95 /* Allocate a buffer for the strings */
96 cj
= pustrDiplayDrivers
->Length
+ pustrDescription
->Length
+ sizeof(WCHAR
);
97 pwsz
= ExAllocatePoolWithTag(PagedPool
, cj
, GDITAG_DRVSUP
);
100 ERR("Could not allocate string buffer\n");
101 ASSERT(FALSE
); // FIXME
102 ExFreePoolWithTag(pGraphicsDevice
, GDITAG_GDEVICE
);
106 /* Copy display driver names */
107 pGraphicsDevice
->pDiplayDrivers
= pwsz
;
108 RtlCopyMemory(pGraphicsDevice
->pDiplayDrivers
,
109 pustrDiplayDrivers
->Buffer
,
110 pustrDiplayDrivers
->Length
);
112 /* Copy description */
113 pGraphicsDevice
->pwszDescription
= pwsz
+ pustrDiplayDrivers
->Length
/ sizeof(WCHAR
);
114 RtlCopyMemory(pGraphicsDevice
->pwszDescription
,
115 pustrDescription
->Buffer
,
116 pustrDescription
->Length
+ sizeof(WCHAR
));
118 /* Initialize the pdevmodeInfo list and default index */
119 pGraphicsDevice
->pdevmodeInfo
= NULL
;
120 pGraphicsDevice
->iDefaultMode
= 0;
121 pGraphicsDevice
->iCurrentMode
= 0;
123 // FIXME: initialize state flags
124 pGraphicsDevice
->StateFlags
= 0;
126 /* Loop through the driver names
127 * This is a REG_MULTI_SZ string */
128 for (; *pwsz
; pwsz
+= wcslen(pwsz
) + 1)
130 TRACE("trying driver: %ls\n", pwsz
);
131 /* Try to load the display driver */
132 pldev
= EngLoadImageEx(pwsz
, LDEV_DEVICE_DISPLAY
);
135 ERR("Could not load driver: '%ls'\n", pwsz
);
139 /* Get the mode list from the driver */
140 pdminfo
= LDEVOBJ_pdmiGetModes(pldev
, pDeviceObject
);
143 ERR("Could not get mode list for '%ls'\n", pwsz
);
147 /* Attach the mode info to the device */
148 pdminfo
->pdmiNext
= pGraphicsDevice
->pdevmodeInfo
;
149 pGraphicsDevice
->pdevmodeInfo
= pdminfo
;
151 /* Loop all DEVMODEs */
152 pdmEnd
= (DEVMODEW
*)((PCHAR
)pdminfo
->adevmode
+ pdminfo
->cbdevmode
);
153 for (pdm
= pdminfo
->adevmode
;
154 (pdm
+ 1 <= pdmEnd
) && (pdm
->dmSize
!= 0);
155 pdm
= (DEVMODEW
*)((PCHAR
)pdm
+ pdm
->dmSize
+ pdm
->dmDriverExtra
))
157 /* Count this DEVMODE */
160 /* Some drivers like the VBox driver don't fill the dmDeviceName
161 with the name of the display driver. So fix that here. */
162 wcsncpy(pdm
->dmDeviceName
, pwsz
, CCHDEVICENAME
);
163 pdm
->dmDeviceName
[CCHDEVICENAME
- 1] = 0;
166 // FIXME: release the driver again until it's used?
169 if (!pGraphicsDevice
->pdevmodeInfo
|| cModes
== 0)
171 ERR("No devmodes\n");
172 ExFreePoolWithTag(pGraphicsDevice
, GDITAG_GDEVICE
);
176 /* Allocate an index buffer */
177 pGraphicsDevice
->cDevModes
= cModes
;
178 pGraphicsDevice
->pDevModeList
= ExAllocatePoolWithTag(PagedPool
,
179 cModes
* sizeof(DEVMODEENTRY
),
181 if (!pGraphicsDevice
->pDevModeList
)
183 ERR("No devmode list\n");
184 ExFreePoolWithTag(pGraphicsDevice
, GDITAG_GDEVICE
);
188 TRACE("Looking for mode %lux%lux%lu(%lu Hz)\n",
189 pdmDefault
->dmPelsWidth
,
190 pdmDefault
->dmPelsHeight
,
191 pdmDefault
->dmBitsPerPel
,
192 pdmDefault
->dmDisplayFrequency
);
194 /* Loop through all DEVMODEINFOs */
195 for (pdminfo
= pGraphicsDevice
->pdevmodeInfo
, i
= 0;
197 pdminfo
= pdminfo
->pdmiNext
)
199 /* Calculate End of the DEVMODEs */
200 pdmEnd
= (DEVMODEW
*)((PCHAR
)pdminfo
->adevmode
+ pdminfo
->cbdevmode
);
202 /* Loop through the DEVMODEs */
203 for (pdm
= pdminfo
->adevmode
;
204 (pdm
+ 1 <= pdmEnd
) && (pdm
->dmSize
!= 0);
205 pdm
= (PDEVMODEW
)((PCHAR
)pdm
+ pdm
->dmSize
+ pdm
->dmDriverExtra
))
207 TRACE(" %S has mode %lux%lux%lu(%lu Hz)\n",
212 pdm
->dmDisplayFrequency
);
213 /* Compare with the default entry */
215 pdm
->dmBitsPerPel
== pdmDefault
->dmBitsPerPel
&&
216 pdm
->dmPelsWidth
== pdmDefault
->dmPelsWidth
&&
217 pdm
->dmPelsHeight
== pdmDefault
->dmPelsHeight
)
219 pGraphicsDevice
->iDefaultMode
= i
;
220 pGraphicsDevice
->iCurrentMode
= i
;
221 TRACE("Found default entry: %lu '%ls'\n", i
, pdm
->dmDeviceName
);
222 if (pdm
->dmDisplayFrequency
== pdmDefault
->dmDisplayFrequency
)
224 /* Uh oh, even the display frequency matches. */
229 /* Initialize the entry */
230 pGraphicsDevice
->pDevModeList
[i
].dwFlags
= 0;
231 pGraphicsDevice
->pDevModeList
[i
].pdm
= pdm
;
237 EngAcquireSemaphore(ghsemGraphicsDeviceList
);
239 /* Insert the device into the global list */
240 pGraphicsDevice
->pNextGraphicsDevice
= NULL
;
241 if (gpGraphicsDeviceLast
)
242 gpGraphicsDeviceLast
->pNextGraphicsDevice
= pGraphicsDevice
;
243 gpGraphicsDeviceLast
= pGraphicsDevice
;
244 if (!gpGraphicsDeviceFirst
)
245 gpGraphicsDeviceFirst
= pGraphicsDevice
;
247 /* Increment device number */
251 EngReleaseSemaphore(ghsemGraphicsDeviceList
);
252 TRACE("Prepared %lu modes for %ls\n", cModes
, pGraphicsDevice
->pwszDescription
);
254 return pGraphicsDevice
;
260 EngpFindGraphicsDevice(
261 PUNICODE_STRING pustrDevice
,
265 UNICODE_STRING ustrCurrent
;
266 PGRAPHICS_DEVICE pGraphicsDevice
;
268 TRACE("EngpFindGraphicsDevice('%wZ', %lu, 0x%lx)\n",
269 pustrDevice
, iDevNum
, dwFlags
);
272 EngAcquireSemaphore(ghsemGraphicsDeviceList
);
274 if (pustrDevice
&& pustrDevice
->Buffer
)
276 /* Loop through the list of devices */
277 for (pGraphicsDevice
= gpGraphicsDeviceFirst
;
279 pGraphicsDevice
= pGraphicsDevice
->pNextGraphicsDevice
)
281 /* Compare the device name */
282 RtlInitUnicodeString(&ustrCurrent
, pGraphicsDevice
->szWinDeviceName
);
283 if (RtlEqualUnicodeString(&ustrCurrent
, pustrDevice
, FALSE
))
291 /* Loop through the list of devices */
292 for (pGraphicsDevice
= gpGraphicsDeviceFirst
, i
= 0;
293 pGraphicsDevice
&& i
< iDevNum
;
294 pGraphicsDevice
= pGraphicsDevice
->pNextGraphicsDevice
, i
++);
298 EngReleaseSemaphore(ghsemGraphicsDeviceList
);
300 return pGraphicsDevice
;
307 PFILE_OBJECT pFileObject
,
308 ULONG ulMajorFunction
,
311 ULONGLONG ullStartOffset
,
312 OUT PULONG_PTR lpInformation
)
314 PDEVICE_OBJECT pDeviceObject
;
317 IO_STATUS_BLOCK Iosb
;
319 LARGE_INTEGER liStartOffset
;
321 /* Get corresponding device object */
322 pDeviceObject
= IoGetRelatedDeviceObject(pFileObject
);
325 return STATUS_INVALID_PARAMETER
;
328 /* Initialize an event */
329 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
332 liStartOffset
.QuadPart
= ullStartOffset
;
333 pIrp
= IoBuildSynchronousFsdRequest(ulMajorFunction
,
342 return STATUS_INSUFFICIENT_RESOURCES
;
345 /* Call the driver */
346 Status
= IoCallDriver(pDeviceObject
, pIrp
);
348 /* Wait if neccessary */
349 if (STATUS_PENDING
== Status
)
351 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, TRUE
, 0);
352 Status
= Iosb
.Status
;
355 /* Return information to the caller about the operation. */
356 *lpInformation
= Iosb
.Information
;
358 /* Return NTSTATUS */
365 IN PFILE_OBJECT pFileObject
,
368 IN PSIZE_T lpBytesWritten
)
370 EngpFileIoRequest(pFileObject
,
381 IN PFILE_OBJECT pFileObject
,
382 IN DWORD dwIoControlCode
,
384 IN SIZE_T nInBufferSize
,
385 OUT PVOID lpOutBuffer
,
386 IN SIZE_T nOutBufferSize
,
387 OUT PULONG_PTR lpInformation
)
389 PDEVICE_OBJECT pDeviceObject
;
392 IO_STATUS_BLOCK Iosb
;
395 /* Get corresponding device object */
396 pDeviceObject
= IoGetRelatedDeviceObject(pFileObject
);
399 return STATUS_INVALID_PARAMETER
;
402 /* Initialize an event */
403 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
405 /* Build IO control IRP */
406 pIrp
= IoBuildDeviceIoControlRequest(dwIoControlCode
,
409 (ULONG
)nInBufferSize
,
411 (ULONG
)nOutBufferSize
,
417 return STATUS_INSUFFICIENT_RESOURCES
;
420 /* Call the driver */
421 Status
= IoCallDriver(pDeviceObject
, pIrp
);
423 /* Wait if neccessary */
424 if (Status
== STATUS_PENDING
)
426 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, TRUE
, 0);
427 Status
= Iosb
.Status
;
430 /* Return information to the caller about the operation. */
431 *lpInformation
= Iosb
.Information
;
433 /* This function returns NTSTATUS */
443 _In_ DWORD dwIoControlCode
,
444 _In_opt_bytecount_(cjInBufferSize
) LPVOID lpInBuffer
,
445 _In_ DWORD cjInBufferSize
,
446 _Out_opt_bytecap_(cjOutBufferSize
) LPVOID lpOutBuffer
,
447 _In_ DWORD cjOutBufferSize
,
448 _Out_ LPDWORD lpBytesReturned
)
453 IO_STATUS_BLOCK Iosb
;
454 PDEVICE_OBJECT DeviceObject
;
456 TRACE("EngDeviceIoControl() called\n");
458 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
460 DeviceObject
= (PDEVICE_OBJECT
) hDevice
;
462 Irp
= IoBuildDeviceIoControlRequest(dwIoControlCode
,
471 if (!Irp
) return ERROR_NOT_ENOUGH_MEMORY
;
473 Status
= IoCallDriver(DeviceObject
, Irp
);
475 if (Status
== STATUS_PENDING
)
477 (VOID
)KeWaitForSingleObject(&Event
, Executive
, KernelMode
, TRUE
, 0);
478 Status
= Iosb
.Status
;
481 TRACE("EngDeviceIoControl(): Returning %X/%X\n", Iosb
.Status
,
484 /* Return information to the caller about the operation. */
485 *lpBytesReturned
= (DWORD
)Iosb
.Information
;
487 /* Convert NT status values to win32 error codes. */
490 case STATUS_INSUFFICIENT_RESOURCES
:
491 return ERROR_NOT_ENOUGH_MEMORY
;
493 case STATUS_BUFFER_OVERFLOW
:
494 return ERROR_MORE_DATA
;
496 case STATUS_NOT_IMPLEMENTED
:
497 return ERROR_INVALID_FUNCTION
;
499 case STATUS_INVALID_PARAMETER
:
500 return ERROR_INVALID_PARAMETER
;
502 case STATUS_BUFFER_TOO_SMALL
:
503 return ERROR_INSUFFICIENT_BUFFER
;
505 case STATUS_DEVICE_DOES_NOT_EXIST
:
506 return ERROR_DEV_NOT_EXIST
;
509 return ERROR_IO_PENDING
;