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