1 /*- 2 * Copyright (c) 2000 Mark R. V. Murray & Jeroen C. van Gelderen 3 * Copyright (c) 2001-2004 Mark R. V. Murray 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/conf.h> 35 #include <sys/uio.h> 36 #include <sys/kernel.h> 37 #include <sys/malloc.h> 38 #include <sys/module.h> 39 #include <sys/disk.h> 40 #include <sys/bus.h> 41 #include <machine/bus.h> 42 43 /* For use with destroy_dev(9). */ 44 static struct cdev *null_dev; 45 static struct cdev *zero_dev; 46 static struct cdev *full_dev; 47 48 static d_write_t null_write; 49 static d_ioctl_t null_ioctl; 50 static d_read_t zero_read; 51 static d_write_t full_write; 52 53 #define CDEV_MAJOR 2 54 #define NULL_MINOR 0 55 #define ZERO_MINOR 1 56 #define FULL_MINOR 2 57 58 static struct cdevsw null_cdevsw = { 59 .d_version = D_VERSION, 60 .d_read = (d_read_t *)nullop, 61 .d_write = null_write, 62 .d_ioctl = null_ioctl, 63 .d_name = "null", 64 .d_maj = CDEV_MAJOR, 65 }; 66 67 static struct cdevsw zero_cdevsw = { 68 .d_version = D_VERSION, 69 .d_read = zero_read, 70 .d_write = null_write, 71 .d_name = "zero", 72 .d_maj = CDEV_MAJOR, 73 .d_flags = D_MMAP_ANON, 74 }; 75 76 static struct cdevsw full_cdevsw = { 77 .d_version = D_VERSION, 78 .d_read = (d_read_t *)nullop, 79 .d_write = full_write, 80 .d_name = "full", 81 .d_maj = CDEV_MAJOR, 82 }; 83 84 static void *zbuf; 85 86 /* ARGSUSED */ 87 static int 88 null_write(struct cdev *dev __unused, struct uio *uio, int flags __unused) 89 { 90 uio->uio_resid = 0; 91 92 return (0); 93 } 94 95 /* ARGSUSED */ 96 static int 97 null_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused, 98 int flags __unused, struct thread *td) 99 { 100 int error; 101 102 if (cmd != DIOCSKERNELDUMP) 103 return (ENOIOCTL); 104 error = suser(td); 105 if (error) 106 return (error); 107 return (set_dumper(NULL)); 108 } 109 110 /* ARGSUSED */ 111 static int 112 zero_read(struct cdev *dev __unused, struct uio *uio, int flags __unused) 113 { 114 int error = 0; 115 116 while (uio->uio_resid > 0 && error == 0) 117 error = uiomove(zbuf, MIN(uio->uio_resid, PAGE_SIZE), uio); 118 119 return (error); 120 } 121 122 /* ARGSUSED */ 123 static int 124 full_write(struct cdev *dev __unused, struct uio *uio, int flags __unused) 125 { 126 return (ENOSPC); 127 } 128 129 /* ARGSUSED */ 130 static int 131 null_modevent(module_t mod __unused, int type, void *data __unused) 132 { 133 switch(type) { 134 case MOD_LOAD: 135 if (bootverbose) 136 printf("null: <null device, zero device>\n"); 137 zbuf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK | M_ZERO); 138 null_dev = make_dev(&null_cdevsw, NULL_MINOR, UID_ROOT, 139 GID_WHEEL, 0666, "null"); 140 zero_dev = make_dev(&zero_cdevsw, ZERO_MINOR, UID_ROOT, 141 GID_WHEEL, 0666, "zero"); 142 full_dev = make_dev(&full_cdevsw, FULL_MINOR, UID_ROOT, 143 GID_WHEEL, 0666, "full"); 144 break; 145 146 case MOD_UNLOAD: 147 destroy_dev(null_dev); 148 destroy_dev(zero_dev); 149 destroy_dev(full_dev); 150 free(zbuf, M_TEMP); 151 break; 152 153 case MOD_SHUTDOWN: 154 break; 155 156 default: 157 return (EOPNOTSUPP); 158 } 159 160 return (0); 161 } 162 163 DEV_MODULE(null, null_modevent, NULL); 164