common_init.c revision 4f5e7dd7
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. 424f5e7dd7Smrg * 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; 544f5e7dd7Smrg 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(); 654f5e7dd7Smrg#endif 664f5e7dd7Smrg 674f5e7dd7Smrg return err; 684f5e7dd7Smrg} 694f5e7dd7Smrg 704f5e7dd7Smrgvoid 714f5e7dd7Smrgpci_system_init_dev_mem(int fd) 724f5e7dd7Smrg{ 734f5e7dd7Smrg#ifdef __OpenBSD__ 744f5e7dd7Smrg pci_system_openbsd_init_dev_mem(fd); 754f5e7dd7Smrg#endif 764f5e7dd7Smrg} 774f5e7dd7Smrg 784f5e7dd7Smrg/** 794f5e7dd7Smrg * Shutdown all access to the PCI subsystem. 804f5e7dd7Smrg * 814f5e7dd7Smrg * \sa pci_system_init 824f5e7dd7Smrg */ 834f5e7dd7Smrgvoid 844f5e7dd7Smrgpci_system_cleanup( void ) 854f5e7dd7Smrg{ 864f5e7dd7Smrg unsigned i; 874f5e7dd7Smrg unsigned j; 884f5e7dd7Smrg 894f5e7dd7Smrg 904f5e7dd7Smrg if ( pci_sys == NULL ) { 914f5e7dd7Smrg return; 924f5e7dd7Smrg } 934f5e7dd7Smrg 944f5e7dd7Smrg 954f5e7dd7Smrg if ( pci_sys->devices ) { 964f5e7dd7Smrg for ( i = 0 ; i < pci_sys->num_devices ; i++ ) { 974f5e7dd7Smrg for ( j = 0 ; j < 6 ; j++ ) { 984f5e7dd7Smrg (void) pci_device_unmap_region( & pci_sys->devices[i].base, j ); 994f5e7dd7Smrg } 1004f5e7dd7Smrg 1014f5e7dd7Smrg free( (char *) pci_sys->devices[i].device_string ); 1024f5e7dd7Smrg free( (char *) pci_sys->devices[i].agp ); 1034f5e7dd7Smrg 1044f5e7dd7Smrg pci_sys->devices[i].device_string = NULL; 1054f5e7dd7Smrg pci_sys->devices[i].agp = NULL; 1064f5e7dd7Smrg 1074f5e7dd7Smrg if ( pci_sys->methods->destroy_device != NULL ) { 1084f5e7dd7Smrg (*pci_sys->methods->destroy_device)( & pci_sys->devices[i].base ); 1094f5e7dd7Smrg } 1104f5e7dd7Smrg } 1114f5e7dd7Smrg 1124f5e7dd7Smrg free( pci_sys->devices ); 1134f5e7dd7Smrg pci_sys->devices = NULL; 1144f5e7dd7Smrg pci_sys->num_devices = 0; 1154f5e7dd7Smrg } 1164f5e7dd7Smrg 1174f5e7dd7Smrg 1184f5e7dd7Smrg if ( pci_sys->methods->destroy != NULL ) { 1194f5e7dd7Smrg (*pci_sys->methods->destroy)(); 1204f5e7dd7Smrg } 1214f5e7dd7Smrg 1224f5e7dd7Smrg free( pci_sys ); 1234f5e7dd7Smrg pci_sys = NULL; 1244f5e7dd7Smrg} 125