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