1 /* $NetBSD: pcons.c,v 1.18 2004/03/21 15:08:24 pk 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.18 2004/03/21 15:08:24 pk 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 53 #include <machine/autoconf.h> 54 #include <machine/openfirm.h> 55 #include <machine/cpu.h> 56 #include <machine/eeprom.h> 57 #include <machine/psl.h> 58 59 #include <dev/cons.h> 60 61 #include <sparc64/dev/cons.h> 62 63 static int pconsmatch __P((struct device *, struct cfdata *, void *)); 64 static void pconsattach __P((struct device *, struct device *, void *)); 65 66 CFATTACH_DECL(pcons, sizeof(struct pconssoftc), 67 pconsmatch, pconsattach, NULL, NULL); 68 69 extern struct cfdriver pcons_cd; 70 71 dev_type_open(pconsopen); 72 dev_type_close(pconsclose); 73 dev_type_read(pconsread); 74 dev_type_write(pconswrite); 75 dev_type_ioctl(pconsioctl); 76 dev_type_tty(pconstty); 77 dev_type_poll(pconspoll); 78 79 const struct cdevsw pcons_cdevsw = { 80 pconsopen, pconsclose, pconsread, pconswrite, pconsioctl, 81 nostop, pconstty, pconspoll, nommap, ttykqfilter, D_TTY 82 }; 83 84 static struct cnm_state pcons_cnm_state; 85 86 static int pconsprobe __P((void)); 87 extern struct consdev *cn_tab; 88 89 static int 90 pconsmatch(parent, match, aux) 91 struct device *parent; 92 struct cfdata *match; 93 void *aux; 94 { 95 struct mainbus_attach_args *ma = aux; 96 extern int prom_cngetc __P((dev_t)); 97 98 /* Only attach if no other console has attached. */ 99 return ((strcmp("pcons", ma->ma_name) == 0) && 100 (cn_tab->cn_getc == prom_cngetc)); 101 102 } 103 104 static void 105 pconsattach(parent, self, aux) 106 struct device *parent, *self; 107 void *aux; 108 { 109 struct pconssoftc *sc = (struct pconssoftc *) self; 110 111 printf("\n"); 112 if (!pconsprobe()) 113 return; 114 115 cn_init_magic(&pcons_cnm_state); 116 cn_set_magic("+++++"); 117 callout_init(&sc->sc_poll_ch); 118 } 119 120 static void pconsstart __P((struct tty *)); 121 static int pconsparam __P((struct tty *, struct termios *)); 122 static void pcons_poll __P((void *)); 123 124 int 125 pconsopen(dev, flag, mode, p) 126 dev_t dev; 127 int flag, mode; 128 struct proc *p; 129 { 130 struct pconssoftc *sc; 131 int unit = minor(dev); 132 struct tty *tp; 133 134 if (unit >= pcons_cd.cd_ndevs) 135 return ENXIO; 136 sc = pcons_cd.cd_devs[unit]; 137 if (!sc) 138 return ENXIO; 139 if (!(tp = sc->of_tty)) 140 sc->of_tty = tp = ttymalloc(); 141 tp->t_oproc = pconsstart; 142 tp->t_param = pconsparam; 143 tp->t_dev = dev; 144 cn_tab->cn_dev = dev; 145 if (!(tp->t_state & TS_ISOPEN)) { 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 } else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag)) 155 return EBUSY; 156 tp->t_state |= TS_CARR_ON; 157 158 if (!(sc->of_flags & OFPOLL)) { 159 sc->of_flags |= OFPOLL; 160 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc); 161 } 162 163 return (*tp->t_linesw->l_open)(dev, tp); 164 } 165 166 int 167 pconsclose(dev, flag, mode, p) 168 dev_t dev; 169 int flag, mode; 170 struct proc *p; 171 { 172 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)]; 173 struct tty *tp = sc->of_tty; 174 175 callout_stop(&sc->sc_poll_ch); 176 sc->of_flags &= ~OFPOLL; 177 (*tp->t_linesw->l_close)(tp, flag); 178 ttyclose(tp); 179 return 0; 180 } 181 182 int 183 pconsread(dev, uio, flag) 184 dev_t dev; 185 struct uio *uio; 186 int flag; 187 { 188 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)]; 189 struct tty *tp = sc->of_tty; 190 191 return (*tp->t_linesw->l_read)(tp, uio, flag); 192 } 193 194 int 195 pconswrite(dev, uio, flag) 196 dev_t dev; 197 struct uio *uio; 198 int flag; 199 { 200 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)]; 201 struct tty *tp = sc->of_tty; 202 203 return (*tp->t_linesw->l_write)(tp, uio, flag); 204 } 205 206 int 207 pconspoll(dev, events, p) 208 dev_t dev; 209 int events; 210 struct proc *p; 211 { 212 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)]; 213 struct tty *tp = sc->of_tty; 214 215 return ((*tp->t_linesw->l_poll)(tp, events, p)); 216 } 217 218 int 219 pconsioctl(dev, cmd, data, flag, p) 220 dev_t dev; 221 u_long cmd; 222 caddr_t data; 223 int flag; 224 struct proc *p; 225 { 226 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)]; 227 struct tty *tp = sc->of_tty; 228 int error; 229 230 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) != EPASSTHROUGH) 231 return error; 232 return ttioctl(tp, cmd, data, flag, p); 233 } 234 235 struct tty * 236 pconstty(dev) 237 dev_t dev; 238 { 239 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)]; 240 241 return sc->of_tty; 242 } 243 244 static void 245 pconsstart(tp) 246 struct tty *tp; 247 { 248 struct clist *cl; 249 int s, len; 250 u_char buf[OFBURSTLEN]; 251 252 s = spltty(); 253 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) { 254 splx(s); 255 return; 256 } 257 tp->t_state |= TS_BUSY; 258 splx(s); 259 cl = &tp->t_outq; 260 len = q_to_b(cl, buf, OFBURSTLEN); 261 prom_write(prom_stdout(), buf, len); 262 s = spltty(); 263 tp->t_state &= ~TS_BUSY; 264 if (cl->c_cc) { 265 tp->t_state |= TS_TIMEOUT; 266 callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp); 267 } 268 if (cl->c_cc <= tp->t_lowat) { 269 if (tp->t_state & TS_ASLEEP) { 270 tp->t_state &= ~TS_ASLEEP; 271 wakeup(cl); 272 } 273 selwakeup(&tp->t_wsel); 274 } 275 splx(s); 276 } 277 278 static int 279 pconsparam(tp, t) 280 struct tty *tp; 281 struct termios *t; 282 { 283 tp->t_ispeed = t->c_ispeed; 284 tp->t_ospeed = t->c_ospeed; 285 tp->t_cflag = t->c_cflag; 286 return 0; 287 } 288 289 static void 290 pcons_poll(aux) 291 void *aux; 292 { 293 struct pconssoftc *sc = aux; 294 struct tty *tp = sc->of_tty; 295 char ch; 296 297 while (prom_read(prom_stdin(), &ch, 1) > 0) { 298 cn_check_magic(tp->t_dev, ch, pcons_cnm_state); 299 if (tp && (tp->t_state & TS_ISOPEN)) 300 (*tp->t_linesw->l_rint)(ch, tp); 301 } 302 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc); 303 } 304 305 int 306 pconsprobe() 307 { 308 309 return (prom_stdin() && prom_stdout()); 310 } 311 312 void 313 pcons_cnpollc(dev, on) 314 dev_t dev; 315 int on; 316 { 317 struct pconssoftc *sc = NULL; 318 319 if (pcons_cd.cd_devs) 320 sc = pcons_cd.cd_devs[minor(dev)]; 321 322 if (on) { 323 if (!sc) return; 324 if (sc->of_flags & OFPOLL) 325 callout_stop(&sc->sc_poll_ch); 326 sc->of_flags &= ~OFPOLL; 327 } else { 328 /* Resuming kernel. */ 329 if (sc && !(sc->of_flags & OFPOLL)) { 330 sc->of_flags |= OFPOLL; 331 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc); 332 } 333 } 334 } 335 336 void pcons_dopoll __P((void)); 337 void 338 pcons_dopoll() { 339 pcons_poll((void*)pcons_cd.cd_devs[0]); 340 } 341