1 /* $NetBSD: pcons.c,v 1.32 2013/09/15 09:25:22 martin 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.32 2013/09/15 09:25:22 martin 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/openfirm.h> 56 #include <machine/cpu.h> 57 #include <machine/eeprom.h> 58 #include <machine/psl.h> 59 60 #include <dev/cons.h> 61 62 #include <sparc64/dev/cons.h> 63 64 static int pconsmatch(device_t, cfdata_t, void *); 65 static void pconsattach(device_t, device_t, void *); 66 67 CFATTACH_DECL_NEW(pcons, sizeof(struct pconssoftc), 68 pconsmatch, pconsattach, NULL, NULL); 69 70 extern struct cfdriver pcons_cd; 71 72 dev_type_open(pconsopen); 73 dev_type_close(pconsclose); 74 dev_type_read(pconsread); 75 dev_type_write(pconswrite); 76 dev_type_ioctl(pconsioctl); 77 dev_type_tty(pconstty); 78 dev_type_poll(pconspoll); 79 80 const struct cdevsw pcons_cdevsw = { 81 pconsopen, pconsclose, pconsread, pconswrite, pconsioctl, 82 nostop, pconstty, pconspoll, nommap, ttykqfilter, D_TTY 83 }; 84 85 static struct cnm_state pcons_cnm_state; 86 87 static int pconsprobe(void); 88 extern struct consdev *cn_tab; 89 90 static int 91 pconsmatch(device_t parent, cfdata_t match, void *aux) 92 { 93 struct mainbus_attach_args *ma = aux; 94 extern int prom_cngetc(dev_t); 95 96 /* Only attach if no other console has attached. */ 97 return ((strcmp("pcons", ma->ma_name) == 0) && 98 (cn_tab->cn_getc == prom_cngetc)); 99 100 } 101 102 static void 103 pconsattach(device_t parent, device_t self, void *aux) 104 { 105 struct pconssoftc *sc = device_private(self); 106 sc->of_dev = self; 107 108 printf("\n"); 109 if (!pconsprobe()) 110 return; 111 112 cn_init_magic(&pcons_cnm_state); 113 cn_set_magic("+++++"); 114 callout_init(&sc->sc_poll_ch, 0); 115 } 116 117 static void pconsstart(struct tty *); 118 static int pconsparam(struct tty *, struct termios *); 119 static void pcons_poll(void *); 120 121 int 122 pconsopen(dev_t dev, int flag, int mode, struct lwp *l) 123 { 124 struct pconssoftc *sc; 125 struct tty *tp; 126 127 sc = device_lookup_private(&pcons_cd, minor(dev)); 128 if (!sc) 129 return ENXIO; 130 if (!(tp = sc->of_tty)) 131 sc->of_tty = tp = tty_alloc(); 132 tp->t_oproc = pconsstart; 133 tp->t_param = pconsparam; 134 tp->t_dev = dev; 135 cn_tab->cn_dev = dev; 136 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 137 return (EBUSY); 138 if (!(tp->t_state & TS_ISOPEN)) { 139 ttychars(tp); 140 tp->t_iflag = TTYDEF_IFLAG; 141 tp->t_oflag = TTYDEF_OFLAG; 142 tp->t_cflag = TTYDEF_CFLAG; 143 tp->t_lflag = TTYDEF_LFLAG; 144 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 145 pconsparam(tp, &tp->t_termios); 146 ttsetwater(tp); 147 } 148 tp->t_state |= TS_CARR_ON; 149 150 if (!(sc->of_flags & OFPOLL)) { 151 sc->of_flags |= OFPOLL; 152 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc); 153 } 154 155 return (*tp->t_linesw->l_open)(dev, tp); 156 } 157 158 int 159 pconsclose(dev_t dev, int flag, int mode, struct lwp *l) 160 { 161 struct pconssoftc *sc = device_lookup_private(&pcons_cd,minor(dev)); 162 struct tty *tp = sc->of_tty; 163 164 callout_stop(&sc->sc_poll_ch); 165 sc->of_flags &= ~OFPOLL; 166 (*tp->t_linesw->l_close)(tp, flag); 167 ttyclose(tp); 168 return 0; 169 } 170 171 int 172 pconsread(dev_t dev, struct uio *uio, int flag) 173 { 174 struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev)); 175 struct tty *tp = sc->of_tty; 176 177 return (*tp->t_linesw->l_read)(tp, uio, flag); 178 } 179 180 int 181 pconswrite(dev_t dev, struct uio *uio, int flag) 182 { 183 struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev)); 184 struct tty *tp = sc->of_tty; 185 186 return (*tp->t_linesw->l_write)(tp, uio, flag); 187 } 188 189 int 190 pconspoll(dev_t dev, int events, struct lwp *l) 191 { 192 struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev)); 193 struct tty *tp = sc->of_tty; 194 195 return ((*tp->t_linesw->l_poll)(tp, events, l)); 196 } 197 198 int 199 pconsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 200 { 201 struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev)); 202 struct tty *tp = sc->of_tty; 203 int error; 204 205 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH) 206 return error; 207 return ttioctl(tp, cmd, data, flag, l); 208 } 209 210 struct tty * 211 pconstty(dev_t dev) 212 { 213 struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev)); 214 215 return sc->of_tty; 216 } 217 218 static void 219 pconsstart(struct tty *tp) 220 { 221 struct clist *cl; 222 int s, len; 223 u_char buf[OFBURSTLEN]; 224 225 s = spltty(); 226 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) { 227 splx(s); 228 return; 229 } 230 tp->t_state |= TS_BUSY; 231 splx(s); 232 cl = &tp->t_outq; 233 len = q_to_b(cl, buf, OFBURSTLEN); 234 prom_write(prom_stdout(), buf, len); 235 s = spltty(); 236 tp->t_state &= ~TS_BUSY; 237 if (ttypull(tp)) { 238 tp->t_state |= TS_TIMEOUT; 239 callout_schedule(&tp->t_rstrt_ch, 1); 240 } 241 splx(s); 242 } 243 244 static int 245 pconsparam(struct tty *tp, struct termios *t) 246 { 247 tp->t_ispeed = t->c_ispeed; 248 tp->t_ospeed = t->c_ospeed; 249 tp->t_cflag = t->c_cflag; 250 return 0; 251 } 252 253 static void 254 pcons_poll(void *aux) 255 { 256 struct pconssoftc *sc = aux; 257 struct tty *tp = sc->of_tty; 258 char ch; 259 260 while (prom_read(prom_stdin(), &ch, 1) > 0) { 261 cn_check_magic(tp->t_dev, ch, pcons_cnm_state); 262 if (tp && (tp->t_state & TS_ISOPEN)) 263 (*tp->t_linesw->l_rint)(ch, tp); 264 } 265 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc); 266 } 267 268 int 269 pconsprobe(void) 270 { 271 272 return (prom_stdin() && prom_stdout()); 273 } 274 275 void 276 pcons_cnpollc(dev_t dev, int on) 277 { 278 struct pconssoftc *sc; 279 280 sc = device_lookup_private(&pcons_cd, minor(dev)); 281 if (sc == NULL) 282 return; 283 284 if (on) { 285 if (sc->of_flags & OFPOLL) 286 callout_stop(&sc->sc_poll_ch); 287 sc->of_flags &= ~OFPOLL; 288 } else { 289 /* Resuming kernel. */ 290 if (!(sc->of_flags & OFPOLL)) { 291 sc->of_flags |= OFPOLL; 292 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc); 293 } 294 } 295 } 296 297 void pcons_dopoll(void); 298 void 299 pcons_dopoll(void) 300 { 301 pcons_poll(device_lookup_private(&pcons_cd, 0)); 302 } 303