xref: /freebsd/sys/dev/null/null.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*-
2  * Copyright (c) 2000 Mark R. V. Murray & Jeroen C. van Gelderen
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/conf.h>
32 #include <sys/uio.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <machine/bus.h>
38 #include <machine/resource.h>
39 #include <sys/rman.h>
40 
41 /* For use with destroy_dev(9). */
42 static dev_t null_dev;
43 static dev_t zero_dev;
44 
45 static d_write_t null_write;
46 static d_read_t zero_read;
47 
48 #define CDEV_MAJOR	2
49 #define NULL_MINOR	2
50 #define ZERO_MINOR	12
51 
52 static struct cdevsw null_cdevsw = {
53 	/* open */	(d_open_t *)nullop,
54 	/* close */	(d_close_t *)nullop,
55 	/* read */	(d_read_t *)nullop,
56 	/* write */	null_write,
57 	/* ioctl */	noioctl,
58 	/* poll */	nopoll,
59 	/* mmap */	nommap,
60 	/* strategy */	nostrategy,
61 	/* name */	"null",
62 	/* maj */	CDEV_MAJOR,
63 	/* dump */	nodump,
64 	/* psize */	nopsize,
65 	/* flags */	0,
66 	/* bmaj */	-1
67 };
68 
69 static struct cdevsw zero_cdevsw = {
70 	/* open */	(d_open_t *)nullop,
71 	/* close */	(d_close_t *)nullop,
72 	/* read */	zero_read,
73 	/* write */	null_write,
74 	/* ioctl */	noioctl,
75 	/* poll */	nopoll,
76 	/* mmap */	nommap,
77 	/* strategy */	nostrategy,
78 	/* name */	"zero",
79 	/* maj */	CDEV_MAJOR,
80 	/* dump */	nodump,
81 	/* psize */	nopsize,
82 	/* flags */	D_MMAP_ANON,
83 	/* bmaj */	-1
84 };
85 
86 static void *zbuf;
87 
88 static int
89 null_write(dev_t dev, struct uio *uio, int flag)
90 {
91 	uio->uio_resid = 0;
92 	return 0;
93 }
94 
95 static int
96 zero_read(dev_t dev, struct uio *uio, int flag)
97 {
98 	u_int c;
99 	int error = 0;
100 
101 	while (uio->uio_resid > 0 && error == 0) {
102 		c = min(uio->uio_resid, PAGE_SIZE);
103 		error = uiomove(zbuf, c, uio);
104 	}
105 	return error;
106 }
107 
108 static int
109 null_modevent(module_t mod, int type, void *data)
110 {
111 	switch(type) {
112 	case MOD_LOAD:
113 		if (bootverbose)
114 			printf("null: <null device, zero device>\n");
115 		zbuf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
116 		bzero(zbuf, PAGE_SIZE);
117 		zero_dev = make_dev(&zero_cdevsw, ZERO_MINOR, UID_ROOT,
118 			GID_WHEEL, 0666, "zero");
119 		null_dev = make_dev(&null_cdevsw, NULL_MINOR, UID_ROOT,
120 			GID_WHEEL, 0666, "null");
121 		return 0;
122 
123 	case MOD_UNLOAD:
124 		destroy_dev(null_dev);
125 		destroy_dev(zero_dev);
126 		free(zbuf, M_TEMP);
127 		return 0;
128 
129 	case MOD_SHUTDOWN:
130 		return 0;
131 
132 	default:
133 		return EOPNOTSUPP;
134 	}
135 }
136 
137 DEV_MODULE(null, null_modevent, NULL);
138