comparison vidix/sysdep/pci_linux.c @ 22900:a9e111b88c4a

merged libdha and libvidix, moved all files from libdha to vidix directory
author ben
date Fri, 06 Apr 2007 15:20:49 +0000
parents libdha/sysdep/pci_linux.c@fa99b3d31d13
children 9c4ad35fabc5
comparison
equal deleted inserted replaced
22899:515545f81186 22900:a9e111b88c4a
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 #include <errno.h>
7 #ifdef __i386__
8 //#include <sys/perm.h> doesn't exist on libc5 systems
9 int iopl();
10 #else
11 #if !defined(__sparc__) && !defined(__powerpc__) && !defined(__x86_64__)
12 #include <sys/io.h>
13 #endif
14 #endif
15
16 #include "config.h"
17
18 #ifdef CONFIG_DHAHELPER
19 #include <fcntl.h>
20 int dhahelper_initialized = 0;
21 int dhahelper_fd = 0;
22 #endif
23
24 #ifdef CONFIG_SVGAHELPER
25 #include <svgalib_helper.h>
26 #ifdef __linux__
27 #include <linux/ioctl.h>
28 #endif
29 #include <fcntl.h>
30 #ifndef SVGALIB_HELPER_IOC_MAGIC
31 /* svgalib 1.9.18+ compatibility ::atmos */
32 #define SVGALIB_HELPER_IOCGPCIINL SVGAHELPER_PCIINL
33 #endif
34 int svgahelper_initialized = 0;
35 int svgahelper_fd = 0;
36
37 static int pci_config_type(void)
38 {
39 return 1;
40 }
41
42 static long pci_config_read_long(
43 unsigned char bus,
44 unsigned char dev,
45 int func,
46 unsigned cmd)
47 {
48 unsigned long config_cmd;
49 pcic_t p;
50
51 p.address = cmd;
52 p.pcipos = (bus << 8) | dev | (func << 5);
53
54 if (ioctl(svgahelper_fd, SVGALIB_HELPER_IOCGPCIINL, &p))
55 return -1;
56
57 return p.val;
58 }
59
60 static int pci_get_vendor(
61 unsigned char bus,
62 unsigned char dev,
63 int func)
64 {
65 return pci_config_read_long(bus, dev, func, 0);
66 }
67 #endif
68
69 static __inline__ int enable_os_io(void)
70 {
71 #ifdef CONFIG_SVGAHELPER
72 svgahelper_fd = open(DEV_SVGA, O_RDWR);
73 if (svgahelper_fd > 0)
74 {
75 svgahelper_initialized = 1;
76 return(0);
77 }
78 svgahelper_initialized = -1;
79 #endif
80
81 #ifdef CONFIG_DHAHELPER
82 dhahelper_fd = open("/dev/dhahelper", O_RDWR);
83 if (dhahelper_fd > 0)
84 {
85 dhahelper_initialized = 1;
86 return(0);
87 }
88 dhahelper_initialized = -1;
89 #endif
90
91 #if defined(__powerpc__) && defined(__linux__)
92 /* should be fixed? */
93 #else
94 if (iopl(3) != 0)
95 return(errno);
96 #endif
97 return(0);
98 }
99
100 static __inline__ int disable_os_io(void)
101 {
102 #ifdef CONFIG_SVGAHELPER
103 if (svgahelper_initialized == 1)
104 close(svgahelper_fd);
105 else
106 #endif
107 #ifdef CONFIG_DHAHELPER
108 if (dhahelper_initialized == 1)
109 close(dhahelper_fd);
110 else
111 #endif
112 #if defined(__powerpc__) && defined(__linux__)
113 /* should be fixed? */
114 #else
115 if (iopl(0) != 0)
116 return(errno);
117 #endif
118 return(0);
119 }
120
121 #if (defined(__powerpc__) || defined(__sparc__) || defined(__sparc64__) \
122 || defined(__x86_64__)) && defined(__linux__) && !defined(CONFIG_SVGAHELPER)
123 #define CONFIG_PCI_LINUX_PROC
124 #endif
125
126 #if defined(CONFIG_PCI_LINUX_PROC)
127 static int pci_config_type( void ) { return 1; }
128
129 /* pci operations for (powerpc) Linux
130 questions, suggestions etc:
131 mplayer-dev-eng@mplayerhq.hu, colin@colino.net*/
132 #include <fcntl.h>
133 //#include <sys/io.h>
134 #include <linux/pci.h>
135 #include "libavutil/common.h"
136 #include "mpbswap.h"
137
138 static int pci_get_vendor(
139 unsigned char bus,
140 unsigned char dev,
141 int func)
142 {
143 int retval;
144 char path[100];
145 int fd;
146 short vendor, device;
147 sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev);
148 fd = open(path,O_RDONLY|O_SYNC);
149 if (fd == -1) {
150 retval=0xFFFF;
151 }
152 else if (pread(fd, &vendor, 2, PCI_VENDOR_ID) == 2 &&
153 pread(fd, &device, 2, PCI_DEVICE_ID) == 2) {
154 vendor = le2me_16(vendor);
155 device = le2me_16(device);
156 retval = vendor + (device<<16); /*no worries about byte order,
157 all ppc are bigendian*/
158 } else {
159 retval = 0xFFFF;
160 }
161 if (fd > 0) {
162 close(fd);
163 }
164 return retval;
165 }
166
167 static long pci_config_read_long(
168 unsigned char bus,
169 unsigned char dev,
170 int func,
171 unsigned cmd)
172 {
173 long retval;
174 char path[100];
175 int fd;
176 sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev);
177 fd = open(path,O_RDONLY|O_SYNC);
178 if (fd == -1) {
179 retval=0;
180 }
181 else if (pread(fd, &retval, 4, cmd) == 4) {
182 retval = le2me_32(retval);
183 } else {
184 retval = 0;
185 }
186 if (fd > 0) {
187 close(fd);
188 }
189 return retval;
190 }
191 #endif