xref: /freebsd/share/examples/kld/cdev/module/cdevmod.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /* 08 Nov 1998*/
2 /*-
3  * cdevmod.c - a sample kld module implementing a character device driver.
4  *
5  * 08 Nov 1998  Rajesh Vaidheeswarran
6  *
7  * SPDX-License-Identifier: BSD-4-Clause
8  *
9  * Copyright (c) 1998 Rajesh Vaidheeswarran
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by Rajesh Vaidheeswarran.
23  * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote
24  *    products derived from this software without specific prior written
25  *    permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY
28  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  * Copyright (c) 1993 Terrence R. Lambert.
40  * All rights reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *      This product includes software developed by Terrence R. Lambert.
53  * 4. The name Terrence R. Lambert may not be used to endorse or promote
54  *    products derived from this software without specific prior written
55  *    permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY
58  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  *
70  * $FreeBSD$
71  */
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/module.h>
76 #include <sys/conf.h>
77 
78 #include "cdev.h"
79 
80 static struct cdevsw my_devsw = {
81 	/* version */	.d_version = D_VERSION,
82 	/* open */	.d_open = mydev_open,
83 	/* close */	.d_close = mydev_close,
84 	/* read */	.d_read = mydev_read,
85 	/* write */	.d_write = mydev_write,
86 	/* ioctl */	.d_ioctl = mydev_ioctl,
87 	/* name */	.d_name = "cdev"
88 };
89 
90 /*
91  * Used as the variable that is the reference to our device
92  * in devfs... we must keep this variable sane until we
93  * call kldunload.
94  */
95 static struct cdev *sdev;
96 
97 /*
98  * This function is called each time the module is loaded or unloaded.
99  * Since we are a miscellaneous module, we have to provide whatever
100  * code is necessary to patch ourselves into the area we are being
101  * loaded to change.
102  *
103  * The stat information is basically common to all modules, so there
104  * is no real issue involved with stat; we will leave it lkm_nullcmd(),
105  * since we don't have to do anything about it.
106  */
107 
108 static int
109 cdev_load(module_t mod, int cmd, void *arg)
110 {
111     int  err = 0;
112     struct make_dev_args mda;
113 
114     switch (cmd) {
115     case MOD_LOAD:
116 
117 	/* Do any initialization that you should do with the kernel */
118 
119 	/* if we make it to here, print copyright on console*/
120 	printf("\nSample Loaded kld character device driver\n");
121 	printf("Copyright (c) 1998\n");
122 	printf("Rajesh Vaidheeswarran\n");
123 	printf("All rights reserved\n");
124 
125 	make_dev_args_init(&mda);
126 	mda.mda_devsw = &my_devsw;
127 	mda.mda_uid = UID_ROOT;
128 	mda.mda_gid = GID_WHEEL;
129 	mda.mda_mode = 0600;
130 	err = make_dev_s(&mda, &sdev, "cdev");
131 	break;
132 
133     case MOD_UNLOAD:
134 	printf("Unloaded kld character device driver\n");
135 	destroy_dev(sdev);
136 	break;		/* Success*/
137 
138     default:	/* we only understand load/unload*/
139 	err = EOPNOTSUPP;
140 	break;
141     }
142 
143     return(err);
144 }
145 
146 /* Now declare the module to the system */
147 
148 DEV_MODULE(cdev, cdev_load, NULL);
149