15a04522aSIan Romanick /* 25a04522aSIan Romanick * (C) Copyright IBM Corporation 2006 35a04522aSIan Romanick * All Rights Reserved. 45a04522aSIan Romanick * 55a04522aSIan Romanick * Permission is hereby granted, free of charge, to any person obtaining a 65a04522aSIan Romanick * copy of this software and associated documentation files (the "Software"), 75a04522aSIan Romanick * to deal in the Software without restriction, including without limitation 85a04522aSIan Romanick * on the rights to use, copy, modify, merge, publish, distribute, sub 95a04522aSIan Romanick * license, and/or sell copies of the Software, and to permit persons to whom 105a04522aSIan Romanick * the Software is furnished to do so, subject to the following conditions: 115a04522aSIan Romanick * 125a04522aSIan Romanick * The above copyright notice and this permission notice (including the next 135a04522aSIan Romanick * paragraph) shall be included in all copies or substantial portions of the 145a04522aSIan Romanick * Software. 155a04522aSIan Romanick * 165a04522aSIan Romanick * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 175a04522aSIan Romanick * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 185a04522aSIan Romanick * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 195a04522aSIan Romanick * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 205a04522aSIan Romanick * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 215a04522aSIan Romanick * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 225a04522aSIan Romanick * DEALINGS IN THE SOFTWARE. 235a04522aSIan Romanick */ 245a04522aSIan Romanick 255a04522aSIan Romanick /** 265a04522aSIan Romanick * \file common_init.c 275a04522aSIan Romanick * Platform independent routines for initializing access to the PCI system. 285a04522aSIan Romanick * 295a04522aSIan Romanick * \author Ian Romanick <[email protected]> 305a04522aSIan Romanick */ 315a04522aSIan Romanick 325a04522aSIan Romanick #include <stdlib.h> 335a04522aSIan Romanick #include <errno.h> 345a04522aSIan Romanick 355a04522aSIan Romanick #include "pciaccess.h" 365a04522aSIan Romanick #include "pciaccess_private.h" 375a04522aSIan Romanick 38adc46f65SJulien Cristau _pci_hidden struct pci_system * pci_sys; 395a04522aSIan Romanick 405a04522aSIan Romanick /** 415a04522aSIan Romanick * Initialize the PCI subsystem for access. 425a04522aSIan Romanick * 435a04522aSIan Romanick * \return 445a04522aSIan Romanick * Zero on success or an errno value on failure. In particular, if no 455a04522aSIan Romanick * platform-specific initializers are available, \c ENOSYS will be returned. 465a04522aSIan Romanick * 475a04522aSIan Romanick * \sa pci_system_cleanup 485a04522aSIan Romanick */ 495a04522aSIan Romanick 505a04522aSIan Romanick int 515a04522aSIan Romanick pci_system_init( void ) 525a04522aSIan Romanick { 535a04522aSIan Romanick int err = ENOSYS; 545a04522aSIan Romanick 555a04522aSIan Romanick #ifdef linux 565a04522aSIan Romanick err = pci_system_linux_sysfs_create(); 576ae37861SPetr Salinger #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) 5807b09d93SEric Anholt err = pci_system_freebsd_create(); 594c1c607cSJuan RP #elif defined(__NetBSD__) 604c1c607cSJuan RP err = pci_system_netbsd_create(); 61d898072eSMark Kettenis #elif defined(__OpenBSD__) 62d898072eSMark Kettenis err = pci_system_openbsd_create(); 63206e2921Sedward shu #elif defined(__sun) 64206e2921Sedward shu err = pci_system_solx_devfs_create(); 655a04522aSIan Romanick #endif 665a04522aSIan Romanick 675a04522aSIan Romanick return err; 685a04522aSIan Romanick } 695a04522aSIan Romanick 70d898072eSMark Kettenis void 71d898072eSMark Kettenis pci_system_init_dev_mem(int fd) 72d898072eSMark Kettenis { 73d898072eSMark Kettenis #ifdef __OpenBSD__ 74d898072eSMark Kettenis pci_system_openbsd_init_dev_mem(fd); 75d898072eSMark Kettenis #endif 76d898072eSMark Kettenis } 775a04522aSIan Romanick 785a04522aSIan Romanick /** 795a04522aSIan Romanick * Shutdown all access to the PCI subsystem. 805a04522aSIan Romanick * 815a04522aSIan Romanick * \sa pci_system_init 825a04522aSIan Romanick */ 835a04522aSIan Romanick void 845a04522aSIan Romanick pci_system_cleanup( void ) 855a04522aSIan Romanick { 865a04522aSIan Romanick unsigned i; 875a04522aSIan Romanick unsigned j; 885a04522aSIan Romanick 895a04522aSIan Romanick 905a04522aSIan Romanick if ( pci_sys == NULL ) { 915a04522aSIan Romanick return; 925a04522aSIan Romanick } 935a04522aSIan Romanick 94*5e8d4c19SAdam Jackson pci_io_cleanup(); 955a04522aSIan Romanick 965a04522aSIan Romanick if ( pci_sys->devices ) { 975a04522aSIan Romanick for ( i = 0 ; i < pci_sys->num_devices ; i++ ) { 985a04522aSIan Romanick for ( j = 0 ; j < 6 ; j++ ) { 995a04522aSIan Romanick (void) pci_device_unmap_region( & pci_sys->devices[i].base, j ); 1005a04522aSIan Romanick } 1015a04522aSIan Romanick 1025a04522aSIan Romanick free( (char *) pci_sys->devices[i].device_string ); 1035a04522aSIan Romanick free( (char *) pci_sys->devices[i].agp ); 1045a04522aSIan Romanick 1055a04522aSIan Romanick pci_sys->devices[i].device_string = NULL; 1065a04522aSIan Romanick pci_sys->devices[i].agp = NULL; 1075a04522aSIan Romanick 1085a04522aSIan Romanick if ( pci_sys->methods->destroy_device != NULL ) { 1095a04522aSIan Romanick (*pci_sys->methods->destroy_device)( & pci_sys->devices[i].base ); 1105a04522aSIan Romanick } 1115a04522aSIan Romanick } 1125a04522aSIan Romanick 1135a04522aSIan Romanick free( pci_sys->devices ); 1145a04522aSIan Romanick pci_sys->devices = NULL; 1155a04522aSIan Romanick pci_sys->num_devices = 0; 1165a04522aSIan Romanick } 1175a04522aSIan Romanick 1185a04522aSIan Romanick 1195a04522aSIan Romanick if ( pci_sys->methods->destroy != NULL ) { 1205a04522aSIan Romanick (*pci_sys->methods->destroy)(); 1215a04522aSIan Romanick } 1225a04522aSIan Romanick 1235a04522aSIan Romanick free( pci_sys ); 1245a04522aSIan Romanick pci_sys = NULL; 1255a04522aSIan Romanick } 126