1 /* $NetBSD: nullcons_subr.c,v 1.8 2009/03/14 15:36:16 dsl 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.8 2009/03/14 15:36:16 dsl Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/proc.h> 34 #include <sys/user.h> 35 #include <sys/systm.h> 36 #include <sys/buf.h> 37 #include <sys/ioctl.h> 38 #include <sys/tty.h> 39 #include <sys/file.h> 40 #include <sys/conf.h> 41 #include <sys/vnode.h> 42 43 #include <dev/cons.h> 44 45 46 extern struct consdev *cn_tab; /* physical console device info */ 47 48 static struct tty *nulltty; /* null console tty */ 49 50 cons_decl(null); 51 52 dev_type_read(nullcndev_read); 53 dev_type_ioctl(nullcndev_ioctl); 54 dev_type_tty(nullcndev_tty); 55 56 static int nullcons_newdev(struct consdev *); 57 58 const struct cdevsw nullcn_devsw = { 59 nullopen, nullclose, nullcndev_read, nullwrite, nullcndev_ioctl, 60 nullstop, nullcndev_tty, nopoll, nommap, ttykqfilter, D_TTY 61 }; 62 63 /* 64 * null console device. We need it because of the ioctl() it handles, 65 * which in particular allows control terminal allocation through 66 * TIOCSCTTY ioctl. Without the latter, system won't even boot past init(8) 67 * invocation. 68 */ 69 int 70 nullcndev_read(dev_t dev, struct uio *uio, int flag) 71 { 72 73 return EIO; 74 } 75 76 int 77 nullcndev_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 78 { 79 int error; 80 81 error = (*nulltty->t_linesw->l_ioctl)(nulltty, cmd, data, flag, l); 82 if (error != EPASSTHROUGH) 83 return (error); 84 85 error = ttioctl(nulltty, cmd, data, flag, l); 86 if (error != EPASSTHROUGH) 87 return (error); 88 89 return (0); 90 } 91 92 struct tty* 93 nullcndev_tty(dev_t dev) 94 { 95 96 return nulltty; 97 } 98 99 /* 100 * Mark console as no-op (null) console. Proper initialization is deferred 101 * to nullconsattach(). 102 */ 103 void 104 nullcnprobe(struct consdev *cn) 105 { 106 107 cn->cn_pri = CN_NULL; 108 cn->cn_dev = NODEV; 109 } 110 111 /* 112 * null console initialization. This includes allocation of a new device and 113 * a new tty. 114 */ 115 void 116 nullcninit(struct consdev *cn) 117 { 118 static struct consdev nullcn = cons_init(null); 119 120 nullcnprobe(&nullcn); 121 cn_tab = &nullcn; 122 } 123 124 /* 125 * Dumb getc() implementation. Simply blocks on call. 126 */ 127 int 128 nullcngetc(dev_t dev) 129 { 130 131 for(;;); 132 return (0); 133 } 134 135 /* 136 * Dumb putc() implementation. 137 */ 138 void 139 nullcnputc(dev_t dev, int c) 140 { 141 142 } 143 144 /* 145 * Allocate a new console device and a tty to handle console ioctls. 146 */ 147 int 148 nullcons_newdev(struct consdev *cn) 149 { 150 int error; 151 int bmajor = -1, cmajor = -1; 152 153 if ((cn == NULL) || (cn->cn_pri != CN_NULL) || (cn->cn_dev != NODEV)) 154 return (0); 155 156 /* 157 * Attach no-op device to the device list. 158 */ 159 error = devsw_attach("nullcn", NULL, &bmajor, &nullcn_devsw, &cmajor); 160 if (error != 0) 161 return (error); 162 163 /* 164 * Allocate tty (mostly to have sane ioctl()). 165 */ 166 nulltty = ttymalloc(); 167 nulltty->t_dev = makedev(cmajor, 0); 168 tty_attach(nulltty); 169 cn->cn_dev = nulltty->t_dev; 170 171 return (0); 172 } 173 174 /* 175 * Pseudo-device attach function -- it's the right time to do the rest of 176 * initialization. 177 */ 178 void 179 nullconsattach(int pdev_count) 180 { 181 182 nullcons_newdev(cn_tab); 183 } 184