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