Merge svn changes up to r28366
[mplayer.git] / vidix / sysdep / pci_linux.c
blob12da9376b981f9155cc580c1d3bb75bb1599d280
1 /*
2 This file is based on:
3 $XFree86: xc/programs/Xserver/hw/xfree86/etc/scanpci.c,v 3.34.2.17 1998/11/10 11:55:40 dawes Exp $
4 Modified for readability by Nick Kurshev
5 */
6 /*
7 * Copyright 1995 by Robin Cutshaw <robin@XFree86.Org>
9 * Permission to use, copy, modify, distribute, and sell this software and its
10 * documentation for any purpose is hereby granted without fee, provided that
11 * the above copyright notice appear in all copies and that both that
12 * copyright notice and this permission notice appear in supporting
13 * documentation, and that the names of the above listed copyright holder(s)
14 * not be used in advertising or publicity pertaining to distribution of
15 * the software without specific, written prior permission. The above listed
16 * copyright holder(s) make(s) no representations about the suitability of this
17 * software for any purpose. It is provided "as is" without express or
18 * implied warranty.
20 * THE ABOVE LISTED COPYRIGHT HOLDER(S) DISCLAIM(S) ALL WARRANTIES WITH REGARD
21 * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS, IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE
23 * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
24 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
25 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
26 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include <errno.h>
30 #ifdef __i386__
31 //#include <sys/perm.h> doesn't exist on libc5 systems
32 int iopl();
33 #else
34 #if !defined(__sparc__) && !defined(__powerpc__) && !defined(__x86_64__)
35 #include <sys/io.h>
36 #endif
37 #endif
39 #ifdef __sh__
40 #define iopl(x) 1
41 #endif
43 #include "config.h"
45 #ifdef CONFIG_DHAHELPER
46 #include <fcntl.h>
47 int dhahelper_initialized = 0;
48 int dhahelper_fd = 0;
49 #endif
51 #ifdef CONFIG_SVGAHELPER
52 #include <svgalib_helper.h>
53 #ifdef __linux__
54 #include <linux/ioctl.h>
55 #endif
56 #include <fcntl.h>
57 #ifndef SVGALIB_HELPER_IOC_MAGIC
58 /* svgalib 1.9.18+ compatibility ::atmos */
59 #define SVGALIB_HELPER_IOCGPCIINL SVGAHELPER_PCIINL
60 #endif
61 int svgahelper_initialized = 0;
62 int svgahelper_fd = 0;
64 static int pci_config_type(void)
66 return 1;
69 static long pci_config_read_long(
70 unsigned char bus,
71 unsigned char dev,
72 int func,
73 unsigned cmd)
75 pcic_t p;
77 p.address = cmd;
78 p.pcipos = (bus << 8) | dev | (func << 5);
80 if (ioctl(svgahelper_fd, SVGALIB_HELPER_IOCGPCIINL, &p))
81 return -1;
83 return p.val;
86 static int pci_get_vendor(
87 unsigned char bus,
88 unsigned char dev,
89 int func)
91 return pci_config_read_long(bus, dev, func, 0);
93 #endif /* CONFIG_SVGAHELPER */
95 static __inline__ int enable_os_io(void)
97 #ifdef CONFIG_SVGAHELPER
98 svgahelper_fd = open(DEV_SVGA, O_RDWR);
99 if (svgahelper_fd > 0)
101 svgahelper_initialized = 1;
102 return 0;
104 svgahelper_initialized = -1;
105 #endif
107 #ifdef CONFIG_DHAHELPER
108 dhahelper_fd = open("/dev/dhahelper", O_RDWR);
109 if (dhahelper_fd > 0)
111 dhahelper_initialized = 1;
112 return 0;
114 dhahelper_initialized = -1;
115 #endif
117 #if defined(__powerpc__) && defined(__linux__)
118 /* should be fixed? */
119 #else
120 if (iopl(3) != 0)
121 return errno;
122 #endif
123 return 0;
126 static __inline__ int disable_os_io(void)
128 #ifdef CONFIG_SVGAHELPER
129 if (svgahelper_initialized == 1)
130 close(svgahelper_fd);
131 else
132 #endif
133 #ifdef CONFIG_DHAHELPER
134 if (dhahelper_initialized == 1)
135 close(dhahelper_fd);
136 else
137 #endif
138 #if defined(__powerpc__) && defined(__linux__)
139 /* should be fixed? */
140 #else
141 if (iopl(0) != 0)
142 return errno;
143 #endif
144 return 0;
147 #if (defined(__powerpc__) || defined(__sparc__) || defined(__sparc64__) \
148 || defined(__x86_64__) || defined(__sh__)) && defined(__linux__) && !defined(CONFIG_SVGAHELPER)
149 #define CONFIG_PCI_LINUX_PROC
150 #endif
152 #if defined(CONFIG_PCI_LINUX_PROC)
153 static int pci_config_type( void ) { return 1; }
155 /* pci operations for (powerpc) Linux
156 questions, suggestions etc:
157 mplayer-dev-eng@mplayerhq.hu, colin@colino.net*/
158 #include <fcntl.h>
159 //#include <sys/io.h>
160 #include <linux/pci.h>
161 #include "libavutil/common.h"
162 #include "mpbswap.h"
164 static int pci_get_vendor(
165 unsigned char bus,
166 unsigned char dev,
167 int func)
169 int retval;
170 char path[100];
171 int fd;
172 short vendor, device;
173 sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev);
174 fd = open(path,O_RDONLY|O_SYNC);
175 if (fd == -1) {
176 retval=0xFFFF;
178 else if (pread(fd, &vendor, 2, PCI_VENDOR_ID) == 2 &&
179 pread(fd, &device, 2, PCI_DEVICE_ID) == 2) {
180 vendor = le2me_16(vendor);
181 device = le2me_16(device);
182 retval = vendor + (device<<16); /*no worries about byte order,
183 all ppc are bigendian*/
184 } else {
185 retval = 0xFFFF;
187 if (fd > 0) {
188 close(fd);
190 return retval;
193 static long pci_config_read_long(
194 unsigned char bus,
195 unsigned char dev,
196 int func,
197 unsigned cmd)
199 long retval;
200 char path[100];
201 int fd;
202 sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev);
203 fd = open(path,O_RDONLY|O_SYNC);
204 if (fd == -1) {
205 retval=0;
207 else if (pread(fd, &retval, 4, cmd) == 4) {
208 retval = le2me_32(retval);
209 } else {
210 retval = 0;
212 if (fd > 0) {
213 close(fd);
215 return retval;
217 #endif /* defined(CONFIG_PCI_LINUX_PROC) */