annotate libdha/libdha.c @ 4088:4435dc3b8bef

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