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