1/* $NetBSD: dev_verbose.h,v 1.2 2015/11/13 01:37:19 christos Exp $ */
2
3/*
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#ifndef _DEV_DEV_VERBOSE_H_
26#define _DEV_DEV_VERBOSE_H_
27
28const char *dev_findvendor(char *, size_t, const char *, size_t,
29 const uint16_t *, size_t, uint16_t);
30const char *dev_findproduct(char *, size_t, const char *, size_t,
31 const uint16_t *, size_t, uint16_t, uint16_t);
32
33#define DEV_VERBOSE_COMMON_DEFINE(tag) \
34static const char * \
35tag ## _findvendor_real(char *buf, size_t len, uint16_t vendor) \
36{ \
37 return dev_findvendor(buf, len, tag ## _words, \
38 __arraycount(tag ## _words), tag ## _vendors, \
39 __arraycount(tag ## _vendors), vendor); \
40} \
41 \
42static const char * \
43tag ## _findproduct_real(char *buf, size_t len, uint16_t vendor, \
44 uint16_t product) \
45{ \
46 return dev_findproduct(buf, len, tag ## _words, \
47 __arraycount(tag ## _words), tag ## _products, \
48 __arraycount(tag ## _products), vendor, product); \
49} \
50
51#ifdef _KERNEL
52
53#define DEV_VERBOSE_MODULE_DEFINE(tag, deps) \
54DEV_VERBOSE_COMMON_DEFINE(tag) \
55extern int tag ## verbose_loaded; \
56 \
57static int \
58tag ## verbose_modcmd(modcmd_t cmd, void *arg) \
59{ \
60 static const char *(*saved_findvendor)(char *, size_t, \
61 uint16_t); \
62 static const char *(*saved_findproduct)(char *, size_t, \
63 uint16_t, uint16_t); \
64 \
65 switch (cmd) { \
66 case MODULE_CMD_INIT: \
67 saved_findvendor = tag ## _findvendor; \
68 saved_findproduct = tag ## _findproduct; \
69 tag ## _findvendor = tag ## _findvendor_real; \
70 tag ## _findproduct = tag ## _findproduct_real; \
71 tag ## verbose_loaded = 1; \
72 return 0; \
73 case MODULE_CMD_FINI: \
74 tag ## _findvendor = saved_findvendor; \
75 tag ## _findproduct = saved_findproduct; \
76 tag ## verbose_loaded = 0; \
77 return 0; \
78 default: \
79 return ENOTTY; \
80 } \
81} \
82MODULE(MODULE_CLASS_MISC, tag ## verbose, deps)
83
84#endif /* KERNEL */
85
86#define DEV_VERBOSE_DECLARE(tag) \
87extern const char * (*tag ## _findvendor)(char *, size_t, uint16_t); \
88extern const char * (*tag ## _findproduct)(char *, size_t, uint16_t, uint16_t)
89
90#if defined(_KERNEL)
91#define DEV_VERBOSE_DEFINE(tag) \
92int tag ## verbose_loaded = 0; \
93 \
94static void \
95tag ## _load_verbose(void) \
96{ \
97 \
98 if (tag ## verbose_loaded == 0) \
99 module_autoload(# tag "verbose", MODULE_CLASS_MISC); \
100} \
101 \
102static const char * \
103tag ## _findvendor_stub(char *buf, size_t len, uint16_t vendor) \
104{ \
105 \
106 tag ## _load_verbose(); \
107 if (tag ## verbose_loaded) \
108 return tag ## _findvendor(buf, len, vendor); \
109 else { \
110 snprintf(buf, len, "vendor %4.4x", vendor); \
111 return NULL; \
112 } \
113} \
114 \
115static const char * \
116tag ## _findproduct_stub(char *buf, size_t len, uint16_t vendor, \
117 uint16_t product) \
118{ \
119 \
120 tag ## _load_verbose(); \
121 if (tag ## verbose_loaded) \
122 return tag ## _findproduct(buf, len, vendor, product); \
123 else { \
124 snprintf(buf, len, "product %4.4x", product); \
125 return NULL; \
126 } \
127} \
128 \
129const char *(*tag ## _findvendor)(char *, size_t, uint16_t) = \
130 tag ## _findvendor_stub; \
131const char *(*tag ## _findproduct)(char *, size_t, uint16_t, uint16_t) =\
132 tag ## _findproduct_stub \
133
134#else
135
136#define DEV_VERBOSE_DEFINE(tag) \
137DEV_VERBOSE_COMMON_DEFINE(tag) \
138const char *(*tag ## _findvendor)(char *, size_t, uint16_t) = \
139 tag ## _findvendor_real; \
140const char *(*tag ## _findproduct)(char *, size_t, uint16_t, uint16_t) =\
141 tag ## _findproduct_real \
142
143#endif /* _KERNEL */
144
145#endif /* _DEV_DEV_VERBOSE_H_ */
146