annotate vidix/sysdep/libdha_win32.c @ 27795:51c2de6369b6

cosmetics: alphabetical order
author diego
date Sun, 26 Oct 2008 07:33:15 +0000
parents ed6ee2ef3b53
children 8df85ad26746
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4164
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
1 /*
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
2 MAPDEV.h - include file for VxD MAPDEV
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
3 Copyright (c) 1996 Vireo Software, Inc.
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
4 Modified for libdha by Nick Kurshev.
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
5 */
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
6
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
7 #include <windows.h>
12056
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
8 #include <ddk/ntddk.h>
26994
4eb30f58ea41 Fix dhahelper.h #include paths.
diego
parents: 26900
diff changeset
9 #include "vidix/dhahelperwin/dhahelper.h"
12056
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
10
4164
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
11 /*
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
12 This is the request structure that applications use
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
13 to request services from the MAPDEV VxD.
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
14 */
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
15
25607
9d0b189ce1b2 Fix illegal identifiers: Names starting with __ or _ and uppercase are reserved
diego
parents: 22900
diff changeset
16 typedef struct MapDevRequest
4164
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
17 {
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
18 DWORD mdr_ServiceID; /* supplied by caller */
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
19 LPVOID mdr_PhysicalAddress; /* supplied by caller */
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
20 DWORD mdr_SizeInBytes; /* supplied by caller */
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
21 LPVOID mdr_LinearAddress; /* returned by VxD */
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
22 WORD mdr_Selector; /* returned if 16-bit caller */
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
23 WORD mdr_Status; /* MDR_xxxx code below */
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
24 } MAPDEVREQUEST, *PMAPDEVREQUEST;
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
25
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
26 #define MDR_SERVICE_MAP CTL_CODE(FILE_DEVICE_UNKNOWN, 1, METHOD_NEITHER, FILE_ANY_ACCESS)
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
27 #define MDR_SERVICE_UNMAP CTL_CODE(FILE_DEVICE_UNKNOWN, 2, METHOD_NEITHER, FILE_ANY_ACCESS)
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
28
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
29 #define MDR_STATUS_SUCCESS 1
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
30 #define MDR_STATUS_ERROR 0
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
31 /*#include "winioctl.h"*/
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
32 #define FILE_DEVICE_UNKNOWN 0x00000022
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
33 #define METHOD_NEITHER 3
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
34
12056
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
35
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
36 int IsWinNT(){
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
37 OSVERSIONINFO OSVersionInfo;
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
38 OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
39 GetVersionEx(&OSVersionInfo);
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
40 return OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT;
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
41 }
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
42
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
43 static HANDLE hDriver = INVALID_HANDLE_VALUE;
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
44
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
45
4164
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
46 /* Memory Map a piece of Real Memory */
10980
e687fa1d55c6 port libdha to mingw
faust3
parents: 4164
diff changeset
47 void *map_phys_mem(unsigned long base, unsigned long size) {
12056
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
48 if(!IsWinNT()){
4164
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
49 HANDLE hDevice ;
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
50 PVOID inBuf[1] ; /* buffer for struct pointer to VxD */
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
51 DWORD cbBytesReturned ; /* count of bytes returned from VxD */
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
52 MAPDEVREQUEST req ; /* map device request structure */
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
53 const PCHAR VxDName = "\\\\.\\MAPDEV.VXD" ;
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
54 const PCHAR VxDNameAlreadyLoaded = "\\\\.\\MAPDEV" ;
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
55
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
56 hDevice = CreateFile(VxDName, 0,0,0,
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
57 CREATE_NEW, FILE_FLAG_DELETE_ON_CLOSE, 0) ;
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
58 if (hDevice == INVALID_HANDLE_VALUE)
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
59 hDevice = CreateFile(VxDNameAlreadyLoaded, 0,0,0,
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
60 CREATE_NEW, FILE_FLAG_DELETE_ON_CLOSE, 0) ;
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
61 if (hDevice == INVALID_HANDLE_VALUE) {
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
62 fprintf(stderr, "Cannot open driver, error=%08lx\n", GetLastError()) ;
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
63 exit(1) ; }
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
64
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
65 req.mdr_ServiceID = MDR_SERVICE_MAP ;
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
66 req.mdr_PhysicalAddress = (PVOID)base ;
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
67 req.mdr_SizeInBytes = size ;
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
68 inBuf[0] = &req ;
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
69
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
70 if ( ! DeviceIoControl(hDevice, MDR_SERVICE_MAP, inBuf, sizeof(PVOID),
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
71 NULL, 0, &cbBytesReturned, NULL) ) {
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
72 fprintf(stderr, "Failed to map device\n") ; exit(1) ; }
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
73
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
74 return (void*)req.mdr_LinearAddress ;
12056
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
75 }
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
76 else{
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
77 dhahelper_t dhahelper_priv;
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
78 DWORD dwBytesReturned;
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
79 dhahelper_priv.size = size;
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
80 dhahelper_priv.base = base;
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
81 if(hDriver==INVALID_HANDLE_VALUE)hDriver = CreateFile("\\\\.\\DHAHELPER",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
82 if (!DeviceIoControl(hDriver, IOCTL_DHAHELPER_MAPPHYSTOLIN, &dhahelper_priv,sizeof(dhahelper_t), &dhahelper_priv, sizeof(dhahelper_t),&dwBytesReturned, NULL)){
17971
fd0d4b94b5ec Change some printf calls to fprintf.
diego
parents: 12056
diff changeset
83 fprintf(stderr,"Unable to map the requested memory region.\n");
12056
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
84 return NULL;
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
85 }
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
86 else return dhahelper_priv.ptr;
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
87 }
4164
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
88 }
2e3262002acb Improved readability and new stuffs
nick
parents:
diff changeset
89
12056
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
90 void unmap_phys_mem(void *ptr, unsigned long size) {
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
91 if(IsWinNT()){
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
92 dhahelper_t dhahelper_priv;
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
93 DWORD dwBytesReturned;
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
94 dhahelper_priv.ptr = ptr;
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
95 DeviceIoControl(hDriver, IOCTL_DHAHELPER_UNMAPPHYSADDR, &dhahelper_priv,sizeof(dhahelper_t), NULL, 0, &dwBytesReturned, NULL);
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
96 }
3a727f52c4a2 windows xp support
faust3
parents: 10980
diff changeset
97 }