common_init.c revision 6a94483f
1/* 2 * (C) Copyright IBM Corporation 2006 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * on the rights to use, copy, modify, merge, publish, distribute, sub 9 * license, and/or sell copies of the Software, and to permit persons to whom 10 * the Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 19 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 * DEALINGS IN THE SOFTWARE. 23 */ 24 25/** 26 * \file common_init.c 27 * Platform independent routines for initializing access to the PCI system. 28 * 29 * \author Ian Romanick <idr@us.ibm.com> 30 */ 31 32#include <stdlib.h> 33#include <errno.h> 34 35#include "pciaccess.h" 36#include "pciaccess_private.h" 37 38_pci_hidden struct pci_system * pci_sys; 39 40/** 41 * Initialize the PCI subsystem for access. 42 * 43 * \return 44 * Zero on success or an errno value on failure. In particular, if no 45 * platform-specific initializers are available, \c ENOSYS will be returned. 46 * 47 * \sa pci_system_cleanup 48 */ 49 50int 51pci_system_init( void ) 52{ 53 int err = ENOSYS; 54 55#ifdef __linux__ 56 err = pci_system_linux_sysfs_create(); 57#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) 58 err = pci_system_freebsd_create(); 59#elif defined(__NetBSD__) 60 err = pci_system_netbsd_create(); 61#elif defined(__OpenBSD__) 62 err = pci_system_openbsd_create(); 63#elif defined(__sun) 64 err = pci_system_solx_devfs_create(); 65#elif defined(__GNU__) || defined(__CYGWIN__) 66 err = pci_system_x86_create(); 67#else 68# error "Unsupported OS" 69#endif 70 71 return err; 72} 73 74void 75pci_system_init_dev_mem(int fd) 76{ 77#ifdef __OpenBSD__ 78 pci_system_openbsd_init_dev_mem(fd); 79#endif 80} 81 82/** 83 * Shutdown all access to the PCI subsystem. 84 * 85 * \sa pci_system_init 86 */ 87void 88pci_system_cleanup( void ) 89{ 90 unsigned i; 91 unsigned j; 92 93 94 if ( pci_sys == NULL ) { 95 return; 96 } 97 98 pci_io_cleanup(); 99 100 if ( pci_sys->devices ) { 101 for ( i = 0 ; i < pci_sys->num_devices ; i++ ) { 102 for ( j = 0 ; j < 6 ; j++ ) { 103 (void) pci_device_unmap_region( & pci_sys->devices[i].base, j ); 104 } 105 106 free( (char *) pci_sys->devices[i].device_string ); 107 free( (char *) pci_sys->devices[i].agp ); 108 109 pci_sys->devices[i].device_string = NULL; 110 pci_sys->devices[i].agp = NULL; 111 112 if ( pci_sys->methods->destroy_device != NULL ) { 113 (*pci_sys->methods->destroy_device)( & pci_sys->devices[i].base ); 114 } 115 } 116 117 free( pci_sys->devices ); 118 pci_sys->devices = NULL; 119 pci_sys->num_devices = 0; 120 } 121 122 if ( pci_sys->methods->destroy != NULL ) { 123 (*pci_sys->methods->destroy)(); 124 } 125 126 free( pci_sys ); 127 pci_sys = NULL; 128} 129