14f5e7dd7Smrg/*
24f5e7dd7Smrg * (C) Copyright IBM Corporation 2006
34f5e7dd7Smrg * All Rights Reserved.
44f5e7dd7Smrg *
54f5e7dd7Smrg * Permission is hereby granted, free of charge, to any person obtaining a
64f5e7dd7Smrg * copy of this software and associated documentation files (the "Software"),
74f5e7dd7Smrg * to deal in the Software without restriction, including without limitation
84f5e7dd7Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
94f5e7dd7Smrg * license, and/or sell copies of the Software, and to permit persons to whom
104f5e7dd7Smrg * the Software is furnished to do so, subject to the following conditions:
114f5e7dd7Smrg *
124f5e7dd7Smrg * The above copyright notice and this permission notice (including the next
134f5e7dd7Smrg * paragraph) shall be included in all copies or substantial portions of the
144f5e7dd7Smrg * Software.
154f5e7dd7Smrg *
164f5e7dd7Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
174f5e7dd7Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
184f5e7dd7Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
194f5e7dd7Smrg * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
204f5e7dd7Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
214f5e7dd7Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
224f5e7dd7Smrg * DEALINGS IN THE SOFTWARE.
234f5e7dd7Smrg */
244f5e7dd7Smrg
254f5e7dd7Smrg/**
264f5e7dd7Smrg * \file common_init.c
274f5e7dd7Smrg * Platform independent routines for initializing access to the PCI system.
284f5e7dd7Smrg *
294f5e7dd7Smrg * \author Ian Romanick <idr@us.ibm.com>
304f5e7dd7Smrg */
3149310723Smrg#ifdef HAVE_CONFIG_H
3249310723Smrg#include "config.h"
3349310723Smrg#endif
344f5e7dd7Smrg
354f5e7dd7Smrg#include <stdlib.h>
364f5e7dd7Smrg#include <errno.h>
374f5e7dd7Smrg
384f5e7dd7Smrg#include "pciaccess.h"
394f5e7dd7Smrg#include "pciaccess_private.h"
404f5e7dd7Smrg
414f5e7dd7Smrg_pci_hidden struct pci_system * pci_sys;
424f5e7dd7Smrg
434f5e7dd7Smrg/**
444f5e7dd7Smrg * Initialize the PCI subsystem for access.
45cad31331Smrg *
464f5e7dd7Smrg * \return
474f5e7dd7Smrg * Zero on success or an errno value on failure.  In particular, if no
484f5e7dd7Smrg * platform-specific initializers are available, \c ENOSYS will be returned.
494f5e7dd7Smrg *
504f5e7dd7Smrg * \sa pci_system_cleanup
514f5e7dd7Smrg */
524f5e7dd7Smrg
534f5e7dd7Smrgint
544f5e7dd7Smrgpci_system_init( void )
554f5e7dd7Smrg{
564f5e7dd7Smrg    int err = ENOSYS;
57cad31331Smrg
586a94483fSmrg#ifdef __linux__
594f5e7dd7Smrg    err = pci_system_linux_sysfs_create();
604f5e7dd7Smrg#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
614f5e7dd7Smrg    err = pci_system_freebsd_create();
624f5e7dd7Smrg#elif defined(__NetBSD__)
634f5e7dd7Smrg    err = pci_system_netbsd_create();
644f5e7dd7Smrg#elif defined(__OpenBSD__)
654f5e7dd7Smrg    err = pci_system_openbsd_create();
664f5e7dd7Smrg#elif defined(__sun)
674f5e7dd7Smrg    err = pci_system_solx_devfs_create();
682029f493Smrg#elif defined(__GNU__)
692029f493Smrg    err = pci_system_hurd_create();
702029f493Smrg#elif defined(__CYGWIN__)
7128d65773Smrg    err = pci_system_x86_create();
726a94483fSmrg#else
736a94483fSmrg# error "Unsupported OS"
744f5e7dd7Smrg#endif
754f5e7dd7Smrg
764f5e7dd7Smrg    return err;
774f5e7dd7Smrg}
784f5e7dd7Smrg
794f5e7dd7Smrgvoid
804f5e7dd7Smrgpci_system_init_dev_mem(int fd)
814f5e7dd7Smrg{
825ad99bdfSmrg#if defined(__OpenBSD__)
834f5e7dd7Smrg    pci_system_openbsd_init_dev_mem(fd);
844f5e7dd7Smrg#endif
854f5e7dd7Smrg}
864f5e7dd7Smrg
874f5e7dd7Smrg/**
884f5e7dd7Smrg * Shutdown all access to the PCI subsystem.
89cad31331Smrg *
904f5e7dd7Smrg * \sa pci_system_init
914f5e7dd7Smrg */
924f5e7dd7Smrgvoid
934f5e7dd7Smrgpci_system_cleanup( void )
944f5e7dd7Smrg{
954f5e7dd7Smrg    unsigned i;
964f5e7dd7Smrg    unsigned j;
974f5e7dd7Smrg
984f5e7dd7Smrg
994f5e7dd7Smrg    if ( pci_sys == NULL ) {
1004f5e7dd7Smrg	return;
1014f5e7dd7Smrg    }
1024f5e7dd7Smrg
103e432255dSmrg    pci_io_cleanup();
1044f5e7dd7Smrg
1054f5e7dd7Smrg    if ( pci_sys->devices ) {
1064f5e7dd7Smrg	for ( i = 0 ; i < pci_sys->num_devices ; i++ ) {
1074f5e7dd7Smrg	    for ( j = 0 ; j < 6 ; j++ ) {
1084f5e7dd7Smrg		(void) pci_device_unmap_region( & pci_sys->devices[i].base, j );
1094f5e7dd7Smrg	    }
1104f5e7dd7Smrg
1114f5e7dd7Smrg	    free( (char *) pci_sys->devices[i].device_string );
1124f5e7dd7Smrg	    free( (char *) pci_sys->devices[i].agp );
113cad31331Smrg
1144f5e7dd7Smrg	    pci_sys->devices[i].device_string = NULL;
1154f5e7dd7Smrg	    pci_sys->devices[i].agp = NULL;
1164f5e7dd7Smrg
1174f5e7dd7Smrg	    if ( pci_sys->methods->destroy_device != NULL ) {
1184f5e7dd7Smrg		(*pci_sys->methods->destroy_device)( & pci_sys->devices[i].base );
1194f5e7dd7Smrg	    }
1204f5e7dd7Smrg	}
121cad31331Smrg
1224f5e7dd7Smrg	free( pci_sys->devices );
1234f5e7dd7Smrg	pci_sys->devices = NULL;
1244f5e7dd7Smrg	pci_sys->num_devices = 0;
1254f5e7dd7Smrg    }
1264f5e7dd7Smrg
1274f5e7dd7Smrg    if ( pci_sys->methods->destroy != NULL ) {
1284f5e7dd7Smrg	(*pci_sys->methods->destroy)();
1294f5e7dd7Smrg    }
130cad31331Smrg
1314f5e7dd7Smrg    free( pci_sys );
1324f5e7dd7Smrg    pci_sys = NULL;
1334f5e7dd7Smrg}
134