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 static PGRAPHICS_DEVICE gpGraphicsDeviceFirst
= NULL
;
16 static PGRAPHICS_DEVICE gpGraphicsDeviceLast
= NULL
;
17 static HSEMAPHORE ghsemGraphicsDeviceList
;
18 static ULONG giDevNum
= 1;
24 ghsemGraphicsDeviceList
= EngCreateSemaphore();
25 if (!ghsemGraphicsDeviceList
)
34 EngpRegisterGraphicsDevice(
35 PUNICODE_STRING pustrDeviceName
,
36 PUNICODE_STRING pustrDiplayDrivers
,
37 PUNICODE_STRING pustrDescription
,
40 PGRAPHICS_DEVICE pGraphicsDevice
;
41 PDEVICE_OBJECT pDeviceObject
;
42 PFILE_OBJECT pFileObject
;
45 ULONG i
, cj
, cModes
= 0;
48 PDEVMODEW pdm
, pdmEnd
;
51 DPRINT1("EngpRegisterGraphicsDevice(%S)\n", pustrDeviceName
->Buffer
);
53 /* Allocate a GRAPHICS_DEVICE structure */
54 pGraphicsDevice
= ExAllocatePoolWithTag(PagedPool
,
55 sizeof(GRAPHICS_DEVICE
),
59 DPRINT1("ExAllocatePoolWithTag failed\n");
63 /* Try to open the driver */
64 Status
= IoGetDeviceObjectPointer(pustrDeviceName
,
65 FILE_READ_DATA
| FILE_WRITE_DATA
,
68 if (!NT_SUCCESS(Status
))
70 DPRINT1("Could not open driver, 0x%lx\n", Status
);
71 ExFreePoolWithTag(pGraphicsDevice
, GDITAG_GDEVICE
);
75 /* Enable the device */
76 EngFileWrite(pFileObject
, &bEnable
, sizeof(BOOL
), &cj
);
78 /* Copy the device and file object pointers */
79 pGraphicsDevice
->DeviceObject
= pDeviceObject
;
80 pGraphicsDevice
->FileObject
= pFileObject
;
82 /* Copy device name */
83 wcsncpy(pGraphicsDevice
->szNtDeviceName
,
84 pustrDeviceName
->Buffer
,
85 sizeof(pGraphicsDevice
->szNtDeviceName
) / sizeof(WCHAR
));
87 /* Create a win device name (FIXME: virtual devices!) */
88 swprintf(pGraphicsDevice
->szWinDeviceName
, L
"\\\\.\\VIDEO%d", (CHAR
)giDevNum
);
90 /* Allocate a buffer for the strings */
91 cj
= pustrDiplayDrivers
->Length
+ pustrDescription
->Length
+ sizeof(WCHAR
);
92 pwsz
= ExAllocatePoolWithTag(PagedPool
, cj
, GDITAG_DRVSUP
);
95 DPRINT1("Could not allocate string buffer\n");
96 ASSERT(FALSE
); // FIXME
99 /* Copy display driver names */
100 pGraphicsDevice
->pDiplayDrivers
= pwsz
;
101 RtlCopyMemory(pGraphicsDevice
->pDiplayDrivers
,
102 pustrDiplayDrivers
->Buffer
,
103 pustrDiplayDrivers
->Length
);
105 /* Copy description */
106 pGraphicsDevice
->pwszDescription
= pwsz
+ pustrDiplayDrivers
->Length
/ sizeof(WCHAR
);
107 RtlCopyMemory(pGraphicsDevice
->pwszDescription
,
108 pustrDescription
->Buffer
,
109 pustrDescription
->Length
+ sizeof(WCHAR
));
111 /* Initialize the pdevmodeInfo list and default index */
112 pGraphicsDevice
->pdevmodeInfo
= NULL
;
113 pGraphicsDevice
->iDefaultMode
= 0;
114 pGraphicsDevice
->iCurrentMode
= 0;
116 // FIXME: initialize state flags
117 pGraphicsDevice
->StateFlags
= 0;
119 /* Loop through the driver names */
120 for (; *pwsz
; pwsz
+= wcslen(pwsz
) + 1)
122 DPRINT1("trying driver: %ls\n", pwsz
);
123 /* Try to load the display driver */
124 pldev
= EngLoadDriver(pwsz
, LDEV_DEVICE_DISPLAY
);
127 DPRINT1("Could not load driver: '%ls'\n", pwsz
);
131 /* Get the mode list from the driver */
132 pdminfo
= LDEVOBJ_pdmiGetModes(pldev
, pDeviceObject
);
135 DPRINT1("Could not get mode list for '%ls'\n", pwsz
);
139 /* Attach the mode info to the device */
140 pdminfo
->pdmiNext
= pGraphicsDevice
->pdevmodeInfo
;
141 pGraphicsDevice
->pdevmodeInfo
= pdminfo
;
144 pdmEnd
= (DEVMODEW
*)((PCHAR
)pdminfo
->adevmode
+ pdminfo
->cbdevmode
);
145 for (pdm
= pdminfo
->adevmode
;
147 pdm
= (DEVMODEW
*)((PCHAR
)pdm
+ pdm
->dmSize
+ pdm
->dmDriverExtra
))
152 // FIXME: release the driver again until it's used?
155 if (!pGraphicsDevice
->pdevmodeInfo
|| cModes
== 0)
157 DPRINT1("No devmodes\n");
158 ExFreePool(pGraphicsDevice
);
162 /* Allocate an index buffer */
163 pGraphicsDevice
->cDevModes
= cModes
;
164 pGraphicsDevice
->pDevModeList
= ExAllocatePoolWithTag(PagedPool
,
165 cModes
* sizeof(DEVMODEENTRY
),
167 if (!pGraphicsDevice
->pDevModeList
)
169 DPRINT1("No devmode list\n");
170 ExFreePool(pGraphicsDevice
);
174 /* Loop through all DEVMODEINFOs */
175 for (pdminfo
= pGraphicsDevice
->pdevmodeInfo
, i
= 0;
177 pdminfo
= pdminfo
->pdmiNext
)
179 /* Calculate End of the DEVMODEs */
180 pdmEnd
= (DEVMODEW
*)((PCHAR
)pdminfo
->adevmode
+ pdminfo
->cbdevmode
);
182 /* Loop through the DEVMODEs */
183 for (pdm
= pdminfo
->adevmode
;
185 pdm
= (PDEVMODEW
)((PCHAR
)pdm
+ pdm
->dmSize
+ pdm
->dmDriverExtra
))
187 /* Compare with the default entry */
188 if (pdm
->dmBitsPerPel
== pdmDefault
->dmBitsPerPel
&&
189 pdm
->dmPelsWidth
== pdmDefault
->dmPelsWidth
&&
190 pdm
->dmPelsHeight
== pdmDefault
->dmPelsHeight
&&
191 pdm
->dmDisplayFrequency
== pdmDefault
->dmDisplayFrequency
)
193 pGraphicsDevice
->iDefaultMode
= i
;
194 pGraphicsDevice
->iCurrentMode
= i
;
195 DPRINT1("Found default entry: %ld '%ls'\n", i
, pdm
->dmDeviceName
);
198 /* Initialize the entry */
199 pGraphicsDevice
->pDevModeList
[i
].dwFlags
= 0;
200 pGraphicsDevice
->pDevModeList
[i
].pdm
= pdm
;
206 EngAcquireSemaphore(ghsemGraphicsDeviceList
);
208 /* Insert the device into the global list */
209 pGraphicsDevice
->pNextGraphicsDevice
= gpGraphicsDeviceLast
;
210 gpGraphicsDeviceLast
= pGraphicsDevice
;
211 if (!gpGraphicsDeviceFirst
)
212 gpGraphicsDeviceFirst
= pGraphicsDevice
;
214 /* Increment device number */
218 EngReleaseSemaphore(ghsemGraphicsDeviceList
);
219 DPRINT1("Prepared %ld modes for %ls\n", cModes
, pGraphicsDevice
->pwszDescription
);
221 return pGraphicsDevice
;
227 EngpFindGraphicsDevice(
228 PUNICODE_STRING pustrDevice
,
232 UNICODE_STRING ustrCurrent
;
233 PGRAPHICS_DEVICE pGraphicsDevice
;
237 EngAcquireSemaphore(ghsemGraphicsDeviceList
);
241 /* Loop through the list of devices */
242 for (pGraphicsDevice
= gpGraphicsDeviceFirst
, i
= 0;
244 pGraphicsDevice
= pGraphicsDevice
->pNextGraphicsDevice
, i
++)
246 /* Compare the device name */
247 RtlInitUnicodeString(&ustrCurrent
, pGraphicsDevice
->szWinDeviceName
);
248 if (RtlEqualUnicodeString(&ustrCurrent
, pustrDevice
, FALSE
))
256 /* Loop through the list of devices */
257 for (pGraphicsDevice
= gpGraphicsDeviceFirst
, i
= 0;
258 pGraphicsDevice
&& i
< iDevNum
;
259 pGraphicsDevice
= pGraphicsDevice
->pNextGraphicsDevice
, i
++);
263 EngReleaseSemaphore(ghsemGraphicsDeviceList
);
265 return pGraphicsDevice
;
272 PFILE_OBJECT pFileObject
,
273 ULONG ulMajorFunction
,
276 ULONGLONG ullStartOffset
,
277 OUT LPDWORD lpInformation
)
279 PDEVICE_OBJECT pDeviceObject
;
282 IO_STATUS_BLOCK Iosb
;
284 LARGE_INTEGER liStartOffset
;
286 /* Get corresponding device object */
287 pDeviceObject
= IoGetRelatedDeviceObject(pFileObject
);
290 return STATUS_INVALID_PARAMETER
;
293 /* Initialize an event */
294 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
297 liStartOffset
.QuadPart
= ullStartOffset
;
298 pIrp
= IoBuildSynchronousFsdRequest(ulMajorFunction
,
307 return STATUS_INSUFFICIENT_RESOURCES
;
310 /* Call the driver */
311 Status
= IoCallDriver(pDeviceObject
, pIrp
);
313 /* Wait if neccessary */
314 if (STATUS_PENDING
== Status
)
316 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, TRUE
, 0);
317 Status
= Iosb
.Status
;
320 /* Return information to the caller about the operation. */
321 *lpInformation
= Iosb
.Information
;
323 /* Return NTSTATUS */
330 IN PFILE_OBJECT pFileObject
,
333 IN PSIZE_T lpBytesWritten
)
335 EngpFileIoRequest(pFileObject
,
346 IN PFILE_OBJECT pFileObject
,
347 IN DWORD dwIoControlCode
,
349 IN SIZE_T nInBufferSize
,
350 OUT PVOID lpOutBuffer
,
351 IN SIZE_T nOutBufferSize
,
352 OUT LPDWORD lpInformation
)
354 PDEVICE_OBJECT pDeviceObject
;
357 IO_STATUS_BLOCK Iosb
;
360 /* Get corresponding device object */
361 pDeviceObject
= IoGetRelatedDeviceObject(pFileObject
);
364 return STATUS_INVALID_PARAMETER
;
367 /* Initialize an event */
368 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
370 /* Build IO control IRP */
371 pIrp
= IoBuildDeviceIoControlRequest(dwIoControlCode
,
382 return STATUS_INSUFFICIENT_RESOURCES
;
385 /* Call the driver */
386 Status
= IoCallDriver(pDeviceObject
, pIrp
);
388 /* Wait if neccessary */
389 if (Status
== STATUS_PENDING
)
391 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, TRUE
, 0);
392 Status
= Iosb
.Status
;
395 /* Return information to the caller about the operation. */
396 *lpInformation
= Iosb
.Information
;
398 /* This function returns NTSTATUS */
408 DWORD dwIoControlCode
,
412 DWORD nOutBufferSize
,
413 DWORD
*lpBytesReturned
)
418 IO_STATUS_BLOCK Iosb
;
419 PDEVICE_OBJECT DeviceObject
;
421 DPRINT("EngDeviceIoControl() called\n");
423 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
425 DeviceObject
= (PDEVICE_OBJECT
) hDevice
;
427 Irp
= IoBuildDeviceIoControlRequest(dwIoControlCode
,
432 nOutBufferSize
, FALSE
, &Event
, &Iosb
);
433 if (!Irp
) return ERROR_NOT_ENOUGH_MEMORY
;
435 Status
= IoCallDriver(DeviceObject
, Irp
);
437 if (Status
== STATUS_PENDING
)
439 (VOID
)KeWaitForSingleObject(&Event
, Executive
, KernelMode
, TRUE
, 0);
440 Status
= Iosb
.Status
;
443 DPRINT("EngDeviceIoControl(): Returning %X/%X\n", Iosb
.Status
,
446 /* Return information to the caller about the operation. */
447 *lpBytesReturned
= Iosb
.Information
;
449 /* Convert NT status values to win32 error codes. */
452 case STATUS_INSUFFICIENT_RESOURCES
:
453 return ERROR_NOT_ENOUGH_MEMORY
;
455 case STATUS_BUFFER_OVERFLOW
:
456 return ERROR_MORE_DATA
;
458 case STATUS_NOT_IMPLEMENTED
:
459 return ERROR_INVALID_FUNCTION
;
461 case STATUS_INVALID_PARAMETER
:
462 return ERROR_INVALID_PARAMETER
;
464 case STATUS_BUFFER_TOO_SMALL
:
465 return ERROR_INSUFFICIENT_BUFFER
;
467 case STATUS_DEVICE_DOES_NOT_EXIST
:
468 return ERROR_DEV_NOT_EXIST
;
471 return ERROR_IO_PENDING
;