OSDN Git Service

f7b59bd3f046b640878951a27cda7933282e51b2
[android-x86/external-libpciaccess.git] / src / common_init.c
1 /*
2  * (C) Copyright IBM Corporation 2006
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 /**
26  * \file common_init.c
27  * Platform independent routines for initializing access to the PCI system.
28  *
29  * \author Ian Romanick <idr@us.ibm.com>
30  */
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <stdlib.h>
36 #include <errno.h>
37
38 #include "pciaccess.h"
39 #include "pciaccess_private.h"
40
41 _pci_hidden struct pci_system * pci_sys;
42
43 /**
44  * Initialize the PCI subsystem for access.
45  *
46  * \return
47  * Zero on success or an errno value on failure.  In particular, if no
48  * platform-specific initializers are available, \c ENOSYS will be returned.
49  *
50  * \sa pci_system_cleanup
51  */
52
53 int
54 pci_system_init( void )
55 {
56     int err = ENOSYS;
57
58 #ifdef __linux__
59     err = pci_system_linux_sysfs_create();
60 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
61     err = pci_system_freebsd_create();
62 #elif defined(__NetBSD__)
63     err = pci_system_netbsd_create();
64 #elif defined(__OpenBSD__)
65     err = pci_system_openbsd_create();
66 #elif defined(__sun)
67     err = pci_system_solx_devfs_create();
68 #elif defined(__GNU__) || defined(__CYGWIN__)
69     err = pci_system_x86_create();
70 #else
71 # error "Unsupported OS"
72 #endif
73
74     return err;
75 }
76
77 void
78 pci_system_init_dev_mem(int fd)
79 {
80 #ifdef __OpenBSD__
81     pci_system_openbsd_init_dev_mem(fd);
82 #endif
83 }
84
85 /**
86  * Shutdown all access to the PCI subsystem.
87  *
88  * \sa pci_system_init
89  */
90 void
91 pci_system_cleanup( void )
92 {
93     unsigned i;
94     unsigned j;
95
96
97     if ( pci_sys == NULL ) {
98         return;
99     }
100
101     pci_io_cleanup();
102
103     if ( pci_sys->devices ) {
104         for ( i = 0 ; i < pci_sys->num_devices ; i++ ) {
105             for ( j = 0 ; j < 6 ; j++ ) {
106                 (void) pci_device_unmap_region( & pci_sys->devices[i].base, j );
107             }
108
109             free( (char *) pci_sys->devices[i].device_string );
110             free( (char *) pci_sys->devices[i].agp );
111
112             pci_sys->devices[i].device_string = NULL;
113             pci_sys->devices[i].agp = NULL;
114
115             if ( pci_sys->methods->destroy_device != NULL ) {
116                 (*pci_sys->methods->destroy_device)( & pci_sys->devices[i].base );
117             }
118         }
119
120         free( pci_sys->devices );
121         pci_sys->devices = NULL;
122         pci_sys->num_devices = 0;
123     }
124
125     if ( pci_sys->methods->destroy != NULL ) {
126         (*pci_sys->methods->destroy)();
127     }
128
129     free( pci_sys );
130     pci_sys = NULL;
131 }