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