Path: blob/main/share/examples/kld/cdev/module/cdevmod.c
39553 views
/* 08 Nov 1998*/1/*-2* cdevmod.c - a sample kld module implementing a character device driver.3*4* 08 Nov 1998 Rajesh Vaidheeswarran5*6* SPDX-License-Identifier: BSD-4-Clause7*8* Copyright (c) 1998 Rajesh Vaidheeswarran9* All rights reserved.10*11* Redistribution and use in source and binary forms, with or without12* modification, are permitted provided that the following conditions13* are met:14* 1. Redistributions of source code must retain the above copyright15* notice, this list of conditions and the following disclaimer.16* 2. Redistributions in binary form must reproduce the above copyright17* notice, this list of conditions and the following disclaimer in the18* documentation and/or other materials provided with the distribution.19* 3. All advertising materials mentioning features or use of this software20* must display the following acknowledgement:21* This product includes software developed by Rajesh Vaidheeswarran.22* 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote23* products derived from this software without specific prior written24* permission.25*26* THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY27* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE28* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29* ARE DISCLAIMED. IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE30* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL31* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS32* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)33* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT34* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY35* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF36* SUCH DAMAGE.37*38* Copyright (c) 1993 Terrence R. Lambert.39* All rights reserved.40*41* Redistribution and use in source and binary forms, with or without42* modification, are permitted provided that the following conditions43* are met:44* 1. Redistributions of source code must retain the above copyright45* notice, this list of conditions and the following disclaimer.46* 2. Redistributions in binary form must reproduce the above copyright47* notice, this list of conditions and the following disclaimer in the48* documentation and/or other materials provided with the distribution.49* 3. All advertising materials mentioning features or use of this software50* must display the following acknowledgement:51* This product includes software developed by Terrence R. Lambert.52* 4. The name Terrence R. Lambert may not be used to endorse or promote53* products derived from this software without specific prior written54* permission.55*56* THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY57* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE58* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE59* ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE60* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL61* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS62* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)63* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT64* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY65* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF66* SUCH DAMAGE.67*68*/69#include <sys/param.h>70#include <sys/systm.h>71#include <sys/kernel.h>72#include <sys/module.h>73#include <sys/conf.h>7475#include "cdev.h"7677static struct cdevsw my_devsw = {78/* version */ .d_version = D_VERSION,79/* open */ .d_open = mydev_open,80/* close */ .d_close = mydev_close,81/* read */ .d_read = mydev_read,82/* write */ .d_write = mydev_write,83/* ioctl */ .d_ioctl = mydev_ioctl,84/* name */ .d_name = "cdev"85};8687/*88* Used as the variable that is the reference to our device89* in devfs... we must keep this variable sane until we90* call kldunload.91*/92static struct cdev *sdev;9394/*95* This function is called each time the module is loaded or unloaded.96* Since we are a miscellaneous module, we have to provide whatever97* code is necessary to patch ourselves into the area we are being98* loaded to change.99*100* The stat information is basically common to all modules, so there101* is no real issue involved with stat; we will leave it lkm_nullcmd(),102* since we don't have to do anything about it.103*/104105static int106cdev_load(module_t mod, int cmd, void *arg)107{108int err = 0;109struct make_dev_args mda;110111switch (cmd) {112case MOD_LOAD:113114/* Do any initialization that you should do with the kernel */115116/* if we make it to here, print copyright on console*/117printf("\nSample Loaded kld character device driver\n");118printf("Copyright (c) 1998\n");119printf("Rajesh Vaidheeswarran\n");120printf("All rights reserved\n");121122make_dev_args_init(&mda);123mda.mda_devsw = &my_devsw;124mda.mda_uid = UID_ROOT;125mda.mda_gid = GID_WHEEL;126mda.mda_mode = 0600;127err = make_dev_s(&mda, &sdev, "cdev");128break;129130case MOD_UNLOAD:131printf("Unloaded kld character device driver\n");132destroy_dev(sdev);133break; /* Success*/134135default: /* we only understand load/unload*/136err = EOPNOTSUPP;137break;138}139140return(err);141}142143/* Now declare the module to the system */144145DEV_MODULE(cdev, cdev_load, NULL);146147148