win-pvdrivers

view xenpci/xenpci_export.c @ 1013:43e76afb2398

Fix compiler warnings under 2000
author James Harper <james.harper@bendigoit.com.au>
date Mon Feb 11 20:59:11 2013 +1100 (2013-02-11)
parents 9fb8690938b3
children cd72cd0e1c19
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2012 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #include "xenpci.h"
22 ULONG
23 XnGetVersion() {
24 return 1;
25 }
27 VOID
28 XenPci_BackendStateCallback(char *path, PVOID context) {
29 PXENPCI_PDO_DEVICE_DATA xppdd = context;
30 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
31 PCHAR err;
32 PCHAR value;
33 ULONG backend_state;
35 FUNCTION_ENTER();
36 //RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
37 FUNCTION_MSG("Read path=%s\n", path);
38 err = XenBus_Read(xpdd, XBT_NIL, path, &value);
39 if (err) {
40 FUNCTION_MSG("Error %s\n", err);
41 XenPci_FreeMem(err);
42 /* this is pretty catastrophic... */
43 /* maybe call the callback with an unknown or something... or just ignore? */
44 FUNCTION_EXIT();
45 return;
46 }
47 FUNCTION_MSG("Read value=%s\n", value);
48 backend_state = atoi(value);
49 XenPci_FreeMem(value);
50 xppdd->device_callback(xppdd->device_callback_context, XN_DEVICE_CALLBACK_BACKEND_STATE, (PVOID)(ULONG_PTR)backend_state);
51 FUNCTION_EXIT();
52 }
54 XN_HANDLE
55 XnOpenDevice(PDEVICE_OBJECT pdo, PXN_DEVICE_CALLBACK callback, PVOID context) {
56 WDFDEVICE device;
57 PXENPCI_PDO_DEVICE_DATA xppdd;
58 PXENPCI_DEVICE_DATA xpdd;
59 PCHAR response;
60 CHAR path[128];
62 FUNCTION_ENTER();
63 device = WdfWdmDeviceGetWdfDeviceHandle(pdo);
64 if (!device) {
65 FUNCTION_MSG("Failed to get WDFDEVICE for %p\n", pdo);
66 return NULL;
67 }
68 xppdd = GetXppdd(device);
69 xpdd = xppdd->xpdd;
70 xppdd->device_callback = callback;
71 xppdd->device_callback_context = context;
72 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
73 response = XenBus_AddWatch(xpdd, XBT_NIL, path, XenPci_BackendStateCallback, xppdd);
74 if (response) {
75 FUNCTION_MSG("XnAddWatch - %s = %s\n", path, response);
76 XenPci_FreeMem(response);
77 xppdd->device_callback = NULL;
78 xppdd->device_callback_context = NULL;
79 FUNCTION_EXIT();
80 return NULL;
81 }
83 FUNCTION_EXIT();
84 return xppdd;
85 }
87 VOID
88 XnCloseDevice(XN_HANDLE handle) {
89 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
90 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
91 PCHAR response;
92 CHAR path[128];
94 FUNCTION_ENTER();
95 RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
96 response = XenBus_RemWatch(xpdd, XBT_NIL, path, XenPci_BackendStateCallback, xppdd);
97 if (response) {
98 FUNCTION_MSG("XnRemWatch - %s = %s\n", path, response);
99 XenPci_FreeMem(response);
100 }
101 xppdd->device_callback = NULL;
102 xppdd->device_callback_context = NULL;
103 FUNCTION_EXIT();
104 return;
105 }
107 NTSTATUS
108 XnBindEvent(XN_HANDLE handle, evtchn_port_t *port, PXN_EVENT_CALLBACK callback, PVOID context) {
109 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
110 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
111 *port = EvtChn_AllocUnbound(xpdd, xppdd->backend_id);
112 return EvtChn_Bind(xpdd, *port, callback, context, EVT_ACTION_FLAGS_DEFAULT);
113 }
115 NTSTATUS
116 XnUnbindEvent(XN_HANDLE handle, evtchn_port_t port) {
117 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
118 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
119 EvtChn_Unbind(xpdd, port);
120 EvtChn_Close(xpdd, port);
121 return STATUS_SUCCESS;
122 }
124 grant_ref_t
125 XnGrantAccess(XN_HANDLE handle, uint32_t frame, int readonly, grant_ref_t ref, ULONG tag) {
126 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
127 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
128 return GntTbl_GrantAccess(xpdd, xppdd->backend_id, frame, readonly, ref, tag);
129 }
131 BOOLEAN
132 XnEndAccess(XN_HANDLE handle, grant_ref_t ref, BOOLEAN keepref, ULONG tag) {
133 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
134 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
135 return GntTbl_EndAccess(xpdd, ref, keepref, tag);
136 }
138 grant_ref_t
139 XnAllocateGrant(XN_HANDLE handle, ULONG tag) {
140 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
141 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
142 return GntTbl_GetRef(xpdd, tag);
143 }
145 VOID
146 XnFreeGrant(XN_HANDLE handle, grant_ref_t ref, ULONG tag) {
147 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
148 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
149 GntTbl_PutRef(xpdd, ref, tag);
150 }
152 /* result must be freed with XnFreeMem() */
153 NTSTATUS
154 XnReadString(XN_HANDLE handle, ULONG base, PCHAR path, PCHAR *value) {
155 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
156 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
157 PCHAR response;
158 CHAR full_path[1024];
160 switch(base) {
161 case XN_BASE_FRONTEND:
162 RtlStringCbCopyA(full_path, ARRAY_SIZE(full_path), xppdd->path);
163 break;
164 case XN_BASE_BACKEND:
165 RtlStringCbCopyA(full_path, ARRAY_SIZE(full_path), xppdd->backend_path);
166 break;
167 case XN_BASE_GLOBAL:
168 full_path[0] = 0;
169 }
170 RtlStringCbCatA(full_path, ARRAY_SIZE(full_path), "/");
171 RtlStringCbCatA(full_path, ARRAY_SIZE(full_path), path);
173 response = XenBus_Read(xpdd, XBT_NIL, full_path, value);
174 if (response) {
175 FUNCTION_MSG("Error reading %s - %s\n", full_path, response);
176 XenPci_FreeMem(response);
177 return STATUS_UNSUCCESSFUL;
178 }
179 return STATUS_SUCCESS;
180 }
182 NTSTATUS
183 XnWriteString(XN_HANDLE handle, ULONG base, PCHAR path, PCHAR value) {
184 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
185 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
186 PCHAR response;
187 CHAR full_path[1024];
189 switch(base) {
190 case XN_BASE_FRONTEND:
191 RtlStringCbCopyA(full_path, ARRAY_SIZE(full_path), xppdd->path);
192 break;
193 case XN_BASE_BACKEND:
194 RtlStringCbCopyA(full_path, ARRAY_SIZE(full_path), xppdd->backend_path);
195 break;
196 case XN_BASE_GLOBAL:
197 full_path[0] = 0;
198 }
199 RtlStringCbCatA(full_path, ARRAY_SIZE(full_path), "/");
200 RtlStringCbCatA(full_path, ARRAY_SIZE(full_path), path);
202 //FUNCTION_MSG("XnWriteString(%s, %s)\n", full_path, value);
203 response = XenBus_Write(xpdd, XBT_NIL, full_path, value);
204 if (response) {
205 FUNCTION_MSG("XnWriteString - %s = %s\n", full_path, response);
206 XenPci_FreeMem(response);
207 FUNCTION_EXIT();
208 return STATUS_UNSUCCESSFUL;
209 }
210 return STATUS_SUCCESS;
211 }
213 NTSTATUS
214 XnReadInt32(XN_HANDLE handle, ULONG base, PCHAR path, ULONG *value) {
215 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
216 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
217 CHAR full_path[1024];
218 PCHAR response;
219 PCHAR string_value;
221 switch(base) {
222 case XN_BASE_FRONTEND:
223 RtlStringCbCopyA(full_path, ARRAY_SIZE(full_path), xppdd->path);
224 break;
225 case XN_BASE_BACKEND:
226 RtlStringCbCopyA(full_path, ARRAY_SIZE(full_path), xppdd->backend_path);
227 break;
228 case XN_BASE_GLOBAL:
229 full_path[0] = 0;
230 }
231 RtlStringCbCatA(full_path, ARRAY_SIZE(full_path), "/");
232 RtlStringCbCatA(full_path, ARRAY_SIZE(full_path), path);
234 response = XenBus_Read(xpdd, XBT_NIL, full_path, &string_value);
235 if (response) {
236 FUNCTION_MSG("XnReadInt - %s = %s\n", full_path, response);
237 XenPci_FreeMem(response);
238 FUNCTION_EXIT();
239 return STATUS_UNSUCCESSFUL;
240 }
241 *value = atoi(string_value);
242 return STATUS_SUCCESS;
243 }
245 NTSTATUS
246 XnWriteInt32(XN_HANDLE handle, ULONG base, PCHAR path, ULONG value) {
247 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
248 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
249 CHAR full_path[1024];
250 PCHAR response;
252 switch(base) {
253 case XN_BASE_FRONTEND:
254 RtlStringCbCopyA(full_path, ARRAY_SIZE(full_path), xppdd->path);
255 break;
256 case XN_BASE_BACKEND:
257 RtlStringCbCopyA(full_path, ARRAY_SIZE(full_path), xppdd->backend_path);
258 break;
259 case XN_BASE_GLOBAL:
260 full_path[0] = 0;
261 }
262 RtlStringCbCatA(full_path, ARRAY_SIZE(full_path), "/");
263 RtlStringCbCatA(full_path, ARRAY_SIZE(full_path), path);
265 //FUNCTION_MSG("XnWriteInt32(%s, %d)\n", full_path, value);
266 response = XenBus_Printf(xpdd, XBT_NIL, full_path, "%d", value);
267 if (response) {
268 FUNCTION_MSG("XnWriteInt - %s = %s\n", full_path, response);
269 XenPci_FreeMem(response);
270 FUNCTION_EXIT();
271 return STATUS_UNSUCCESSFUL;
272 }
273 return STATUS_SUCCESS;
274 }
276 NTSTATUS
277 XnReadInt64(XN_HANDLE handle, ULONG base, PCHAR path, ULONGLONG *value) {
278 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
279 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
280 CHAR full_path[1024];
281 PCHAR response;
282 PCHAR string_value;
283 PCHAR ptr;
285 switch(base) {
286 case XN_BASE_FRONTEND:
287 RtlStringCbCopyA(full_path, ARRAY_SIZE(full_path), xppdd->path);
288 break;
289 case XN_BASE_BACKEND:
290 RtlStringCbCopyA(full_path, ARRAY_SIZE(full_path), xppdd->backend_path);
291 break;
292 case XN_BASE_GLOBAL:
293 full_path[0] = 0;
294 }
295 RtlStringCbCatA(full_path, ARRAY_SIZE(full_path), "/");
296 RtlStringCbCatA(full_path, ARRAY_SIZE(full_path), path);
298 response = XenBus_Read(xpdd, XBT_NIL, full_path, &string_value);
299 if (response) {
300 FUNCTION_MSG("XnReadInt - %s = %s\n", full_path, response);
301 XenPci_FreeMem(response);
302 FUNCTION_EXIT();
303 return STATUS_UNSUCCESSFUL;
304 }
305 *value = 0;
306 for (ptr = string_value; *ptr && *ptr >= '0' && *ptr <= '9'; ptr++) {
307 *value *= 10;
308 *value += (*ptr) - '0';
309 }
310 return STATUS_SUCCESS;
311 }
313 NTSTATUS
314 XnWriteInt64(XN_HANDLE handle, ULONG base, PCHAR path, ULONGLONG value) {
315 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
316 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
317 CHAR full_path[1024];
318 PCHAR response;
320 switch(base) {
321 case XN_BASE_FRONTEND:
322 RtlStringCbCopyA(full_path, ARRAY_SIZE(full_path), xppdd->path);
323 break;
324 case XN_BASE_BACKEND:
325 RtlStringCbCopyA(full_path, ARRAY_SIZE(full_path), xppdd->backend_path);
326 break;
327 case XN_BASE_GLOBAL:
328 full_path[0] = 0;
329 }
330 RtlStringCbCatA(full_path, ARRAY_SIZE(full_path), "/");
331 RtlStringCbCatA(full_path, ARRAY_SIZE(full_path), path);
333 response = XenBus_Printf(xpdd, XBT_NIL, full_path, "%I64d", value);
334 if (response) {
335 FUNCTION_MSG("XnWriteInt - %s = %s\n", full_path, response);
336 XenPci_FreeMem(response);
337 FUNCTION_EXIT();
338 return STATUS_UNSUCCESSFUL;
339 }
340 return STATUS_SUCCESS;
341 }
343 NTSTATUS
344 XnNotify(XN_HANDLE handle, evtchn_port_t port) {
345 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
346 PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
347 return EvtChn_Notify(xpdd, port);
348 }
350 /* called at PASSIVE_LEVEL */
351 VOID
352 XnGetValue(XN_HANDLE handle, ULONG value_type, PVOID value) {
353 PXENPCI_PDO_DEVICE_DATA xppdd = handle;
354 //PXENPCI_DEVICE_DATA xpdd = xppdd->xpdd;
355 DECLARE_UNICODE_STRING_SIZE(my_device_name, 128);
356 ULONG i;
358 switch (value_type) {
359 case XN_VALUE_TYPE_QEMU_HIDE_FLAGS:
360 *(PULONG)value = (ULONG)qemu_hide_flags_value;
361 break;
362 case XN_VALUE_TYPE_QEMU_FILTER:
363 *(PULONG)value = FALSE;
364 RtlUnicodeStringPrintf(&my_device_name, L"#%S#", xppdd->device);
365 for (i = 0; i < WdfCollectionGetCount(qemu_hide_devices); i++) {
366 WDFSTRING wdf_string = WdfCollectionGetItem(qemu_hide_devices, i);
367 UNICODE_STRING hide_device_name;
368 WdfStringGetUnicodeString(wdf_string, &hide_device_name);
369 if (RtlCompareUnicodeString(&hide_device_name, &my_device_name, TRUE) != 0) {
370 *(PULONG)value = TRUE;
371 break;
372 }
373 }
374 break;
375 default:
376 FUNCTION_MSG("GetValue unknown type %d\n", value_type);
377 break;
378 }
379 }
381 /* called by storage devices in dump mode to re-hook DebugPrint */
382 VOID
383 XnDumpModeHookDebugPrint() {
384 XenPci_DumpModeHookDebugPrint();
385 }