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