xref: /netbsd-src/sys/net/if_module.h (revision 1d8e08d4c8dc7da4e3393437ab16917ebe2f31d4)
1 /*	$NetBSD: if_module.h,v 1.1 2016/08/07 17:38:34 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifdef _MODULE
30 # define IF_MODULE_CFDRIVER_DECL(name) CFDRIVER_DECL(name, DV_IFNET, NULL)
31 # define IF_MODULE_CFDRIVER_ATTACH(name)			\
32     error = config_cfdriver_attach(& name ## _cd);		\
33     if (error) {						\
34 	    aprint_error("%s: unable to register cfdriver for"	\
35 		"%s, error %d\n", __func__, name ## _cd.cd_name,\
36 		error);						\
37 	    break;						\
38     }
39 # define IF_MODULE_CFDRIVER_DETACH(name)			\
40     /* Remove device from autoconf database */			\
41     error = config_cfdriver_detach(&name ## _cd);		\
42     if (error) {						\
43 	    aprint_error("%s: failed to detach %s cfdriver, "	\
44 		"error %d\n", __func__, name ## _cd.cd_name,	\
45 		error);						\
46 	    break;						\
47     }
48 #else
49 # define IF_MODULE_CFDRIVER_DECL(name)
50 # define IF_MODULE_CFDRIVER_ATTACH(name)
51 # define IF_MODULE_CFDRIVER_DETACH(name)
52 #endif
53 
54 #define IF_MODULE(class, name, dep)				\
55 MODULE(class, if_ ## name, dep);				\
56 IF_MODULE_CFDRIVER_DECL(name);					\
57 static int							\
58 if_ ## name ## _modcmd(modcmd_t cmd, void *arg)			\
59 {								\
60 	int error = 0;						\
61 								\
62 	switch (cmd) {						\
63 	case MODULE_CMD_INIT:					\
64 		IF_MODULE_CFDRIVER_ATTACH(name)			\
65 		/* Init the unit list/line discipline stuff */	\
66 		name ## init();					\
67 		break;						\
68 								\
69 	case MODULE_CMD_FINI:					\
70 		/*						\
71 		 * Make sure it's ok to detach - no units left,	\
72 		 * and line discipline is removed		\
73 		 */						\
74 		error = name ## detach();			\
75 		if (error != 0)					\
76 			break;					\
77 		IF_MODULE_CFDRIVER_DETACH(name)			\
78 		break;						\
79 								\
80 	case MODULE_CMD_STAT:					\
81 		error = ENOTTY;					\
82 		break;						\
83 	default:						\
84 		error = ENOTTY;					\
85 		break;						\
86 	}							\
87 								\
88 	return error;						\
89 }
90