Home | History | Annotate | Line # | Download | only in ping
ping.c revision 1.1.24.1
      1  1.1.24.1    martin /*	$NetBSD: ping.c,v 1.1.24.1 2020/04/08 14:08:55 martin Exp $	*/
      2       1.1  pgoyette 
      3       1.1  pgoyette /*-
      4       1.1  pgoyette  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5       1.1  pgoyette  * All rights reserved.
      6       1.1  pgoyette  *
      7       1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
      8       1.1  pgoyette  * modification, are permitted provided that the following conditions
      9       1.1  pgoyette  * are met:
     10       1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     11       1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     12       1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     15       1.1  pgoyette  *
     16       1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1  pgoyette  */
     28       1.1  pgoyette 
     29       1.1  pgoyette #include <sys/cdefs.h>
     30  1.1.24.1    martin __KERNEL_RCSID(0, "$NetBSD: ping.c,v 1.1.24.1 2020/04/08 14:08:55 martin Exp $");
     31       1.1  pgoyette 
     32       1.1  pgoyette #include <sys/param.h>
     33       1.1  pgoyette #include <sys/conf.h>
     34       1.1  pgoyette #include <sys/device.h>
     35       1.1  pgoyette #include <sys/kernel.h>
     36       1.1  pgoyette #include <sys/module.h>
     37       1.1  pgoyette 
     38       1.1  pgoyette #include "ping.h"
     39       1.1  pgoyette 
     40       1.1  pgoyette /*
     41       1.1  pgoyette  * Create a device /dev/ping in order to test this module.
     42       1.1  pgoyette  *
     43       1.1  pgoyette  * To use this device you need to do:
     44  1.1.24.1    martin  *     mknod /dev/ping c 351 0
     45       1.1  pgoyette  *
     46       1.1  pgoyette  */
     47       1.1  pgoyette 
     48       1.1  pgoyette dev_type_open(ping_open);
     49       1.1  pgoyette dev_type_close(ping_close);
     50       1.1  pgoyette dev_type_ioctl(ping_ioctl);
     51       1.1  pgoyette 
     52       1.1  pgoyette static struct cdevsw ping_cdevsw = {
     53       1.1  pgoyette 	.d_open = ping_open,
     54       1.1  pgoyette 	.d_close = ping_close,
     55       1.1  pgoyette 	.d_read = noread,
     56       1.1  pgoyette 	.d_write = nowrite,
     57       1.1  pgoyette 	.d_ioctl = ping_ioctl,
     58       1.1  pgoyette 	.d_stop = nostop,
     59       1.1  pgoyette 	.d_tty = notty,
     60       1.1  pgoyette 	.d_poll = nopoll,
     61       1.1  pgoyette 	.d_mmap = nommap,
     62       1.1  pgoyette 	.d_kqfilter = nokqfilter,
     63       1.1  pgoyette 	.d_discard = nodiscard,
     64       1.1  pgoyette 	.d_flag = D_OTHER
     65       1.1  pgoyette };
     66       1.1  pgoyette 
     67       1.1  pgoyette 
     68       1.1  pgoyette struct ping_softc {
     69       1.1  pgoyette 	int		refcnt;
     70       1.1  pgoyette };
     71       1.1  pgoyette 
     72       1.1  pgoyette static struct ping_softc sc;
     73       1.1  pgoyette 
     74       1.1  pgoyette int
     75       1.1  pgoyette ping_open(dev_t self __unused, int flag __unused, int mode __unused,
     76       1.1  pgoyette           struct lwp *l __unused)
     77       1.1  pgoyette {
     78       1.1  pgoyette 	if (sc.refcnt > 0)
     79       1.1  pgoyette 		return EBUSY;
     80       1.1  pgoyette 
     81       1.1  pgoyette 	++sc.refcnt;
     82       1.1  pgoyette 
     83       1.1  pgoyette 	return 0;
     84       1.1  pgoyette }
     85       1.1  pgoyette 
     86       1.1  pgoyette int
     87       1.1  pgoyette ping_close(dev_t self __unused, int flag __unused, int mode __unused,
     88       1.1  pgoyette            struct lwp *l __unused)
     89       1.1  pgoyette {
     90       1.1  pgoyette 	--sc.refcnt;
     91       1.1  pgoyette 
     92       1.1  pgoyette 	return 0;
     93       1.1  pgoyette }
     94       1.1  pgoyette 
     95       1.1  pgoyette int
     96       1.1  pgoyette ping_ioctl(dev_t self __unused, u_long cmd, void *data, int flag,
     97       1.1  pgoyette            struct lwp *l __unused)
     98       1.1  pgoyette {
     99       1.1  pgoyette 	switch(cmd) {
    100       1.1  pgoyette 	case CMD_PING:
    101       1.1  pgoyette 		printf("ping: pong!\n");
    102       1.1  pgoyette 		return 0;
    103       1.1  pgoyette 	default:
    104  1.1.24.1    martin 		return ENOTTY;
    105       1.1  pgoyette 	}
    106       1.1  pgoyette }
    107       1.1  pgoyette 
    108       1.1  pgoyette MODULE(MODULE_CLASS_MISC, ping, NULL);
    109       1.1  pgoyette 
    110       1.1  pgoyette static int
    111       1.1  pgoyette ping_modcmd(modcmd_t cmd, void *arg __unused)
    112       1.1  pgoyette {
    113       1.1  pgoyette 	/* The major should be verified and changed if needed to avoid
    114       1.1  pgoyette 	 * conflicts with other devices. */
    115  1.1.24.1    martin 	int cmajor = 351, bmajor = -1;
    116       1.1  pgoyette 
    117       1.1  pgoyette 	switch (cmd) {
    118       1.1  pgoyette 	case MODULE_CMD_INIT:
    119       1.1  pgoyette 		if (devsw_attach("ping", NULL, &bmajor, &ping_cdevsw, &cmajor))
    120       1.1  pgoyette 			return ENXIO;
    121       1.1  pgoyette 		return 0;
    122       1.1  pgoyette 	case MODULE_CMD_FINI:
    123       1.1  pgoyette 		if (sc.refcnt > 0)
    124       1.1  pgoyette 			return EBUSY;
    125       1.1  pgoyette 
    126       1.1  pgoyette 		devsw_detach(NULL, &ping_cdevsw);
    127       1.1  pgoyette 		return 0;
    128       1.1  pgoyette 	default:
    129       1.1  pgoyette 		return ENOTTY;
    130       1.1  pgoyette 	}
    131       1.1  pgoyette }
    132