Home | History | Annotate | Line # | Download | only in pci
pci_verbose.c revision 1.6
      1  1.6  pgoyette /*	$NetBSD: pci_verbose.c,v 1.6 2010/06/06 18:58:24 pgoyette Exp $	*/
      2  1.1  pgoyette 
      3  1.1  pgoyette /*
      4  1.1  pgoyette  * Copyright (c) 1997 Zubin D. Dittia.  All rights reserved.
      5  1.1  pgoyette  * Copyright (c) 1995, 1996, 1998, 2000
      6  1.1  pgoyette  *	Christopher G. Demetriou.  All rights reserved.
      7  1.1  pgoyette  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
      8  1.1  pgoyette  *
      9  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     10  1.1  pgoyette  * modification, are permitted provided that the following conditions
     11  1.1  pgoyette  * are met:
     12  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     13  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     14  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     17  1.1  pgoyette  * 3. All advertising materials mentioning features or use of this software
     18  1.1  pgoyette  *    must display the following acknowledgement:
     19  1.1  pgoyette  *	This product includes software developed by Charles M. Hannum.
     20  1.1  pgoyette  * 4. The name of the author may not be used to endorse or promote products
     21  1.1  pgoyette  *    derived from this software without specific prior written permission.
     22  1.1  pgoyette  *
     23  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  1.1  pgoyette  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  1.1  pgoyette  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  1.1  pgoyette  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  1.1  pgoyette  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  1.1  pgoyette  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  1.1  pgoyette  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  1.1  pgoyette  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  1.1  pgoyette  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  1.1  pgoyette  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.1  pgoyette  */
     34  1.1  pgoyette 
     35  1.1  pgoyette /*
     36  1.1  pgoyette  * PCI autoconfiguration support functions.
     37  1.1  pgoyette  *
     38  1.1  pgoyette  * Note: This file is also built into a userland library (libpci).
     39  1.1  pgoyette  * Pay attention to this when you make modifications.
     40  1.1  pgoyette  */
     41  1.1  pgoyette 
     42  1.1  pgoyette #include <sys/cdefs.h>
     43  1.6  pgoyette __KERNEL_RCSID(0, "$NetBSD: pci_verbose.c,v 1.6 2010/06/06 18:58:24 pgoyette Exp $");
     44  1.1  pgoyette 
     45  1.1  pgoyette #include <sys/param.h>
     46  1.1  pgoyette 
     47  1.1  pgoyette #ifdef _KERNEL
     48  1.1  pgoyette #include <sys/module.h>
     49  1.1  pgoyette #else
     50  1.1  pgoyette #include <pci.h>
     51  1.1  pgoyette #endif
     52  1.1  pgoyette 
     53  1.1  pgoyette #include <dev/pci/pcireg.h>
     54  1.3  pgoyette #include <dev/pci/pcidevs.h>
     55  1.1  pgoyette #ifdef _KERNEL
     56  1.3  pgoyette #include <dev/pci/pci_verbose.h>
     57  1.1  pgoyette #endif
     58  1.1  pgoyette 
     59  1.1  pgoyette /*
     60  1.1  pgoyette  * Descriptions of of known vendors and devices ("products").
     61  1.1  pgoyette  */
     62  1.1  pgoyette 
     63  1.1  pgoyette #include <dev/pci/pcidevs_data.h>
     64  1.1  pgoyette 
     65  1.1  pgoyette #ifndef _KERNEL
     66  1.1  pgoyette #include <string.h>
     67  1.1  pgoyette #endif
     68  1.1  pgoyette 
     69  1.1  pgoyette #ifdef _KERNEL
     70  1.1  pgoyette static int pciverbose_modcmd(modcmd_t, void *);
     71  1.1  pgoyette 
     72  1.1  pgoyette MODULE(MODULE_CLASS_MISC, pciverbose, NULL);
     73  1.1  pgoyette 
     74  1.1  pgoyette static int
     75  1.1  pgoyette pciverbose_modcmd(modcmd_t cmd, void *arg)
     76  1.1  pgoyette {
     77  1.6  pgoyette 	static const char *(*saved_findvendor)(pcireg_t);
     78  1.6  pgoyette 	static const char *(*saved_findproduct)(pcireg_t);
     79  1.6  pgoyette 	static const char *saved_unmatched;
     80  1.6  pgoyette 
     81  1.1  pgoyette 	switch (cmd) {
     82  1.1  pgoyette 	case MODULE_CMD_INIT:
     83  1.6  pgoyette 		saved_findvendor = pci_findvendor;
     84  1.6  pgoyette 		saved_findproduct = pci_findproduct;
     85  1.6  pgoyette 		saved_unmatched = pci_unmatched;
     86  1.2  pgoyette 		pci_findvendor = pci_findvendor_real;
     87  1.2  pgoyette 		pci_findproduct = pci_findproduct_real;
     88  1.1  pgoyette 		pci_unmatched = "unmatched ";
     89  1.1  pgoyette 		return 0;
     90  1.1  pgoyette 	case MODULE_CMD_FINI:
     91  1.6  pgoyette 		pci_findvendor = saved_findvendor;
     92  1.6  pgoyette 		pci_findproduct = saved_findproduct;
     93  1.6  pgoyette 		pci_unmatched = saved_unmatched;
     94  1.6  pgoyette 		pciverbose_loaded = 0;
     95  1.1  pgoyette 		return 0;
     96  1.1  pgoyette 	default:
     97  1.1  pgoyette 		return ENOTTY;
     98  1.1  pgoyette 	}
     99  1.1  pgoyette }
    100  1.1  pgoyette #endif /* KERNEL */
    101  1.1  pgoyette 
    102  1.1  pgoyette static const char *
    103  1.1  pgoyette pci_untokenstring(const uint16_t *token, char *buf, size_t len)
    104  1.1  pgoyette {
    105  1.1  pgoyette 	char *cp = buf;
    106  1.1  pgoyette 
    107  1.1  pgoyette 	buf[0] = '\0';
    108  1.1  pgoyette 	for (; *token != 0; token++) {
    109  1.1  pgoyette 		cp = buf + strlcat(buf, pci_words + *token, len - 2);
    110  1.1  pgoyette 		cp[0] = ' ';
    111  1.1  pgoyette 		cp[1] = '\0';
    112  1.1  pgoyette 	}
    113  1.1  pgoyette 	*cp = '\0';
    114  1.1  pgoyette 	return cp != buf ? buf : NULL;
    115  1.1  pgoyette }
    116  1.1  pgoyette 
    117  1.1  pgoyette const char *
    118  1.2  pgoyette pci_findvendor_real(pcireg_t id_reg)
    119  1.1  pgoyette {
    120  1.1  pgoyette 	static char buf[256];
    121  1.1  pgoyette 	pci_vendor_id_t vendor = PCI_VENDOR(id_reg);
    122  1.1  pgoyette 	size_t n;
    123  1.1  pgoyette 
    124  1.1  pgoyette 	for (n = 0; n < __arraycount(pci_vendors); n++) {
    125  1.1  pgoyette 		if (pci_vendors[n] == vendor)
    126  1.1  pgoyette 			return pci_untokenstring(&pci_vendors[n+1], buf,
    127  1.1  pgoyette 			    sizeof(buf));
    128  1.1  pgoyette 
    129  1.1  pgoyette 		/* Skip Tokens */
    130  1.1  pgoyette 		n++;
    131  1.1  pgoyette 		while (pci_vendors[n] != 0 && n < __arraycount(pci_vendors))
    132  1.1  pgoyette 			n++;
    133  1.1  pgoyette 	}
    134  1.1  pgoyette 	return (NULL);
    135  1.1  pgoyette }
    136  1.1  pgoyette 
    137  1.1  pgoyette const char *
    138  1.2  pgoyette pci_findproduct_real(pcireg_t id_reg)
    139  1.1  pgoyette {
    140  1.1  pgoyette 	static char buf[256];
    141  1.1  pgoyette 	pci_vendor_id_t vendor = PCI_VENDOR(id_reg);
    142  1.1  pgoyette 	pci_product_id_t product = PCI_PRODUCT(id_reg);
    143  1.1  pgoyette 	size_t n;
    144  1.1  pgoyette 
    145  1.1  pgoyette 	for (n = 0; n < __arraycount(pci_products); n++) {
    146  1.1  pgoyette 		if (pci_products[n] == vendor && pci_products[n+1] == product)
    147  1.1  pgoyette 			return pci_untokenstring(&pci_products[n+2], buf,
    148  1.1  pgoyette 			    sizeof(buf));
    149  1.1  pgoyette 
    150  1.1  pgoyette 		/* Skip Tokens */
    151  1.1  pgoyette 		n += 2;
    152  1.1  pgoyette 		while (pci_products[n] != 0 && n < __arraycount(pci_products))
    153  1.1  pgoyette 			n++;
    154  1.1  pgoyette 	}
    155  1.1  pgoyette 	return (NULL);
    156  1.1  pgoyette }
    157  1.1  pgoyette 
    158