1 /* $NetBSD: nullcons_subr.c,v 1.13 2014/07/25 08:10:35 dholland 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.13 2014/07/25 08:10:35 dholland 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 .d_open = nullopen, 56 .d_close = nullclose, 57 .d_read = nullcndev_read, 58 .d_write = nullwrite, 59 .d_ioctl = nullcndev_ioctl, 60 .d_stop = nullstop, 61 .d_tty = nullcndev_tty, 62 .d_poll = nopoll, 63 .d_mmap = nommap, 64 .d_kqfilter = ttykqfilter, 65 .d_discard = nodiscard, 66 .d_flag = D_TTY 67 }; 68 69 /* 70 * null console device. We need it because of the ioctl() it handles, 71 * which in particular allows control terminal allocation through 72 * TIOCSCTTY ioctl. Without the latter, system won't even boot past init(8) 73 * invocation. 74 */ 75 int 76 nullcndev_read(dev_t dev, struct uio *uio, int flag) 77 { 78 79 return EIO; 80 } 81 82 int 83 nullcndev_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 84 { 85 int error; 86 87 error = (*nulltty->t_linesw->l_ioctl)(nulltty, cmd, data, flag, l); 88 if (error != EPASSTHROUGH) 89 return error; 90 91 error = ttioctl(nulltty, cmd, data, flag, l); 92 if (error != EPASSTHROUGH) 93 return error; 94 95 return 0; 96 } 97 98 struct tty* 99 nullcndev_tty(dev_t dev) 100 { 101 102 return nulltty; 103 } 104 105 /* 106 * Mark console as no-op (null) console. Proper initialization is deferred 107 * to nullconsattach(). 108 */ 109 void 110 nullcnprobe(struct consdev *cn) 111 { 112 113 cn->cn_pri = CN_NULL; 114 cn->cn_dev = NODEV; 115 } 116 117 /* 118 * null console initialization. This includes allocation of a new device and 119 * a new tty. 120 */ 121 void 122 nullcninit(struct consdev *cn) 123 { 124 static struct consdev nullcn = cons_init(null); 125 126 nullcnprobe(&nullcn); 127 cn_tab = &nullcn; 128 } 129 130 /* 131 * Dumb getc() implementation. Simply blocks on call. 132 */ 133 int 134 nullcngetc(dev_t dev) 135 { 136 137 for (;;) 138 ; 139 return 0; 140 } 141 142 /* 143 * Dumb putc() implementation. 144 */ 145 void 146 nullcnputc(dev_t dev, int c) 147 { 148 149 } 150 151 /* 152 * Allocate a new console device and a tty to handle console ioctls. 153 */ 154 int 155 nullcons_newdev(struct consdev *cn) 156 { 157 int error; 158 int bmajor = -1, cmajor = -1; 159 160 if ((cn == NULL) || (cn->cn_pri != CN_NULL) || (cn->cn_dev != NODEV)) 161 return 0; 162 163 /* 164 * Attach no-op device to the device list. 165 */ 166 error = devsw_attach("nullcn", NULL, &bmajor, &nullcn_devsw, &cmajor); 167 if (error != 0) 168 return error; 169 170 /* 171 * Allocate tty (mostly to have sane ioctl()). 172 */ 173 nulltty = tty_alloc(); 174 nulltty->t_dev = makedev(cmajor, 0); 175 tty_attach(nulltty); 176 cn->cn_dev = nulltty->t_dev; 177 178 return 0; 179 } 180 181 /* 182 * Pseudo-device attach function -- it's the right time to do the rest of 183 * initialization. 184 */ 185 void 186 nullconsattach(int pdev_count) 187 { 188 189 nullcons_newdev(cn_tab); 190 } 191