common_init.c revision 49310723
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(); 686a94483fSmrg#elif defined(__GNU__) || defined(__CYGWIN__) 6928d65773Smrg err = pci_system_x86_create(); 706a94483fSmrg#else 716a94483fSmrg# error "Unsupported OS" 724f5e7dd7Smrg#endif 734f5e7dd7Smrg 744f5e7dd7Smrg return err; 754f5e7dd7Smrg} 764f5e7dd7Smrg 774f5e7dd7Smrgvoid 784f5e7dd7Smrgpci_system_init_dev_mem(int fd) 794f5e7dd7Smrg{ 804f5e7dd7Smrg#ifdef __OpenBSD__ 814f5e7dd7Smrg pci_system_openbsd_init_dev_mem(fd); 824f5e7dd7Smrg#endif 834f5e7dd7Smrg} 844f5e7dd7Smrg 854f5e7dd7Smrg/** 864f5e7dd7Smrg * Shutdown all access to the PCI subsystem. 87cad31331Smrg * 884f5e7dd7Smrg * \sa pci_system_init 894f5e7dd7Smrg */ 904f5e7dd7Smrgvoid 914f5e7dd7Smrgpci_system_cleanup( void ) 924f5e7dd7Smrg{ 934f5e7dd7Smrg unsigned i; 944f5e7dd7Smrg unsigned j; 954f5e7dd7Smrg 964f5e7dd7Smrg 974f5e7dd7Smrg if ( pci_sys == NULL ) { 984f5e7dd7Smrg return; 994f5e7dd7Smrg } 1004f5e7dd7Smrg 101e432255dSmrg pci_io_cleanup(); 1024f5e7dd7Smrg 1034f5e7dd7Smrg if ( pci_sys->devices ) { 1044f5e7dd7Smrg for ( i = 0 ; i < pci_sys->num_devices ; i++ ) { 1054f5e7dd7Smrg for ( j = 0 ; j < 6 ; j++ ) { 1064f5e7dd7Smrg (void) pci_device_unmap_region( & pci_sys->devices[i].base, j ); 1074f5e7dd7Smrg } 1084f5e7dd7Smrg 1094f5e7dd7Smrg free( (char *) pci_sys->devices[i].device_string ); 1104f5e7dd7Smrg free( (char *) pci_sys->devices[i].agp ); 111cad31331Smrg 1124f5e7dd7Smrg pci_sys->devices[i].device_string = NULL; 1134f5e7dd7Smrg pci_sys->devices[i].agp = NULL; 1144f5e7dd7Smrg 1154f5e7dd7Smrg if ( pci_sys->methods->destroy_device != NULL ) { 1164f5e7dd7Smrg (*pci_sys->methods->destroy_device)( & pci_sys->devices[i].base ); 1174f5e7dd7Smrg } 1184f5e7dd7Smrg } 119cad31331Smrg 1204f5e7dd7Smrg free( pci_sys->devices ); 1214f5e7dd7Smrg pci_sys->devices = NULL; 1224f5e7dd7Smrg pci_sys->num_devices = 0; 1234f5e7dd7Smrg } 1244f5e7dd7Smrg 1254f5e7dd7Smrg if ( pci_sys->methods->destroy != NULL ) { 1264f5e7dd7Smrg (*pci_sys->methods->destroy)(); 1274f5e7dd7Smrg } 128cad31331Smrg 1294f5e7dd7Smrg free( pci_sys ); 1304f5e7dd7Smrg pci_sys = NULL; 1314f5e7dd7Smrg} 132