Home | History | Annotate | Line # | Download | only in ping_block
      1 /*	$NetBSD: ping.c,v 1.2 2020/02/05 14:10:47 pgoyette Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2020 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 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: ping.c,v 1.2 2020/02/05 14:10:47 pgoyette Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/conf.h>
     34 #include <sys/device.h>
     35 #include <sys/kernel.h>
     36 #include <sys/module.h>
     37 
     38 #include "ping.h"
     39 
     40 /*
     41  * Create a device /dev/ping in order to test this module.
     42  *
     43  * To use this device you need to do:
     44  *     mknod /dev/ping b 351 0
     45  *
     46  */
     47 
     48 dev_type_open(ping_open);
     49 dev_type_close(ping_close);
     50 dev_type_ioctl(ping_ioctl);
     51 
     52 static struct cdevsw ping_cdevsw = {
     53 	.d_open = noopen,
     54 	.d_close = noclose,
     55 	.d_read = noread,
     56 	.d_write = nowrite,
     57 	.d_ioctl = noioctl,
     58 	.d_stop = nostop,
     59 	.d_tty = notty,
     60 	.d_poll = nopoll,
     61 	.d_mmap = nommap,
     62 	.d_kqfilter = nokqfilter,
     63 	.d_discard = nodiscard,
     64 	.d_flag = D_OTHER
     65 };
     66 
     67 static struct bdevsw ping_bdevsw = {
     68 	.d_open = ping_open,
     69 	.d_close = ping_close,
     70 	.d_strategy = NULL,
     71 	.d_ioctl = ping_ioctl,
     72 	.d_dump = nodump,
     73 	.d_psize = nosize,
     74 	.d_discard = nodiscard,
     75 	.d_flag = D_OTHER
     76 };
     77 
     78 struct ping_softc {
     79 	int		refcnt;
     80 };
     81 
     82 static struct ping_softc sc;
     83 
     84 int
     85 ping_open(dev_t self __unused, int flag __unused, int mode __unused,
     86           struct lwp *l __unused)
     87 {
     88 	if (sc.refcnt > 0)
     89 		return EBUSY;
     90 
     91 	++sc.refcnt;
     92 
     93 	return 0;
     94 }
     95 
     96 int
     97 ping_close(dev_t self __unused, int flag __unused, int mode __unused,
     98            struct lwp *l __unused)
     99 {
    100 	--sc.refcnt;
    101 
    102 	return 0;
    103 }
    104 
    105 int
    106 ping_ioctl(dev_t self __unused, u_long cmd, void *data, int flag,
    107            struct lwp *l __unused)
    108 {
    109 	switch(cmd) {
    110 	case CMD_PING:
    111 		printf("ping: pong!\n");
    112 		return 0;
    113 	default:
    114 		return ENOTTY;
    115 	}
    116 }
    117 
    118 MODULE(MODULE_CLASS_MISC, ping, NULL);
    119 
    120 static int
    121 ping_modcmd(modcmd_t cmd, void *arg __unused)
    122 {
    123 	int bmajor = 351, cmajor = -1;
    124 
    125 	switch (cmd) {
    126 	case MODULE_CMD_INIT:
    127 		if (devsw_attach("ping", &ping_bdevsw, &bmajor, &ping_cdevsw, &cmajor))
    128 			return ENXIO;
    129 		return 0;
    130 	case MODULE_CMD_FINI:
    131 		if (sc.refcnt > 0)
    132 			return EBUSY;
    133 
    134 		devsw_detach(&ping_bdevsw, &ping_cdevsw);
    135 		return 0;
    136 	default:
    137 		return ENOTTY;
    138 	}
    139 }
    140