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