pci_verbose.c revision 1.5.2.2 1 /* $NetBSD: pci_verbose.c,v 1.5.2.2 2010/05/30 05:17:36 rmind 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.5.2.2 2010/05/30 05:17:36 rmind 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 switch (cmd) {
78 case MODULE_CMD_INIT:
79 pci_findvendor = pci_findvendor_real;
80 pci_findproduct = pci_findproduct_real;
81 pci_unmatched = "unmatched ";
82 return 0;
83 case MODULE_CMD_FINI:
84 pci_findvendor = pci_null;
85 pci_findproduct = pci_null;
86 pci_unmatched = "";
87 return 0;
88 default:
89 return ENOTTY;
90 }
91 }
92 #endif /* KERNEL */
93
94 static const char *
95 pci_untokenstring(const uint16_t *token, char *buf, size_t len)
96 {
97 char *cp = buf;
98
99 buf[0] = '\0';
100 for (; *token != 0; token++) {
101 cp = buf + strlcat(buf, pci_words + *token, len - 2);
102 cp[0] = ' ';
103 cp[1] = '\0';
104 }
105 *cp = '\0';
106 return cp != buf ? buf : NULL;
107 }
108
109 const char *
110 pci_findvendor_real(pcireg_t id_reg)
111 {
112 static char buf[256];
113 pci_vendor_id_t vendor = PCI_VENDOR(id_reg);
114 size_t n;
115
116 for (n = 0; n < __arraycount(pci_vendors); n++) {
117 if (pci_vendors[n] == vendor)
118 return pci_untokenstring(&pci_vendors[n+1], buf,
119 sizeof(buf));
120
121 /* Skip Tokens */
122 n++;
123 while (pci_vendors[n] != 0 && n < __arraycount(pci_vendors))
124 n++;
125 }
126 return (NULL);
127 }
128
129 const char *
130 pci_findproduct_real(pcireg_t id_reg)
131 {
132 static char buf[256];
133 pci_vendor_id_t vendor = PCI_VENDOR(id_reg);
134 pci_product_id_t product = PCI_PRODUCT(id_reg);
135 size_t n;
136
137 for (n = 0; n < __arraycount(pci_products); n++) {
138 if (pci_products[n] == vendor && pci_products[n+1] == product)
139 return pci_untokenstring(&pci_products[n+2], buf,
140 sizeof(buf));
141
142 /* Skip Tokens */
143 n += 2;
144 while (pci_products[n] != 0 && n < __arraycount(pci_products))
145 n++;
146 }
147 return (NULL);
148 }
149
150