summaryrefslogtreecommitdiff
path: root/src/common_init.c
blob: 7c9db9cd02f6c95ccee91c33d7410e1e2a98342e (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
/*
 * (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>
 */

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

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

_pci_hidden struct pci_system * pci_sys;

/**
 * Initialize the PCI subsystem for access.
 *
 * \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;

#ifdef linux
    err = pci_system_linux_sysfs_create();
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
    err = pci_system_freebsd_create();
#elif defined(__NetBSD__)
    err = pci_system_netbsd_create();
#elif defined(__OpenBSD__)
    err = pci_system_openbsd_create();
#elif defined(__sun)
    err = pci_system_solx_devfs_create();
#elif defined(__GNU__)
    err = pci_system_x86_create();
#endif

    return err;
}

void
pci_system_init_dev_mem(int fd)
{
#ifdef __OpenBSD__
    pci_system_openbsd_init_dev_mem(fd);
#endif
}

/**
 * Shutdown all access to the PCI subsystem.
 *
 * \sa pci_system_init
 */
void
pci_system_cleanup( void )
{
    unsigned i;
    unsigned j;


    if ( pci_sys == NULL ) {
	return;
    }

    pci_io_cleanup();

    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 );

	    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 );
	    }
	}

	free( pci_sys->devices );
	pci_sys->devices = NULL;
	pci_sys->num_devices = 0;
    }

    if ( pci_sys->methods->destroy != NULL ) {
	(*pci_sys->methods->destroy)();
    }

    free( pci_sys );
    pci_sys = NULL;
}