Home | History | Annotate | Line # | Download | only in dev
dev_verbose.h revision 1.7
      1 /*	$NetBSD: dev_verbose.h,v 1.7 2021/06/15 23:24:57 riastradh 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 
     28 #ifdef _KERNEL
     29 #include <sys/module_hook.h>
     30 #endif
     31 
     32 const char *dev_findvendor(char *, size_t, const char *, size_t,
     33 	const uint16_t *, size_t, uint16_t);
     34 const char *dev_findproduct(char *, size_t, const char *, size_t,
     35 	const uint16_t *, size_t, uint16_t, uint16_t);
     36 
     37 #define DEV_VERBOSE_COMMON_DEFINE(tag)					\
     38 static const char *							\
     39 tag ## _findvendor_real(char *buf, size_t len, uint16_t vendor)		\
     40 {									\
     41 	return dev_findvendor(buf, len, tag ## _words,			\
     42 	    __arraycount(tag ## _words), tag ## _vendors,		\
     43 	    __arraycount(tag ## _vendors), vendor);			\
     44 }									\
     45 									\
     46 static const char *							\
     47 tag ## _findproduct_real(char *buf, size_t len, uint16_t vendor,	\
     48     uint16_t product)							\
     49 {									\
     50 	return dev_findproduct(buf, len, tag ## _words,			\
     51 	    __arraycount(tag ## _words), tag ## _products,		\
     52 	    __arraycount(tag ## _products), vendor, product);		\
     53 }									\
     54 
     55 #ifdef _KERNEL
     56 
     57 #define DEV_VERBOSE_KERNEL_DECLARE(tag)					\
     58 MODULE_HOOK(tag ## _findvendor_hook, const char *,			\
     59 	(char *, size_t, uint16_t));					\
     60 MODULE_HOOK(tag ## _findproduct_hook, const char *,			\
     61 	(char *, size_t, uint16_t, uint16_t));				\
     62 extern int tag ## verbose_loaded;
     63 
     64 #define DEV_VERBOSE_MODULE_DEFINE(tag, deps)				\
     65 DEV_VERBOSE_COMMON_DEFINE(tag)						\
     66 DEV_VERBOSE_KERNEL_DECLARE(tag)						\
     67 									\
     68 static int								\
     69 tag ## verbose_modcmd(modcmd_t cmd, void *arg)				\
     70 {									\
     71 									\
     72 	switch (cmd) {							\
     73 	case MODULE_CMD_INIT:						\
     74 		MODULE_HOOK_SET(tag ## _findvendor_hook,		\
     75 			tag ## _findvendor_real);			\
     76 		MODULE_HOOK_SET(tag ## _findproduct_hook,		\
     77 			tag ## _findproduct_real);			\
     78 		tag ## verbose_loaded = 1;				\
     79 		return 0;						\
     80 	case MODULE_CMD_FINI:						\
     81 		tag ## verbose_loaded = 0;				\
     82 		MODULE_HOOK_UNSET(tag ## _findproduct_hook);		\
     83 		MODULE_HOOK_UNSET(tag ## _findvendor_hook);		\
     84 		return 0;						\
     85 	default:							\
     86 		return ENOTTY;						\
     87 	}								\
     88 }									\
     89 MODULE(MODULE_CLASS_DRIVER, tag ## verbose, deps)
     90 
     91 #endif /* KERNEL */
     92 
     93 #define DEV_VERBOSE_DECLARE(tag)					\
     94 extern const char * tag ## _findvendor(char *, size_t, uint16_t);	\
     95 extern const char * tag ## _findproduct(char *, size_t, uint16_t, uint16_t)
     96 
     97 #if defined(_KERNEL)
     98 
     99 #define DEV_VERBOSE_DEFINE(tag)						\
    100 DEV_VERBOSE_KERNEL_DECLARE(tag)						\
    101 struct tag ## _findvendor_hook_t tag ## _findvendor_hook;		\
    102 struct tag ## _findproduct_hook_t tag ## _findproduct_hook;		\
    103 int tag ## verbose_loaded = 0;						\
    104 									\
    105 static void								\
    106 tag ## _load_verbose(void)						\
    107 {									\
    108 									\
    109 	if (tag ## verbose_loaded == 0)					\
    110 		module_autoload(# tag "verbose", MODULE_CLASS_DRIVER);	\
    111 }									\
    112 									\
    113 const char *								\
    114 tag ## _findvendor(char *buf, size_t len, uint16_t vendor)		\
    115 {									\
    116 	const char *retval = NULL;					\
    117 									\
    118 	tag ## _load_verbose();						\
    119 	MODULE_HOOK_CALL(tag ## _findvendor_hook, (buf, len, vendor),	\
    120 		(snprintf(buf, len, "vendor %4.4x", vendor), NULL),	\
    121 		retval);						\
    122 	return retval;							\
    123 }									\
    124 									\
    125 const char *								\
    126 tag ## _findproduct(char *buf, size_t len, uint16_t vendor,		\
    127     uint16_t product)							\
    128 {									\
    129 	const char *retval = NULL;					\
    130 									\
    131 	tag ## _load_verbose();						\
    132 	MODULE_HOOK_CALL(tag ## _findproduct_hook,			\
    133 		(buf, len, vendor, product),				\
    134 		(snprintf(buf, len, "product %4.4x", product), NULL),	\
    135 		retval);						\
    136 	return retval;							\
    137 }									\
    138 
    139 #else	/* _KERNEL */
    140 
    141 #define DEV_VERBOSE_DEFINE(tag)						\
    142 DEV_VERBOSE_COMMON_DEFINE(tag)						\
    143 const char *tag ## _findvendor(char *buf, size_t len, uint16_t vendor)	\
    144 {									\
    145 									\
    146 	return tag ## _findvendor_real(buf, len, vendor);		\
    147 }									\
    148 									\
    149 const char *tag ## _findproduct(char *buf, size_t len, uint16_t vendor,	\
    150 		uint16_t product)					\
    151 {									\
    152 									\
    153 	return tag ## _findproduct_real(buf, len, vendor, product);	\
    154 }
    155 
    156 #endif /* _KERNEL */
    157 
    158 #endif /* _DEV_DEV_VERBOSE_H_ */
    159