Home | History | Annotate | Line # | Download | only in pci
pci_verbose.c revision 1.2
      1  1.2  pgoyette /*	$NetBSD: pci_verbose.c,v 1.2 2010/05/25 08:35:45 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.2  pgoyette __KERNEL_RCSID(0, "$NetBSD: pci_verbose.c,v 1.2 2010/05/25 08:35:45 pgoyette Exp $");
     44  1.1  pgoyette 
     45  1.1  pgoyette #ifdef _KERNEL_OPT
     46  1.1  pgoyette #include "opt_pci.h"
     47  1.1  pgoyette #endif
     48  1.1  pgoyette 
     49  1.1  pgoyette #include <sys/param.h>
     50  1.1  pgoyette 
     51  1.1  pgoyette #ifdef _KERNEL
     52  1.1  pgoyette #include <sys/module.h>
     53  1.1  pgoyette #else
     54  1.1  pgoyette #include <pci.h>
     55  1.1  pgoyette #endif
     56  1.1  pgoyette 
     57  1.1  pgoyette #include <dev/pci/pcireg.h>
     58  1.1  pgoyette #ifdef _KERNEL
     59  1.1  pgoyette #include <dev/pci/pcivar.h>
     60  1.1  pgoyette #endif
     61  1.1  pgoyette 
     62  1.1  pgoyette #include <dev/pci/pcidevs.h>
     63  1.1  pgoyette 
     64  1.1  pgoyette /*
     65  1.1  pgoyette  * Descriptions of of known vendors and devices ("products").
     66  1.1  pgoyette  */
     67  1.1  pgoyette 
     68  1.1  pgoyette #include <dev/pci/pcidevs_data.h>
     69  1.1  pgoyette 
     70  1.1  pgoyette #ifndef _KERNEL
     71  1.1  pgoyette #include <string.h>
     72  1.1  pgoyette #endif
     73  1.1  pgoyette 
     74  1.1  pgoyette #ifdef _KERNEL
     75  1.1  pgoyette static int pciverbose_modcmd(modcmd_t, void *);
     76  1.1  pgoyette 
     77  1.1  pgoyette MODULE(MODULE_CLASS_MISC, pciverbose, NULL);
     78  1.1  pgoyette 
     79  1.1  pgoyette static int
     80  1.1  pgoyette pciverbose_modcmd(modcmd_t cmd, void *arg)
     81  1.1  pgoyette {
     82  1.1  pgoyette 	aprint_normal("%s: cmd %d\n", __func__, cmd);	/* XXX */
     83  1.1  pgoyette 	switch (cmd) {
     84  1.1  pgoyette 	case MODULE_CMD_INIT:
     85  1.2  pgoyette 		pci_findvendor = pci_findvendor_real;
     86  1.2  pgoyette 		pci_findproduct = pci_findproduct_real;
     87  1.1  pgoyette 		pci_unmatched = "unmatched ";
     88  1.1  pgoyette 		return 0;
     89  1.1  pgoyette 	case MODULE_CMD_FINI:
     90  1.2  pgoyette 		pci_findvendor = pci_null;
     91  1.2  pgoyette 		pci_findproduct = pci_null;
     92  1.1  pgoyette 		pci_unmatched = "";
     93  1.1  pgoyette 		return 0;
     94  1.1  pgoyette 	default:
     95  1.1  pgoyette 		return ENOTTY;
     96  1.1  pgoyette 	}
     97  1.1  pgoyette }
     98  1.1  pgoyette #endif /* KERNEL */
     99  1.1  pgoyette 
    100  1.1  pgoyette static const char *
    101  1.1  pgoyette pci_untokenstring(const uint16_t *token, char *buf, size_t len)
    102  1.1  pgoyette {
    103  1.1  pgoyette 	char *cp = buf;
    104  1.1  pgoyette 
    105  1.1  pgoyette 	buf[0] = '\0';
    106  1.1  pgoyette 	for (; *token != 0; token++) {
    107  1.1  pgoyette 		cp = buf + strlcat(buf, pci_words + *token, len - 2);
    108  1.1  pgoyette 		cp[0] = ' ';
    109  1.1  pgoyette 		cp[1] = '\0';
    110  1.1  pgoyette 	}
    111  1.1  pgoyette 	*cp = '\0';
    112  1.1  pgoyette 	return cp != buf ? buf : NULL;
    113  1.1  pgoyette }
    114  1.1  pgoyette 
    115  1.1  pgoyette const char *
    116  1.2  pgoyette pci_findvendor_real(pcireg_t id_reg)
    117  1.1  pgoyette {
    118  1.1  pgoyette 	static char buf[256];
    119  1.1  pgoyette 	pci_vendor_id_t vendor = PCI_VENDOR(id_reg);
    120  1.1  pgoyette 	size_t n;
    121  1.1  pgoyette 
    122  1.1  pgoyette 	for (n = 0; n < __arraycount(pci_vendors); n++) {
    123  1.1  pgoyette 		if (pci_vendors[n] == vendor)
    124  1.1  pgoyette 			return pci_untokenstring(&pci_vendors[n+1], buf,
    125  1.1  pgoyette 			    sizeof(buf));
    126  1.1  pgoyette 
    127  1.1  pgoyette 		/* Skip Tokens */
    128  1.1  pgoyette 		n++;
    129  1.1  pgoyette 		while (pci_vendors[n] != 0 && n < __arraycount(pci_vendors))
    130  1.1  pgoyette 			n++;
    131  1.1  pgoyette 	}
    132  1.1  pgoyette 	return (NULL);
    133  1.1  pgoyette }
    134  1.1  pgoyette 
    135  1.1  pgoyette const char *
    136  1.2  pgoyette pci_findproduct_real(pcireg_t id_reg)
    137  1.1  pgoyette {
    138  1.1  pgoyette 	static char buf[256];
    139  1.1  pgoyette 	pci_vendor_id_t vendor = PCI_VENDOR(id_reg);
    140  1.1  pgoyette 	pci_product_id_t product = PCI_PRODUCT(id_reg);
    141  1.1  pgoyette 	size_t n;
    142  1.1  pgoyette 
    143  1.1  pgoyette 	for (n = 0; n < __arraycount(pci_products); n++) {
    144  1.1  pgoyette 		if (pci_products[n] == vendor && pci_products[n+1] == product)
    145  1.1  pgoyette 			return pci_untokenstring(&pci_products[n+2], buf,
    146  1.1  pgoyette 			    sizeof(buf));
    147  1.1  pgoyette 
    148  1.1  pgoyette 		/* Skip Tokens */
    149  1.1  pgoyette 		n += 2;
    150  1.1  pgoyette 		while (pci_products[n] != 0 && n < __arraycount(pci_products))
    151  1.1  pgoyette 			n++;
    152  1.1  pgoyette 	}
    153  1.1  pgoyette 	return (NULL);
    154  1.1  pgoyette }
    155  1.1  pgoyette 
    156