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
15 PGRAPHICS_DEVICE gpPrimaryGraphicsDevice
;
16 PGRAPHICS_DEVICE gpVgaGraphicsDevice
;
18 static PGRAPHICS_DEVICE gpGraphicsDeviceFirst
= NULL
;
19 static PGRAPHICS_DEVICE gpGraphicsDeviceLast
= NULL
;
20 static HSEMAPHORE ghsemGraphicsDeviceList
;
21 static ULONG giDevNum
= 1;
28 ghsemGraphicsDeviceList
= EngCreateSemaphore();
29 if (!ghsemGraphicsDeviceList
)
30 return STATUS_INSUFFICIENT_RESOURCES
;
32 return STATUS_SUCCESS
;
38 EngpRegisterGraphicsDevice(
39 PUNICODE_STRING pustrDeviceName
,
40 PUNICODE_STRING pustrDiplayDrivers
,
41 PUNICODE_STRING pustrDescription
,
44 PGRAPHICS_DEVICE pGraphicsDevice
;
45 PDEVICE_OBJECT pDeviceObject
;
46 PFILE_OBJECT pFileObject
;
49 ULONG i
, cj
, cModes
= 0;
52 PDEVMODEW pdm
, pdmEnd
;
55 DPRINT("EngpRegisterGraphicsDevice(%wZ)\n", pustrDeviceName
);
57 /* Allocate a GRAPHICS_DEVICE structure */
58 pGraphicsDevice
= ExAllocatePoolWithTag(PagedPool
,
59 sizeof(GRAPHICS_DEVICE
),
63 DPRINT1("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 DPRINT1("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
), &cj
);
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 DPRINT1("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 DPRINT("trying driver: %ls\n", pwsz
);
131 /* Try to load the display driver */
132 pldev
= EngLoadImageEx(pwsz
, LDEV_DEVICE_DISPLAY
);
135 DPRINT1("Could not load driver: '%ls'\n", pwsz
);
139 /* Get the mode list from the driver */
140 pdminfo
= LDEVOBJ_pdmiGetModes(pldev
, pDeviceObject
);
143 DPRINT1("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
;
152 pdmEnd
= (DEVMODEW
*)((PCHAR
)pdminfo
->adevmode
+ pdminfo
->cbdevmode
);
153 for (pdm
= pdminfo
->adevmode
;
155 pdm
= (DEVMODEW
*)((PCHAR
)pdm
+ pdm
->dmSize
+ pdm
->dmDriverExtra
))
160 // FIXME: release the driver again until it's used?
163 if (!pGraphicsDevice
->pdevmodeInfo
|| cModes
== 0)
165 DPRINT1("No devmodes\n");
166 ExFreePoolWithTag(pGraphicsDevice
, GDITAG_GDEVICE
);
170 /* Allocate an index buffer */
171 pGraphicsDevice
->cDevModes
= cModes
;
172 pGraphicsDevice
->pDevModeList
= ExAllocatePoolWithTag(PagedPool
,
173 cModes
* sizeof(DEVMODEENTRY
),
175 if (!pGraphicsDevice
->pDevModeList
)
177 DPRINT1("No devmode list\n");
178 ExFreePoolWithTag(pGraphicsDevice
, GDITAG_GDEVICE
);
182 /* Loop through all DEVMODEINFOs */
183 for (pdminfo
= pGraphicsDevice
->pdevmodeInfo
, i
= 0;
185 pdminfo
= pdminfo
->pdmiNext
)
187 /* Calculate End of the DEVMODEs */
188 pdmEnd
= (DEVMODEW
*)((PCHAR
)pdminfo
->adevmode
+ pdminfo
->cbdevmode
);
190 /* Loop through the DEVMODEs */
191 for (pdm
= pdminfo
->adevmode
;
193 pdm
= (PDEVMODEW
)((PCHAR
)pdm
+ pdm
->dmSize
+ pdm
->dmDriverExtra
))
195 /* Compare with the default entry */
196 if (pdm
->dmBitsPerPel
== pdmDefault
->dmBitsPerPel
&&
197 pdm
->dmPelsWidth
== pdmDefault
->dmPelsWidth
&&
198 pdm
->dmPelsHeight
== pdmDefault
->dmPelsHeight
&&
199 pdm
->dmDisplayFrequency
== pdmDefault
->dmDisplayFrequency
)
201 pGraphicsDevice
->iDefaultMode
= i
;
202 pGraphicsDevice
->iCurrentMode
= i
;
203 DPRINT("Found default entry: %ld '%ls'\n", i
, pdm
->dmDeviceName
);
206 /* Initialize the entry */
207 pGraphicsDevice
->pDevModeList
[i
].dwFlags
= 0;
208 pGraphicsDevice
->pDevModeList
[i
].pdm
= pdm
;
214 EngAcquireSemaphore(ghsemGraphicsDeviceList
);
216 /* Insert the device into the global list */
217 pGraphicsDevice
->pNextGraphicsDevice
= gpGraphicsDeviceLast
;
218 gpGraphicsDeviceLast
= pGraphicsDevice
;
219 if (!gpGraphicsDeviceFirst
)
220 gpGraphicsDeviceFirst
= pGraphicsDevice
;
222 /* Increment device number */
226 EngReleaseSemaphore(ghsemGraphicsDeviceList
);
227 DPRINT("Prepared %ld modes for %ls\n", cModes
, pGraphicsDevice
->pwszDescription
);
229 return pGraphicsDevice
;
235 EngpFindGraphicsDevice(
236 PUNICODE_STRING pustrDevice
,
240 UNICODE_STRING ustrCurrent
;
241 PGRAPHICS_DEVICE pGraphicsDevice
;
245 EngAcquireSemaphore(ghsemGraphicsDeviceList
);
249 /* Loop through the list of devices */
250 for (pGraphicsDevice
= gpGraphicsDeviceFirst
;
252 pGraphicsDevice
= pGraphicsDevice
->pNextGraphicsDevice
)
254 /* Compare the device name */
255 RtlInitUnicodeString(&ustrCurrent
, pGraphicsDevice
->szWinDeviceName
);
256 if (RtlEqualUnicodeString(&ustrCurrent
, pustrDevice
, FALSE
))
264 /* Loop through the list of devices */
265 for (pGraphicsDevice
= gpGraphicsDeviceFirst
, i
= 0;
266 pGraphicsDevice
&& i
< iDevNum
;
267 pGraphicsDevice
= pGraphicsDevice
->pNextGraphicsDevice
, i
++);
271 EngReleaseSemaphore(ghsemGraphicsDeviceList
);
273 return pGraphicsDevice
;
280 PFILE_OBJECT pFileObject
,
281 ULONG ulMajorFunction
,
284 ULONGLONG ullStartOffset
,
285 OUT LPDWORD lpInformation
)
287 PDEVICE_OBJECT pDeviceObject
;
290 IO_STATUS_BLOCK Iosb
;
292 LARGE_INTEGER liStartOffset
;
294 /* Get corresponding device object */
295 pDeviceObject
= IoGetRelatedDeviceObject(pFileObject
);
298 return STATUS_INVALID_PARAMETER
;
301 /* Initialize an event */
302 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
305 liStartOffset
.QuadPart
= ullStartOffset
;
306 pIrp
= IoBuildSynchronousFsdRequest(ulMajorFunction
,
315 return STATUS_INSUFFICIENT_RESOURCES
;
318 /* Call the driver */
319 Status
= IoCallDriver(pDeviceObject
, pIrp
);
321 /* Wait if neccessary */
322 if (STATUS_PENDING
== Status
)
324 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, TRUE
, 0);
325 Status
= Iosb
.Status
;
328 /* Return information to the caller about the operation. */
329 *lpInformation
= Iosb
.Information
;
331 /* Return NTSTATUS */
338 IN PFILE_OBJECT pFileObject
,
341 IN PSIZE_T lpBytesWritten
)
343 EngpFileIoRequest(pFileObject
,
354 IN PFILE_OBJECT pFileObject
,
355 IN DWORD dwIoControlCode
,
357 IN SIZE_T nInBufferSize
,
358 OUT PVOID lpOutBuffer
,
359 IN SIZE_T nOutBufferSize
,
360 OUT LPDWORD lpInformation
)
362 PDEVICE_OBJECT pDeviceObject
;
365 IO_STATUS_BLOCK Iosb
;
368 /* Get corresponding device object */
369 pDeviceObject
= IoGetRelatedDeviceObject(pFileObject
);
372 return STATUS_INVALID_PARAMETER
;
375 /* Initialize an event */
376 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
378 /* Build IO control IRP */
379 pIrp
= IoBuildDeviceIoControlRequest(dwIoControlCode
,
390 return STATUS_INSUFFICIENT_RESOURCES
;
393 /* Call the driver */
394 Status
= IoCallDriver(pDeviceObject
, pIrp
);
396 /* Wait if neccessary */
397 if (Status
== STATUS_PENDING
)
399 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, TRUE
, 0);
400 Status
= Iosb
.Status
;
403 /* Return information to the caller about the operation. */
404 *lpInformation
= Iosb
.Information
;
406 /* This function returns NTSTATUS */
416 DWORD dwIoControlCode
,
420 DWORD nOutBufferSize
,
421 DWORD
*lpBytesReturned
)
426 IO_STATUS_BLOCK Iosb
;
427 PDEVICE_OBJECT DeviceObject
;
429 DPRINT("EngDeviceIoControl() called\n");
431 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
433 DeviceObject
= (PDEVICE_OBJECT
) hDevice
;
435 Irp
= IoBuildDeviceIoControlRequest(dwIoControlCode
,
440 nOutBufferSize
, FALSE
, &Event
, &Iosb
);
441 if (!Irp
) return ERROR_NOT_ENOUGH_MEMORY
;
443 Status
= IoCallDriver(DeviceObject
, Irp
);
445 if (Status
== STATUS_PENDING
)
447 (VOID
)KeWaitForSingleObject(&Event
, Executive
, KernelMode
, TRUE
, 0);
448 Status
= Iosb
.Status
;
451 DPRINT("EngDeviceIoControl(): Returning %X/%X\n", Iosb
.Status
,
454 /* Return information to the caller about the operation. */
455 *lpBytesReturned
= Iosb
.Information
;
457 /* Convert NT status values to win32 error codes. */
460 case STATUS_INSUFFICIENT_RESOURCES
:
461 return ERROR_NOT_ENOUGH_MEMORY
;
463 case STATUS_BUFFER_OVERFLOW
:
464 return ERROR_MORE_DATA
;
466 case STATUS_NOT_IMPLEMENTED
:
467 return ERROR_INVALID_FUNCTION
;
469 case STATUS_INVALID_PARAMETER
:
470 return ERROR_INVALID_PARAMETER
;
472 case STATUS_BUFFER_TOO_SMALL
:
473 return ERROR_INSUFFICIENT_BUFFER
;
475 case STATUS_DEVICE_DOES_NOT_EXIST
:
476 return ERROR_DEV_NOT_EXIST
;
479 return ERROR_IO_PENDING
;