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