common_init.c 3.43 KB
Newer Older
Ian Romanick's avatar
Ian Romanick committed
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
/*
 * (C) Copyright IBM Corporation 2006
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/**
 * \file common_init.c
 * Platform independent routines for initializing access to the PCI system.
 *
 * \author Ian Romanick <idr@us.ibm.com>
 */
31
32
33
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Ian Romanick's avatar
Ian Romanick committed
34
35
36
37
38
39
40

#include <stdlib.h>
#include <errno.h>

#include "pciaccess.h"
#include "pciaccess_private.h"

Julien Cristau's avatar
Julien Cristau committed
41
_pci_hidden struct pci_system * pci_sys;
Ian Romanick's avatar
Ian Romanick committed
42
43
44

/**
 * Initialize the PCI subsystem for access.
Alan Coopersmith's avatar
Alan Coopersmith committed
45
 *
Ian Romanick's avatar
Ian Romanick committed
46
47
48
49
50
51
52
53
54
55
56
 * \return
 * Zero on success or an errno value on failure.  In particular, if no
 * platform-specific initializers are available, \c ENOSYS will be returned.
 *
 * \sa pci_system_cleanup
 */

int
pci_system_init( void )
{
    int err = ENOSYS;
Alan Coopersmith's avatar
Alan Coopersmith committed
57

58
#ifdef __linux__
Ian Romanick's avatar
Ian Romanick committed
59
    err = pci_system_linux_sysfs_create();
Petr Salinger's avatar
Petr Salinger committed
60
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
61
    err = pci_system_freebsd_create();
Juan RP's avatar
Juan RP committed
62
63
#elif defined(__NetBSD__)
    err = pci_system_netbsd_create();
64
65
#elif defined(__OpenBSD__)
    err = pci_system_openbsd_create();
66
#elif defined(__sun)
67
    err = pci_system_solx_devfs_create();
68
#elif defined(__GNU__) || defined(__CYGWIN__)
Samuel Thibault's avatar
Samuel Thibault committed
69
    err = pci_system_x86_create();
70
71
#else
# error "Unsupported OS"
Ian Romanick's avatar
Ian Romanick committed
72
73
74
75
76
#endif

    return err;
}

77
78
79
void
pci_system_init_dev_mem(int fd)
{
80
81
82
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
    pci_system_freebsd_init_dev_mem(fd);
#elif defined(__OpenBSD__)
83
84
85
    pci_system_openbsd_init_dev_mem(fd);
#endif
}
Ian Romanick's avatar
Ian Romanick committed
86
87
88

/**
 * Shutdown all access to the PCI subsystem.
Alan Coopersmith's avatar
Alan Coopersmith committed
89
 *
Ian Romanick's avatar
Ian Romanick committed
90
91
92
93
94
95
96
97
98
99
100
101
102
 * \sa pci_system_init
 */
void
pci_system_cleanup( void )
{
    unsigned i;
    unsigned j;


    if ( pci_sys == NULL ) {
	return;
    }

Adam Jackson's avatar
Adam Jackson committed
103
    pci_io_cleanup();
Ian Romanick's avatar
Ian Romanick committed
104
105
106
107
108
109
110
111
112

    if ( pci_sys->devices ) {
	for ( i = 0 ; i < pci_sys->num_devices ; i++ ) {
	    for ( j = 0 ; j < 6 ; j++ ) {
		(void) pci_device_unmap_region( & pci_sys->devices[i].base, j );
	    }

	    free( (char *) pci_sys->devices[i].device_string );
	    free( (char *) pci_sys->devices[i].agp );
Alan Coopersmith's avatar
Alan Coopersmith committed
113

Ian Romanick's avatar
Ian Romanick committed
114
115
116
117
118
119
120
	    pci_sys->devices[i].device_string = NULL;
	    pci_sys->devices[i].agp = NULL;

	    if ( pci_sys->methods->destroy_device != NULL ) {
		(*pci_sys->methods->destroy_device)( & pci_sys->devices[i].base );
	    }
	}
Alan Coopersmith's avatar
Alan Coopersmith committed
121

Ian Romanick's avatar
Ian Romanick committed
122
123
124
125
126
127
128
129
	free( pci_sys->devices );
	pci_sys->devices = NULL;
	pci_sys->num_devices = 0;
    }

    if ( pci_sys->methods->destroy != NULL ) {
	(*pci_sys->methods->destroy)();
    }
Alan Coopersmith's avatar
Alan Coopersmith committed
130

Ian Romanick's avatar
Ian Romanick committed
131
132
133
    free( pci_sys );
    pci_sys = NULL;
}