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