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