xref: /netbsd-src/sys/dev/nullcons_subr.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: nullcons_subr.c,v 1.10 2009/11/23 02:13:45 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: nullcons_subr.c,v 1.10 2009/11/23 02:13:45 rmind Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/proc.h>
34 #include <sys/systm.h>
35 #include <sys/buf.h>
36 #include <sys/ioctl.h>
37 #include <sys/tty.h>
38 #include <sys/file.h>
39 #include <sys/conf.h>
40 #include <sys/vnode.h>
41 
42 #include <dev/cons.h>
43 
44 static struct tty *nulltty;		/* null console tty */
45 
46 cons_decl(null);
47 
48 dev_type_read(nullcndev_read);
49 dev_type_ioctl(nullcndev_ioctl);
50 dev_type_tty(nullcndev_tty);
51 
52 static int	nullcons_newdev(struct consdev *);
53 
54 const struct cdevsw nullcn_devsw = {
55 	nullopen, nullclose, nullcndev_read, nullwrite, nullcndev_ioctl,
56 	nullstop, nullcndev_tty, nopoll, nommap, ttykqfilter, D_TTY
57 };
58 
59 /*
60  * null console device. We need it because of the ioctl() it handles,
61  * which in particular allows control terminal allocation through
62  * TIOCSCTTY ioctl. Without the latter, system won't even boot past init(8)
63  * invocation.
64  */
65 int
66 nullcndev_read(dev_t dev, struct uio *uio, int flag)
67 {
68 
69 	return EIO;
70 }
71 
72 int
73 nullcndev_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
74 {
75 	int error;
76 
77 	error = (*nulltty->t_linesw->l_ioctl)(nulltty, cmd, data, flag, l);
78 	if (error != EPASSTHROUGH)
79 		return error;
80 
81 	error = ttioctl(nulltty, cmd, data, flag, l);
82 	if (error != EPASSTHROUGH)
83 		return error;
84 
85 	return 0;
86 }
87 
88 struct tty*
89 nullcndev_tty(dev_t dev)
90 {
91 
92 	return nulltty;
93 }
94 
95 /*
96  * Mark console as no-op (null) console. Proper initialization is deferred
97  * to nullconsattach().
98  */
99 void
100 nullcnprobe(struct consdev *cn)
101 {
102 
103 	cn->cn_pri = CN_NULL;
104 	cn->cn_dev = NODEV;
105 }
106 
107 /*
108  * null console initialization. This includes allocation of a new device and
109  * a new tty.
110  */
111 void
112 nullcninit(struct consdev *cn)
113 {
114 	static struct consdev nullcn = cons_init(null);
115 
116 	nullcnprobe(&nullcn);
117 	cn_tab = &nullcn;
118 }
119 
120 /*
121  * Dumb getc() implementation. Simply blocks on call.
122  */
123 int
124 nullcngetc(dev_t dev)
125 {
126 
127 	for (;;)
128 		;
129 	return 0;
130 }
131 
132 /*
133  * Dumb putc() implementation.
134  */
135 void
136 nullcnputc(dev_t dev, int c)
137 {
138 
139 }
140 
141 /*
142  * Allocate a new console device and a tty to handle console ioctls.
143  */
144 int
145 nullcons_newdev(struct consdev *cn)
146 {
147 	int error;
148 	int bmajor = -1, cmajor = -1;
149 
150 	if ((cn == NULL) || (cn->cn_pri != CN_NULL) || (cn->cn_dev != NODEV))
151 		return 0;
152 
153 	/*
154 	 * Attach no-op device to the device list.
155 	 */
156 	error = devsw_attach("nullcn", NULL, &bmajor, &nullcn_devsw, &cmajor);
157 	if (error != 0)
158 		return error;
159 
160 	/*
161 	 * Allocate tty (mostly to have sane ioctl()).
162 	 */
163 	nulltty = ttymalloc();
164 	nulltty->t_dev = makedev(cmajor, 0);
165 	tty_attach(nulltty);
166 	cn->cn_dev = nulltty->t_dev;
167 
168 	return 0;
169 }
170 
171 /*
172  * Pseudo-device attach function -- it's the right time to do the rest of
173  * initialization.
174  */
175 void
176 nullconsattach(int pdev_count)
177 {
178 
179 	nullcons_newdev(cn_tab);
180 }
181