Mercurial > mplayer.hg
comparison libdha/libdha.c @ 3973:138800dfbe22
preliminary support of direct hardware access
author | nick |
---|---|
date | Fri, 04 Jan 2002 10:32:26 +0000 |
parents | |
children | 0d9de811e312 |
comparison
equal
deleted
inserted
replaced
3972:f021941d26c9 | 3973:138800dfbe22 |
---|---|
1 /* | |
2 libgha.c - Library for direct hardware access | |
3 Copyrights: | |
4 1996/10/27 - Robin Cutshaw (robin@xfree86.org) | |
5 XFree86 3.3.3 implementation | |
6 1999 - Øyvind Aabling. | |
7 Modified for GATOS/win/gfxdump. | |
8 | |
9 2002 - library implementation by Nick Kurshev | |
10 | |
11 supported O/S's: SVR4, UnixWare, SCO, Solaris, | |
12 FreeBSD, NetBSD, 386BSD, BSDI BSD/386, | |
13 Linux, Mach/386, ISC | |
14 DOS (WATCOM 9.5 compiler), Win9x (with mapdev.vxd) | |
15 Licence: GPL | |
16 Original location: www.linuxvideo.org/gatos | |
17 */ | |
18 | |
19 #include "libdha.h" | |
20 #include "AsmMacros.h" | |
21 #include <stdio.h> | |
22 #include <stdlib.h> | |
23 #include <string.h> | |
24 #include <fcntl.h> | |
25 #include <sys/stat.h> | |
26 #include <sys/types.h> | |
27 #include <unistd.h> | |
28 | |
29 #ifdef _WIN32 | |
30 // MAPDEV.h - include file for VxD MAPDEV | |
31 // Copyright (c) 1996 Vireo Software, Inc. | |
32 | |
33 #include <windows.h> | |
34 | |
35 // This is the request structure that applications use | |
36 // to request services from the MAPDEV VxD. | |
37 | |
38 typedef struct _MapDevRequest | |
39 { | |
40 DWORD mdr_ServiceID; // supplied by caller | |
41 LPVOID mdr_PhysicalAddress; // supplied by caller | |
42 DWORD mdr_SizeInBytes; // supplied by caller | |
43 LPVOID mdr_LinearAddress; // returned by VxD | |
44 WORD mdr_Selector; // returned if 16-bit caller | |
45 WORD mdr_Status; // MDR_xxxx code below | |
46 } MAPDEVREQUEST, *PMAPDEVREQUEST; | |
47 | |
48 #define MDR_SERVICE_MAP CTL_CODE(FILE_DEVICE_UNKNOWN, 1, METHOD_NEITHER, FILE_ANY_ACCESS) | |
49 #define MDR_SERVICE_UNMAP CTL_CODE(FILE_DEVICE_UNKNOWN, 2, METHOD_NEITHER, FILE_ANY_ACCESS) | |
50 | |
51 #define MDR_STATUS_SUCCESS 1 | |
52 #define MDR_STATUS_ERROR 0 | |
53 /*#include "winioctl.h"*/ | |
54 #define FILE_DEVICE_UNKNOWN 0x00000022 | |
55 #define METHOD_NEITHER 3 | |
56 #define FILE_ANY_ACCESS 0 | |
57 #define CTL_CODE( DeviceType, Function, Method, Access ) ( \ | |
58 ((DeviceType)<<16) | ((Access)<<14) | ((Function)<<2) | (Method) ) | |
59 | |
60 /* Memory Map a piece of Real Memory */ | |
61 void *map_phys_mem(unsigned base, unsigned size) { | |
62 | |
63 HANDLE hDevice ; | |
64 PVOID inBuf[1] ; /* buffer for struct pointer to VxD */ | |
65 DWORD RetInfo[2] ; /* buffer to receive data from VxD */ | |
66 DWORD cbBytesReturned ; /* count of bytes returned from VxD */ | |
67 MAPDEVREQUEST req ; /* map device request structure */ | |
68 DWORD *pNicstar, Status, Time ; int i ; char *endptr ; | |
69 const PCHAR VxDName = "\\\\.\\MAPDEV.VXD" ; | |
70 const PCHAR VxDNameAlreadyLoaded = "\\\\.\\MAPDEV" ; | |
71 | |
72 hDevice = CreateFile(VxDName, 0,0,0, | |
73 CREATE_NEW, FILE_FLAG_DELETE_ON_CLOSE, 0) ; | |
74 if (hDevice == INVALID_HANDLE_VALUE) | |
75 hDevice = CreateFile(VxDNameAlreadyLoaded, 0,0,0, | |
76 CREATE_NEW, FILE_FLAG_DELETE_ON_CLOSE, 0) ; | |
77 if (hDevice == INVALID_HANDLE_VALUE) { | |
78 fprintf(stderr, "Cannot open driver, error=%08lx\n", GetLastError()) ; | |
79 exit(1) ; } | |
80 | |
81 req.mdr_ServiceID = MDR_SERVICE_MAP ; | |
82 req.mdr_PhysicalAddress = (PVOID)base ; | |
83 req.mdr_SizeInBytes = size ; | |
84 inBuf[0] = &req ; | |
85 | |
86 if ( ! DeviceIoControl(hDevice, MDR_SERVICE_MAP, inBuf, sizeof(PVOID), | |
87 NULL, 0, &cbBytesReturned, NULL) ) { | |
88 fprintf(stderr, "Failed to map device\n") ; exit(1) ; } | |
89 | |
90 return (void*)req.mdr_LinearAddress ; | |
91 } | |
92 | |
93 void unmap_phys_mem(void *ptr, unsigned size) { } | |
94 | |
95 #else | |
96 #include <sys/mman.h> | |
97 | |
98 static int mem=-1; | |
99 void *map_phys_mem(unsigned base, unsigned size) | |
100 { | |
101 void *ptr; | |
102 if ( (mem = open("/dev/mem",O_RDWR)) == -1) { | |
103 perror("libdha: open(/dev/mem) failed") ; exit(1) ; | |
104 } | |
105 ptr=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,mem,base) ; | |
106 if ((int)ptr == -1) { | |
107 perror("libdha: mmap() failed") ; exit(1) ; | |
108 } | |
109 return ptr; | |
110 } | |
111 | |
112 void unmap_phys_mem(void *ptr, unsigned size) | |
113 { | |
114 int res=munmap(ptr,size) ; | |
115 if (res == -1) { perror("libdha: munmap() failed") ; exit(1) ; } | |
116 close(mem); | |
117 } | |
118 #endif | |
119 | |
120 unsigned char INREG8(unsigned idx) | |
121 { | |
122 return inb(idx); | |
123 } | |
124 | |
125 unsigned short INREG16(unsigned idx) | |
126 { | |
127 return inw(idx); | |
128 } | |
129 | |
130 unsigned INREG32(unsigned idx) | |
131 { | |
132 return inl(idx); | |
133 } | |
134 | |
135 void OUTREG8(unsigned idx,unsigned char val) | |
136 { | |
137 outb(idx,val); | |
138 } | |
139 | |
140 void OUTREG16(unsigned idx,unsigned short val) | |
141 { | |
142 outw(idx,val); | |
143 } | |
144 | |
145 void OUTREG32(unsigned idx,unsigned val) | |
146 { | |
147 outl(idx,val); | |
148 } |