141681Sdfr/* 08 Nov 1998*/
241681Sdfr/*
341681Sdfr * cdevmod.c - a sample kld module implementing a character device driver.
441681Sdfr *
541681Sdfr * 08 Nov 1998  Rajesh Vaidheeswarran
641681Sdfr *
741681Sdfr * Copyright (c) 1998 Rajesh Vaidheeswarran
841681Sdfr * All rights reserved.
941681Sdfr *
1041681Sdfr * Redistribution and use in source and binary forms, with or without
1141681Sdfr * modification, are permitted provided that the following conditions
1241681Sdfr * are met:
1341681Sdfr * 1. Redistributions of source code must retain the above copyright
1441681Sdfr *    notice, this list of conditions and the following disclaimer.
1541681Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1641681Sdfr *    notice, this list of conditions and the following disclaimer in the
1741681Sdfr *    documentation and/or other materials provided with the distribution.
1841681Sdfr * 3. All advertising materials mentioning features or use of this software
1941681Sdfr *    must display the following acknowledgement:
2041681Sdfr *      This product includes software developed by Rajesh Vaidheeswarran.
2141681Sdfr * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote
2241681Sdfr *    products derived from this software without specific prior written
2341681Sdfr *    permission.
2441681Sdfr *
2541681Sdfr * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY
2641681Sdfr * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2741681Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2841681Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE
2941681Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3041681Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3141681Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3241681Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3341681Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3441681Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3541681Sdfr * SUCH DAMAGE.
3641681Sdfr *
3741681Sdfr * Copyright (c) 1993 Terrence R. Lambert.
3841681Sdfr * All rights reserved.
3941681Sdfr *
4041681Sdfr * Redistribution and use in source and binary forms, with or without
4141681Sdfr * modification, are permitted provided that the following conditions
4241681Sdfr * are met:
4341681Sdfr * 1. Redistributions of source code must retain the above copyright
4441681Sdfr *    notice, this list of conditions and the following disclaimer.
4541681Sdfr * 2. Redistributions in binary form must reproduce the above copyright
4641681Sdfr *    notice, this list of conditions and the following disclaimer in the
4741681Sdfr *    documentation and/or other materials provided with the distribution.
4841681Sdfr * 3. All advertising materials mentioning features or use of this software
4941681Sdfr *    must display the following acknowledgement:
5041681Sdfr *      This product includes software developed by Terrence R. Lambert.
5141681Sdfr * 4. The name Terrence R. Lambert may not be used to endorse or promote
5241681Sdfr *    products derived from this software without specific prior written
5341681Sdfr *    permission.
5441681Sdfr *
5541681Sdfr * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY
5641681Sdfr * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5741681Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5841681Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
5941681Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6041681Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6141681Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6241681Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6341681Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6441681Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6541681Sdfr * SUCH DAMAGE.
6641681Sdfr *
6766549Ssobomax *
6866549Ssobomax * $FreeBSD$
6941681Sdfr */
7041681Sdfr#include <sys/param.h>
7141681Sdfr#include <sys/systm.h>
7241681Sdfr#include <sys/kernel.h>
7341681Sdfr#include <sys/module.h>
7448275Sdfr#include <sys/conf.h>
7541681Sdfr
7641681Sdfr#include "cdev.h"
7741681Sdfr
7848275Sdfrstatic struct cdevsw my_devsw = {
79132247Stjr	/* version */	.d_version = D_VERSION,
80118385Smbr	/* open */	.d_open = mydev_open,
81118385Smbr	/* close */	.d_close = mydev_close,
82118385Smbr	/* read */	.d_read = mydev_read,
83118385Smbr	/* write */	.d_write = mydev_write,
84118385Smbr	/* ioctl */	.d_ioctl = mydev_ioctl,
85158751Ssobomax	/* name */	.d_name = "cdev"
8648275Sdfr};
8748275Sdfr
8866549Ssobomax/*
8966549Ssobomax * Used as the variable that is the reference to our device
9066549Ssobomax * in devfs... we must keep this variable sane until we
9166549Ssobomax * call kldunload.
9266549Ssobomax */
93132247Stjrstatic struct cdev *sdev;
9466549Ssobomax
9541681Sdfr/*
9641681Sdfr * This function is called each time the module is loaded or unloaded.
9741681Sdfr * Since we are a miscellaneous module, we have to provide whatever
9841681Sdfr * code is necessary to patch ourselves into the area we are being
9941681Sdfr * loaded to change.
10041681Sdfr *
10141681Sdfr * The stat information is basically common to all modules, so there
10241681Sdfr * is no real issue involved with stat; we will leave it lkm_nullcmd(),
10341681Sdfr * since we don't have to do anything about it.
10441681Sdfr */
10541681Sdfr
10641681Sdfrstatic int
10748275Sdfrcdev_load(module_t mod, int cmd, void *arg)
10841681Sdfr{
10941681Sdfr    int  err = 0;
11041681Sdfr
11141681Sdfr    switch (cmd) {
11241681Sdfr    case MOD_LOAD:
11341681Sdfr
11441681Sdfr	/* Do any initialization that you should do with the kernel */
11541681Sdfr
11641681Sdfr	/* if we make it to here, print copyright on console*/
11741681Sdfr	printf("\nSample Loaded kld character device driver\n");
11841681Sdfr	printf("Copyright (c) 1998\n");
11941681Sdfr	printf("Rajesh Vaidheeswarran\n");
12041681Sdfr	printf("All rights reserved\n");
12166549Ssobomax	sdev = make_dev(&my_devsw, 0, UID_ROOT, GID_WHEEL, 0600, "cdev");
12241681Sdfr	break;		/* Success*/
12341681Sdfr
12441681Sdfr    case MOD_UNLOAD:
12541681Sdfr	printf("Unloaded kld character device driver\n");
12666549Ssobomax	destroy_dev(sdev);
12741681Sdfr	break;		/* Success*/
12841681Sdfr
12941681Sdfr    default:	/* we only understand load/unload*/
130134520Spjd	err = EOPNOTSUPP;
13141681Sdfr	break;
13241681Sdfr    }
13341681Sdfr
13441681Sdfr    return(err);
13541681Sdfr}
13641681Sdfr
13741681Sdfr/* Now declare the module to the system */
13841681Sdfr
13966549SsobomaxDEV_MODULE(cdev, cdev_load, NULL);
140