sync with trunk r46493
[reactos.git] / drivers / video / videoprt / int10.c
1 /*
2 * VideoPort driver
3 *
4 * Copyright (C) 2002, 2003, 2004 ReactOS Team
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
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 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "videoprt.h"
23 #include "internal/i386/v86m.h"
24
25 /* PRIVATE FUNCTIONS **********************************************************/
26
27 #if defined(_M_IX86)
28 VP_STATUS NTAPI
29 IntInt10AllocateBuffer(
30 IN PVOID Context,
31 OUT PUSHORT Seg,
32 OUT PUSHORT Off,
33 IN OUT PULONG Length)
34 {
35 PVOID MemoryAddress;
36 NTSTATUS Status;
37 PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
38 KAPC_STATE ApcState;
39
40 TRACE_(VIDEOPRT, "IntInt10AllocateBuffer\n");
41
42 IntAttachToCSRSS(&CallingProcess, &ApcState);
43
44 MemoryAddress = (PVOID)0x20000;
45 Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &MemoryAddress, 0,
46 Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
47
48 if (!NT_SUCCESS(Status))
49 {
50 WARN_(VIDEOPRT, "- ZwAllocateVirtualMemory failed\n");
51 IntDetachFromCSRSS(&CallingProcess, &ApcState);
52 return ERROR_NOT_ENOUGH_MEMORY;
53 }
54
55 if (MemoryAddress > (PVOID)(0x100000 - *Length))
56 {
57 ZwFreeVirtualMemory(NtCurrentProcess(), &MemoryAddress, Length,
58 MEM_RELEASE);
59 WARN_(VIDEOPRT, "- Unacceptable memory allocated\n");
60 IntDetachFromCSRSS(&CallingProcess, &ApcState);
61 return ERROR_NOT_ENOUGH_MEMORY;
62 }
63
64 *Seg = (ULONG)MemoryAddress >> 4;
65 *Off = (ULONG)MemoryAddress & 0xF;
66
67 INFO_(VIDEOPRT, "- Segment: %x\n", (ULONG)MemoryAddress >> 4);
68 INFO_(VIDEOPRT, "- Offset: %x\n", (ULONG)MemoryAddress & 0xF);
69 INFO_(VIDEOPRT, "- Length: %x\n", *Length);
70
71 IntDetachFromCSRSS(&CallingProcess, &ApcState);
72
73 return NO_ERROR;
74 }
75
76 VP_STATUS NTAPI
77 IntInt10FreeBuffer(
78 IN PVOID Context,
79 IN USHORT Seg,
80 IN USHORT Off)
81 {
82 PVOID MemoryAddress = (PVOID)((Seg << 4) | Off);
83 NTSTATUS Status;
84 PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
85 KAPC_STATE ApcState;
86
87 TRACE_(VIDEOPRT, "IntInt10FreeBuffer\n");
88 INFO_(VIDEOPRT, "- Segment: %x\n", Seg);
89 INFO_(VIDEOPRT, "- Offset: %x\n", Off);
90
91 IntAttachToCSRSS(&CallingProcess, &ApcState);
92 Status = ZwFreeVirtualMemory(NtCurrentProcess(), &MemoryAddress, 0,
93 MEM_RELEASE);
94 IntDetachFromCSRSS(&CallingProcess, &ApcState);
95
96 return Status;
97 }
98
99 VP_STATUS NTAPI
100 IntInt10ReadMemory(
101 IN PVOID Context,
102 IN USHORT Seg,
103 IN USHORT Off,
104 OUT PVOID Buffer,
105 IN ULONG Length)
106 {
107 PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
108 KAPC_STATE ApcState;
109
110 TRACE_(VIDEOPRT, "IntInt10ReadMemory\n");
111 INFO_(VIDEOPRT, "- Segment: %x\n", Seg);
112 INFO_(VIDEOPRT, "- Offset: %x\n", Off);
113 INFO_(VIDEOPRT, "- Buffer: %x\n", Buffer);
114 INFO_(VIDEOPRT, "- Length: %x\n", Length);
115
116 IntAttachToCSRSS(&CallingProcess, &ApcState);
117 RtlCopyMemory(Buffer, (PVOID)((Seg << 4) | Off), Length);
118 IntDetachFromCSRSS(&CallingProcess, &ApcState);
119
120 return NO_ERROR;
121 }
122
123 VP_STATUS NTAPI
124 IntInt10WriteMemory(
125 IN PVOID Context,
126 IN USHORT Seg,
127 IN USHORT Off,
128 IN PVOID Buffer,
129 IN ULONG Length)
130 {
131 PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
132 KAPC_STATE ApcState;
133
134 TRACE_(VIDEOPRT, "IntInt10WriteMemory\n");
135 INFO_(VIDEOPRT, "- Segment: %x\n", Seg);
136 INFO_(VIDEOPRT, "- Offset: %x\n", Off);
137 INFO_(VIDEOPRT, "- Buffer: %x\n", Buffer);
138 INFO_(VIDEOPRT, "- Length: %x\n", Length);
139
140 IntAttachToCSRSS(&CallingProcess, &ApcState);
141 RtlCopyMemory((PVOID)((Seg << 4) | Off), Buffer, Length);
142 IntDetachFromCSRSS(&CallingProcess, &ApcState);
143
144 return NO_ERROR;
145 }
146
147 VP_STATUS
148 NTAPI
149 IntInt10CallBios(
150 IN PVOID Context,
151 IN OUT PINT10_BIOS_ARGUMENTS BiosArguments)
152 {
153 CONTEXT BiosContext;
154 NTSTATUS Status;
155 PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
156 KAPC_STATE ApcState;
157
158 /* Attach to CSRSS */
159 IntAttachToCSRSS(&CallingProcess, &ApcState);
160
161 /* Clear the context */
162 RtlZeroMemory(&BiosContext, sizeof(CONTEXT));
163
164 /* Fill out the bios arguments */
165 BiosContext.Eax = BiosArguments->Eax;
166 BiosContext.Ebx = BiosArguments->Ebx;
167 BiosContext.Ecx = BiosArguments->Ecx;
168 BiosContext.Edx = BiosArguments->Edx;
169 BiosContext.Esi = BiosArguments->Esi;
170 BiosContext.Edi = BiosArguments->Edi;
171 BiosContext.Ebp = BiosArguments->Ebp;
172 BiosContext.SegDs = BiosArguments->SegDs;
173 BiosContext.SegEs = BiosArguments->SegEs;
174
175 /* Do the ROM BIOS call */
176 Status = Ke386CallBios(0x10, &BiosContext);
177
178 /* Return the arguments */
179 BiosArguments->Eax = BiosContext.Eax;
180 BiosArguments->Ebx = BiosContext.Ebx;
181 BiosArguments->Ecx = BiosContext.Ecx;
182 BiosArguments->Edx = BiosContext.Edx;
183 BiosArguments->Esi = BiosContext.Esi;
184 BiosArguments->Edi = BiosContext.Edi;
185 BiosArguments->Ebp = BiosContext.Ebp;
186 BiosArguments->SegDs = BiosContext.SegDs;
187 BiosArguments->SegEs = BiosContext.SegEs;
188
189 /* Detach and return status */
190 IntDetachFromCSRSS(&CallingProcess, &ApcState);
191 if (NT_SUCCESS(Status)) return NO_ERROR;
192 return ERROR_INVALID_PARAMETER;
193 }
194 #endif
195
196 /* PUBLIC FUNCTIONS ***********************************************************/
197
198 /*
199 * @implemented
200 */
201
202 VP_STATUS NTAPI
203 VideoPortInt10(
204 IN PVOID HwDeviceExtension,
205 IN PVIDEO_X86_BIOS_ARGUMENTS BiosArguments)
206 {
207 #if defined(_M_IX86)
208 CONTEXT BiosContext;
209 NTSTATUS Status;
210 PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
211 KAPC_STATE ApcState;
212
213 if (!CsrssInitialized)
214 {
215 return ERROR_INVALID_PARAMETER;
216 }
217
218 /* Attach to CSRSS */
219 IntAttachToCSRSS(&CallingProcess, &ApcState);
220
221 /* Clear the context */
222 RtlZeroMemory(&BiosContext, sizeof(CONTEXT));
223
224 /* Fill out the bios arguments */
225 BiosContext.Eax = BiosArguments->Eax;
226 BiosContext.Ebx = BiosArguments->Ebx;
227 BiosContext.Ecx = BiosArguments->Ecx;
228 BiosContext.Edx = BiosArguments->Edx;
229 BiosContext.Esi = BiosArguments->Esi;
230 BiosContext.Edi = BiosArguments->Edi;
231 BiosContext.Ebp = BiosArguments->Ebp;
232
233 /* Do the ROM BIOS call */
234 Status = Ke386CallBios(0x10, &BiosContext);
235
236 /* Return the arguments */
237 BiosArguments->Eax = BiosContext.Eax;
238 BiosArguments->Ebx = BiosContext.Ebx;
239 BiosArguments->Ecx = BiosContext.Ecx;
240 BiosArguments->Edx = BiosContext.Edx;
241 BiosArguments->Esi = BiosContext.Esi;
242 BiosArguments->Edi = BiosContext.Edi;
243 BiosArguments->Ebp = BiosContext.Ebp;
244
245 /* Detach from CSRSS */
246 IntDetachFromCSRSS(&CallingProcess, &ApcState);
247 if (NT_SUCCESS(Status)) return NO_ERROR;
248 return ERROR_INVALID_PARAMETER;
249 #else
250 /* Not implemented for anything else than X86*/
251 DPRINT1("Int10 not available on non-x86!\n");
252 return ERROR_INVALID_FUNCTION;
253 #endif
254 }