1 /* $NetBSD: pcons.c,v 1.21 2014/03/16 05:20:26 dholland Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 Eduardo E. Horvath 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 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 /* 32 * Default console driver. Uses the PROM or whatever 33 * driver(s) are appropriate. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: pcons.c,v 1.21 2014/03/16 05:20:26 dholland Exp $"); 38 39 #include "opt_ddb.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/conf.h> 44 #include <sys/device.h> 45 #include <sys/file.h> 46 #include <sys/ioctl.h> 47 #include <sys/kernel.h> 48 #include <sys/proc.h> 49 #include <sys/tty.h> 50 #include <sys/time.h> 51 #include <sys/syslog.h> 52 #include <sys/kauth.h> 53 54 #include <machine/autoconf.h> 55 #include <machine/promlib.h> 56 #include <machine/cpu.h> 57 #include <machine/psl.h> 58 59 #include <dev/cons.h> 60 61 #include <sun2/dev/cons.h> 62 63 #include "ioconf.h" 64 65 static int pconsmatch(device_t, cfdata_t, void *); 66 static void pconsattach(device_t, device_t, void *); 67 68 CFATTACH_DECL_NEW(pcons, sizeof(struct pconssoftc), 69 pconsmatch, pconsattach, NULL, NULL); 70 71 static struct cnm_state pcons_cnm_state; 72 73 static int pconsprobe(void); 74 75 dev_type_open(pconsopen); 76 dev_type_close(pconsclose); 77 dev_type_read(pconsread); 78 dev_type_write(pconswrite); 79 dev_type_ioctl(pconsioctl); 80 dev_type_tty(pconstty); 81 dev_type_poll(pconspoll); 82 83 const struct cdevsw pcons_cdevsw = { 84 .d_open = pconsopen, 85 .d_close = pconsclose, 86 .d_read = pconsread, 87 .d_write = pconswrite, 88 .d_ioctl = pconsioctl, 89 .d_stop = nostop, 90 .d_tty = pconstty, 91 .d_poll = pconspoll, 92 .d_mmap = nommap, 93 .d_kqfilter = ttykqfilter, 94 .d_flag = D_TTY 95 }; 96 97 static int 98 pconsmatch(device_t parent, cfdata_t cf, void *aux) 99 { 100 struct mainbus_attach_args *ma = aux; 101 extern int prom_cngetc(dev_t); 102 103 /* Only attach if no other console has attached. */ 104 return (ma->ma_name != NULL) && 105 (strcmp("pcons", ma->ma_name) == 0) && 106 (cn_tab->cn_getc == prom_cngetc); 107 108 } 109 110 static void 111 pconsattach(device_t parent, device_t self, void *aux) 112 { 113 struct pconssoftc *sc = device_private(self); 114 115 aprint_normal("\n"); 116 if (!pconsprobe()) 117 return; 118 119 cn_init_magic(&pcons_cnm_state); 120 cn_set_magic("+++++"); 121 callout_init(&sc->sc_poll_ch, 0); 122 } 123 124 static void pconsstart(struct tty *); 125 static int pconsparam(struct tty *, struct termios *); 126 static void pcons_poll(void *); 127 128 int 129 pconsopen(dev_t dev, int flag, int mode, struct lwp *l) 130 { 131 struct pconssoftc *sc; 132 struct tty *tp; 133 134 sc = device_lookup_private(&pcons_cd, minor(dev)); 135 if (sc == NULL) 136 return ENXIO; 137 if ((tp = sc->of_tty) == NULL) 138 sc->of_tty = tp = tty_alloc(); 139 tp->t_oproc = pconsstart; 140 tp->t_param = pconsparam; 141 tp->t_dev = dev; 142 cn_tab->cn_dev = dev; 143 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 144 return (EBUSY); 145 if ((tp->t_state & TS_ISOPEN) == 0) { 146 ttychars(tp); 147 tp->t_iflag = TTYDEF_IFLAG; 148 tp->t_oflag = TTYDEF_OFLAG; 149 tp->t_cflag = TTYDEF_CFLAG; 150 tp->t_lflag = TTYDEF_LFLAG; 151 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 152 pconsparam(tp, &tp->t_termios); 153 ttsetwater(tp); 154 } 155 tp->t_state |= TS_CARR_ON; 156 157 if ((sc->of_flags & OFPOLL) == 0) { 158 sc->of_flags |= OFPOLL; 159 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc); 160 } 161 162 return (*tp->t_linesw->l_open)(dev, tp); 163 } 164 165 int 166 pconsclose(dev_t dev, int flag, int mode, struct lwp *l) 167 { 168 struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev)); 169 struct tty *tp = sc->of_tty; 170 171 callout_stop(&sc->sc_poll_ch); 172 sc->of_flags &= ~OFPOLL; 173 (*tp->t_linesw->l_close)(tp, flag); 174 ttyclose(tp); 175 return 0; 176 } 177 178 int 179 pconsread(dev_t dev, struct uio *uio, int flag) 180 { 181 struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev)); 182 struct tty *tp = sc->of_tty; 183 184 return (*tp->t_linesw->l_read)(tp, uio, flag); 185 } 186 187 int 188 pconswrite(dev_t dev, struct uio *uio, int flag) 189 { 190 struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev)); 191 struct tty *tp = sc->of_tty; 192 193 return (*tp->t_linesw->l_write)(tp, uio, flag); 194 } 195 196 int 197 pconspoll(dev_t dev, int events, struct lwp *l) 198 { 199 struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev)); 200 struct tty *tp = sc->of_tty; 201 202 return (*tp->t_linesw->l_poll)(tp, events, l); 203 } 204 205 int 206 pconsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 207 { 208 struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev)); 209 struct tty *tp = sc->of_tty; 210 int error; 211 212 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) >= 0) 213 return error; 214 if ((error = ttioctl(tp, cmd, data, flag, l)) >= 0) 215 return error; 216 return ENOTTY; 217 } 218 219 struct tty * 220 pconstty(dev_t dev) 221 { 222 struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev)); 223 224 return sc->of_tty; 225 } 226 227 static void 228 pconsstart(struct tty *tp) 229 { 230 struct clist *cl; 231 int s, len; 232 uint8_t buf[OFBURSTLEN]; 233 234 s = spltty(); 235 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) { 236 splx(s); 237 return; 238 } 239 tp->t_state |= TS_BUSY; 240 splx(s); 241 cl = &tp->t_outq; 242 len = q_to_b(cl, buf, OFBURSTLEN); 243 prom_putstr(buf, len); 244 s = spltty(); 245 tp->t_state &= ~TS_BUSY; 246 if (ttypull(tp)) { 247 tp->t_state |= TS_TIMEOUT; 248 callout_schedule(&tp->t_rstrt_ch, 1); 249 } 250 splx(s); 251 } 252 253 static int 254 pconsparam(struct tty *tp, struct termios *t) 255 { 256 257 tp->t_ispeed = t->c_ispeed; 258 tp->t_ospeed = t->c_ospeed; 259 tp->t_cflag = t->c_cflag; 260 return 0; 261 } 262 263 static void 264 pcons_poll(void *aux) 265 { 266 struct pconssoftc *sc = aux; 267 struct tty *tp = sc->of_tty; 268 int c; 269 char ch; 270 271 while ((c = prom_peekchar()) >= 0) { 272 ch = c; 273 cn_check_magic(tp->t_dev, ch, pcons_cnm_state); 274 if (tp && (tp->t_state & TS_ISOPEN)) 275 (*tp->t_linesw->l_rint)(ch, tp); 276 } 277 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc); 278 } 279 280 int 281 pconsprobe(void) 282 { 283 284 switch (prom_version()) { 285 #ifdef PROM_OLDMON 286 case PROM_OLDMON: 287 case PROM_OBP_V0: 288 return 1; 289 #endif /* PROM_OLDMON */ 290 #ifdef PROM_OBP_V2 291 case PROM_OBP_V2: 292 case PROM_OBP_V3: 293 case PROM_OPENFIRM: 294 return prom_stdin() && prom_stdout(); 295 #endif /* PROM_OBP_V2 */ 296 default: 297 break; 298 } 299 return 0; 300 } 301 302 void 303 pcons_cnpollc(dev_t dev, int on) 304 { 305 struct pconssoftc *sc; 306 307 sc = device_lookup_private(&pcons_cd, minor(dev)); 308 if (sc == NULL) 309 return; 310 311 if (on) { 312 if (sc->of_flags & OFPOLL) 313 callout_stop(&sc->sc_poll_ch); 314 sc->of_flags &= ~OFPOLL; 315 } else { 316 /* Resuming kernel. */ 317 if ((sc->of_flags & OFPOLL) == 0) { 318 sc->of_flags |= OFPOLL; 319 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc); 320 } 321 } 322 } 323 324 void pcons_dopoll(void); 325 326 void 327 pcons_dopoll(void) 328 { 329 330 pcons_poll(device_lookup_private(&pcons_cd, 0)); 331 } 332