xref: /netbsd-src/sys/modules/examples/ping/ping.c (revision 7fa5b1d2e938a8c905bee5f3d38bda8450277ea2)
1 /*	$NetBSD: ping.c,v 1.5 2020/04/30 10:55:32 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2015 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.5 2020/04/30 10:55:32 christos 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 c 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 = ping_open,
54 	.d_close = ping_close,
55 	.d_read = noread,
56 	.d_write = nowrite,
57 	.d_ioctl = ping_ioctl,
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 
68 struct ping_softc {
69 	int		refcnt;
70 };
71 
72 static struct ping_softc sc;
73 
74 int
ping_open(dev_t self __unused,int flag __unused,int mode __unused,struct lwp * l __unused)75 ping_open(dev_t self __unused, int flag __unused, int mode __unused,
76           struct lwp *l __unused)
77 {
78 	if (sc.refcnt > 0)
79 		return EBUSY;
80 
81 	++sc.refcnt;
82 
83 	return 0;
84 }
85 
86 int
ping_close(dev_t self __unused,int flag __unused,int mode __unused,struct lwp * l __unused)87 ping_close(dev_t self __unused, int flag __unused, int mode __unused,
88            struct lwp *l __unused)
89 {
90 	--sc.refcnt;
91 
92 	return 0;
93 }
94 
95 int
ping_ioctl(dev_t self __unused,u_long cmd,void * data,int flag,struct lwp * l __unused)96 ping_ioctl(dev_t self __unused, u_long cmd, void *data, int flag,
97            struct lwp *l __unused)
98 {
99 	switch(cmd) {
100 	case CMD_PING:
101 		printf("ping: pong!\n");
102 		return 0;
103 	default:
104 		return ENOTTY;
105 	}
106 }
107 
108 MODULE(MODULE_CLASS_MISC, ping, NULL);
109 
110 static int
ping_modcmd(modcmd_t cmd,void * arg __unused)111 ping_modcmd(modcmd_t cmd, void *arg __unused)
112 {
113 	/* The major should be verified and changed if needed to avoid
114 	 * conflicts with other devices. */
115 	int cmajor = 351, bmajor = -1;
116 
117 	switch (cmd) {
118 	case MODULE_CMD_INIT:
119 		if (devsw_attach("ping", NULL, &bmajor, &ping_cdevsw, &cmajor))
120 			return ENXIO;
121 		return 0;
122 	case MODULE_CMD_FINI:
123 		if (sc.refcnt > 0)
124 			return EBUSY;
125 
126 		devsw_detach(NULL, &ping_cdevsw);
127 		return 0;
128 	default:
129 		return ENOTTY;
130 	}
131 }
132