summaryrefslogtreecommitdiffstats
path: root/vidix/dha.c
blob: 4e5d5395efba4e1b8285ce8c6c0efdc9f6f63356 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * VIDIX Direct Hardware Access (DHA).
 * Copyright (C) 2002 Nick Kurshev
 *
 * 1996/10/27   - Robin Cutshaw (robin@xfree86.org)
 *                XFree86 3.3.3 implementation
 * 1999         - Øyvind Aabling.
 *                Modified for GATOS/win/gfxdump.
 *
 * 2002         - library implementation by Nick Kurshev
 *              - dhahelper and some changes by Alex Beregszaszi
 *
 * supported OSes: SVR4, UnixWare, SCO, Solaris,
 *                 FreeBSD, NetBSD, 386BSD, BSDI BSD/386,
 *                 Linux, Mach/386, ISC
 *                 DOS (WATCOM 9.5 compiler), Win9x (with mapdev.vxd)
 * original location: www.linuxvideo.org/gatos
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include "dha.h"
#include "AsmMacros.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#if ARCH_ALPHA
#include <sys/io.h>
#endif
#include <unistd.h>

#if defined(__MINGW32__) || defined(__CYGWIN__)
#include "sysdep/libdha_win32.c"
#elif defined (__EMX__)
#include "sysdep/libdha_os2.c"
#else

#if defined(SVR4) || defined(SCO325)
#  if !(defined(sun) && defined (i386) && defined (SVR4))
#    define DEV_MEM "/dev/pmem"
#  elif defined(PowerMAX_OS)
#    define DEV_MEM "/dev/iomem"
#  endif
#  ifdef SCO325
#   undef DEV_MEM
#   define DEV_MEM "/dev/mem"
#  endif
#elif defined(sun) && defined (i386)
#define DEV_MEM "/dev/xsvc"
# endif /* SVR4 */

#if defined(__OpenBSD__)
#define DEV_APERTURE "/dev/xf86"
#endif

/* Generic version */
#include <sys/mman.h>

#ifndef DEV_MEM
#define DEV_MEM "/dev/mem"
#endif

#ifdef CONFIG_DHAHELPER
#include "dhahelper/dhahelper.h"
#endif

#ifdef CONFIG_SVGAHELPER
#include <svgalib_helper.h>
#endif

static int mem_fd = -1;

void *map_phys_mem(unsigned long base, unsigned long size)
{
#if ARCH_ALPHA
/* TODO: move it into sysdep */
  base += bus_base();
#endif

#ifdef CONFIG_SVGAHELPER
  if ( (mem_fd = open(DEV_SVGA,O_RDWR)) == -1) {
      perror("libdha: SVGAlib kernelhelper failed");
#ifdef CONFIG_DHAHELPER
      goto dha_helper_way;
#else
      goto dev_mem_way;
#endif
  }
  else
      goto mmap;
#endif

#ifdef CONFIG_DHAHELPER
#ifdef CONFIG_SVGAHELPER
dha_helper_way:
#endif
  if ( (mem_fd = open("/dev/dhahelper",O_RDWR)) < 0)
  {
      perror("libdha: DHA kernelhelper failed");
      goto dev_mem_way;
  }
  else
  {
    dhahelper_memory_t mem_req;

    mem_req.operation = MEMORY_OP_MAP;
    mem_req.start = base;
    mem_req.offset = 0;
    mem_req.size = size;

    if (ioctl(mem_fd, DHAHELPER_MEMORY, &mem_req) < 0)
    {
	perror("libdha: DHA kernelhelper failed");
	close(mem_fd);
	goto dev_mem_way;
    }
    else
	goto mmap;
  }
#endif

#if defined(CONFIG_DHAHELPER) || defined (CONFIG_SVGAHELPER)
dev_mem_way:
#endif
#ifdef DEV_APERTURE
  if ((mem_fd = open(DEV_APERTURE, O_RDWR)) == -1)
	perror("libdha: opening aperture failed");
  else {
	void *p = mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,mem_fd,base);

	if (p == MAP_FAILED) {
	    perror("libdha: mapping aperture failed");
	    close(mem_fd);
	} else
	    return p;
  }
#endif

  if ( (mem_fd = open(DEV_MEM,O_RDWR)) == -1)
  {
    perror("libdha: opening /dev/mem failed");
    return MAP_FAILED;
  }

#if defined(CONFIG_DHAHELPER) || defined (CONFIG_SVGAHELPER)
mmap:
#endif
  return mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,mem_fd,base);
}

void unmap_phys_mem(void *ptr, unsigned long size)
{
  int res = munmap(ptr,size);

  if (res == (int)MAP_FAILED)
  {
      perror("libdha: unmapping memory failed");
      return;
  }

  close(mem_fd);
  mem_fd = -1;

  return;
}

#endif /* Generic mmap (not win32, nor os2) */

#if !defined(__alpha__) && !defined(__powerpc__) && !defined(__sh__)
unsigned char INPORT8(unsigned idx)
{
  return inb(idx);
}

unsigned short INPORT16(unsigned idx)
{
  return inw(idx);
}

unsigned INPORT32(unsigned idx)
{
  return inl(idx);
}

void OUTPORT8(unsigned idx,unsigned char val)
{
  outb(idx,val);
}

void OUTPORT16(unsigned idx,unsigned short val)
{
  outw(idx,val);
}

void OUTPORT32(unsigned idx,unsigned val)
{
  outl(idx,val);
}
#endif