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