xref: /freebsd/sys/dev/null/null.c (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
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 };
67 
68 static struct cdevsw zero_cdevsw = {
69 	/* open */	(d_open_t *)nullop,
70 	/* close */	(d_close_t *)nullop,
71 	/* read */	zero_read,
72 	/* write */	null_write,
73 	/* ioctl */	noioctl,
74 	/* poll */	nopoll,
75 	/* mmap */	nommap,
76 	/* strategy */	nostrategy,
77 	/* name */	"zero",
78 	/* maj */	CDEV_MAJOR,
79 	/* dump */	nodump,
80 	/* psize */	nopsize,
81 	/* flags */	D_MMAP_ANON,
82 };
83 
84 static void *zbuf;
85 
86 static int
87 null_write(dev_t dev, struct uio *uio, int flag)
88 {
89 	uio->uio_resid = 0;
90 	return 0;
91 }
92 
93 static int
94 zero_read(dev_t dev, struct uio *uio, int flag)
95 {
96 	u_int c;
97 	int error = 0;
98 
99 	while (uio->uio_resid > 0 && error == 0) {
100 		c = min(uio->uio_resid, PAGE_SIZE);
101 		error = uiomove(zbuf, c, uio);
102 	}
103 	return error;
104 }
105 
106 static int
107 null_modevent(module_t mod, int type, void *data)
108 {
109 	switch(type) {
110 	case MOD_LOAD:
111 		if (bootverbose)
112 			printf("null: <null device, zero device>\n");
113 		zbuf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK | M_ZERO);
114 		zero_dev = make_dev(&zero_cdevsw, ZERO_MINOR, UID_ROOT,
115 			GID_WHEEL, 0666, "zero");
116 		null_dev = make_dev(&null_cdevsw, NULL_MINOR, UID_ROOT,
117 			GID_WHEEL, 0666, "null");
118 		return 0;
119 
120 	case MOD_UNLOAD:
121 		destroy_dev(null_dev);
122 		destroy_dev(zero_dev);
123 		free(zbuf, M_TEMP);
124 		return 0;
125 
126 	case MOD_SHUTDOWN:
127 		return 0;
128 
129 	default:
130 		return EOPNOTSUPP;
131 	}
132 }
133 
134 DEV_MODULE(null, null_modevent, NULL);
135