common_init.c revision cad31331
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 */ 314f5e7dd7Smrg 324f5e7dd7Smrg#include <stdlib.h> 334f5e7dd7Smrg#include <errno.h> 344f5e7dd7Smrg 354f5e7dd7Smrg#include "pciaccess.h" 364f5e7dd7Smrg#include "pciaccess_private.h" 374f5e7dd7Smrg 384f5e7dd7Smrg_pci_hidden struct pci_system * pci_sys; 394f5e7dd7Smrg 404f5e7dd7Smrg/** 414f5e7dd7Smrg * Initialize the PCI subsystem for access. 42cad31331Smrg * 434f5e7dd7Smrg * \return 444f5e7dd7Smrg * Zero on success or an errno value on failure. In particular, if no 454f5e7dd7Smrg * platform-specific initializers are available, \c ENOSYS will be returned. 464f5e7dd7Smrg * 474f5e7dd7Smrg * \sa pci_system_cleanup 484f5e7dd7Smrg */ 494f5e7dd7Smrg 504f5e7dd7Smrgint 514f5e7dd7Smrgpci_system_init( void ) 524f5e7dd7Smrg{ 534f5e7dd7Smrg int err = ENOSYS; 54cad31331Smrg 554f5e7dd7Smrg#ifdef linux 564f5e7dd7Smrg err = pci_system_linux_sysfs_create(); 574f5e7dd7Smrg#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) 584f5e7dd7Smrg err = pci_system_freebsd_create(); 594f5e7dd7Smrg#elif defined(__NetBSD__) 604f5e7dd7Smrg err = pci_system_netbsd_create(); 614f5e7dd7Smrg#elif defined(__OpenBSD__) 624f5e7dd7Smrg err = pci_system_openbsd_create(); 634f5e7dd7Smrg#elif defined(__sun) 644f5e7dd7Smrg err = pci_system_solx_devfs_create(); 6528d65773Smrg#elif defined(__GNU__) 6628d65773Smrg err = pci_system_x86_create(); 674f5e7dd7Smrg#endif 684f5e7dd7Smrg 694f5e7dd7Smrg return err; 704f5e7dd7Smrg} 714f5e7dd7Smrg 724f5e7dd7Smrgvoid 734f5e7dd7Smrgpci_system_init_dev_mem(int fd) 744f5e7dd7Smrg{ 754f5e7dd7Smrg#ifdef __OpenBSD__ 764f5e7dd7Smrg pci_system_openbsd_init_dev_mem(fd); 774f5e7dd7Smrg#endif 784f5e7dd7Smrg} 794f5e7dd7Smrg 804f5e7dd7Smrg/** 814f5e7dd7Smrg * Shutdown all access to the PCI subsystem. 82cad31331Smrg * 834f5e7dd7Smrg * \sa pci_system_init 844f5e7dd7Smrg */ 854f5e7dd7Smrgvoid 864f5e7dd7Smrgpci_system_cleanup( void ) 874f5e7dd7Smrg{ 884f5e7dd7Smrg unsigned i; 894f5e7dd7Smrg unsigned j; 904f5e7dd7Smrg 914f5e7dd7Smrg 924f5e7dd7Smrg if ( pci_sys == NULL ) { 934f5e7dd7Smrg return; 944f5e7dd7Smrg } 954f5e7dd7Smrg 96e432255dSmrg pci_io_cleanup(); 974f5e7dd7Smrg 984f5e7dd7Smrg if ( pci_sys->devices ) { 994f5e7dd7Smrg for ( i = 0 ; i < pci_sys->num_devices ; i++ ) { 1004f5e7dd7Smrg for ( j = 0 ; j < 6 ; j++ ) { 1014f5e7dd7Smrg (void) pci_device_unmap_region( & pci_sys->devices[i].base, j ); 1024f5e7dd7Smrg } 1034f5e7dd7Smrg 1044f5e7dd7Smrg free( (char *) pci_sys->devices[i].device_string ); 1054f5e7dd7Smrg free( (char *) pci_sys->devices[i].agp ); 106cad31331Smrg 1074f5e7dd7Smrg pci_sys->devices[i].device_string = NULL; 1084f5e7dd7Smrg pci_sys->devices[i].agp = NULL; 1094f5e7dd7Smrg 1104f5e7dd7Smrg if ( pci_sys->methods->destroy_device != NULL ) { 1114f5e7dd7Smrg (*pci_sys->methods->destroy_device)( & pci_sys->devices[i].base ); 1124f5e7dd7Smrg } 1134f5e7dd7Smrg } 114cad31331Smrg 1154f5e7dd7Smrg free( pci_sys->devices ); 1164f5e7dd7Smrg pci_sys->devices = NULL; 1174f5e7dd7Smrg pci_sys->num_devices = 0; 1184f5e7dd7Smrg } 1194f5e7dd7Smrg 1204f5e7dd7Smrg 1214f5e7dd7Smrg if ( pci_sys->methods->destroy != NULL ) { 1224f5e7dd7Smrg (*pci_sys->methods->destroy)(); 1234f5e7dd7Smrg } 124cad31331Smrg 1254f5e7dd7Smrg free( pci_sys ); 1264f5e7dd7Smrg pci_sys = NULL; 1274f5e7dd7Smrg} 128