Home | History | Annotate | Line # | Download | only in dev
pcons.c revision 1.3
      1 /*	$NetBSD: pcons.c,v 1.3 2000/07/10 20:24:23 eeh 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 "opt_ddb.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/conf.h>
     41 #include <sys/device.h>
     42 #include <sys/file.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/kernel.h>
     45 #include <sys/proc.h>
     46 #include <sys/tty.h>
     47 #include <sys/time.h>
     48 #include <sys/syslog.h>
     49 
     50 #include <machine/autoconf.h>
     51 #include <machine/openfirm.h>
     52 #include <machine/bsd_openprom.h>
     53 #include <machine/conf.h>
     54 #include <machine/cpu.h>
     55 #include <machine/eeprom.h>
     56 #include <machine/psl.h>
     57 #include <machine/z8530var.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 struct cfattach pcons_ca = {
     67 	sizeof(struct pconssoftc), pconsmatch, pconsattach
     68 };
     69 
     70 extern struct cfdriver pcons_cd;
     71 
     72 static int pconsprobe __P((void));
     73 
     74 static int
     75 pconsmatch(parent, match, aux)
     76 	struct device *parent;
     77 	struct cfdata *match;
     78 	void *aux;
     79 {
     80 	struct mainbus_attach_args *ma = aux;
     81 	extern struct consdev *cn_tab;
     82 	extern int  prom_cngetc __P((dev_t));
     83 
     84 	/* Only attach if no other console has attached. */
     85 	return ((strcmp("pcons", ma->ma_name) == 0) &&
     86 		(cn_tab->cn_getc == prom_cngetc));
     87 
     88 }
     89 
     90 static void
     91 pconsattach(parent, self, aux)
     92 	struct device *parent, *self;
     93 	void *aux;
     94 {
     95 	struct pconssoftc *sc = (struct pconssoftc *) self;
     96 
     97 	printf("\n");
     98 	if (!pconsprobe())
     99 		return;
    100 
    101 	callout_init(&sc->sc_poll_ch);
    102 }
    103 
    104 static void pconsstart __P((struct tty *));
    105 static int pconsparam __P((struct tty *, struct termios *));
    106 static void pcons_poll __P((void *));
    107 
    108 int
    109 pconsopen(dev, flag, mode, p)
    110 	dev_t dev;
    111 	int flag, mode;
    112 	struct proc *p;
    113 {
    114 	struct pconssoftc *sc;
    115 	int unit = minor(dev);
    116 	struct tty *tp;
    117 
    118 	if (unit >= pcons_cd.cd_ndevs)
    119 		return ENXIO;
    120 	sc = pcons_cd.cd_devs[unit];
    121 	if (!sc)
    122 		return ENXIO;
    123 	if (!(tp = sc->of_tty))
    124 		sc->of_tty = tp = ttymalloc();
    125 	tp->t_oproc = pconsstart;
    126 	tp->t_param = pconsparam;
    127 	tp->t_dev = dev;
    128 	if (!(tp->t_state & TS_ISOPEN)) {
    129 		ttychars(tp);
    130 		tp->t_iflag = TTYDEF_IFLAG;
    131 		tp->t_oflag = TTYDEF_OFLAG;
    132 		tp->t_cflag = TTYDEF_CFLAG;
    133 		tp->t_lflag = TTYDEF_LFLAG;
    134 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    135 		pconsparam(tp, &tp->t_termios);
    136 		ttsetwater(tp);
    137 	} else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
    138 		return EBUSY;
    139 	tp->t_state |= TS_CARR_ON;
    140 
    141 	if (!(sc->of_flags & OFPOLL)) {
    142 		sc->of_flags |= OFPOLL;
    143 		callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    144 	}
    145 
    146 	return (*linesw[tp->t_line].l_open)(dev, tp);
    147 }
    148 
    149 int
    150 pconsclose(dev, flag, mode, p)
    151 	dev_t dev;
    152 	int flag, mode;
    153 	struct proc *p;
    154 {
    155 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    156 	struct tty *tp = sc->of_tty;
    157 
    158 	callout_stop(&sc->sc_poll_ch);
    159 	sc->of_flags &= ~OFPOLL;
    160 	(*linesw[tp->t_line].l_close)(tp, flag);
    161 	ttyclose(tp);
    162 	return 0;
    163 }
    164 
    165 int
    166 pconsread(dev, uio, flag)
    167 	dev_t dev;
    168 	struct uio *uio;
    169 	int flag;
    170 {
    171 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    172 	struct tty *tp = sc->of_tty;
    173 
    174 	return (*linesw[tp->t_line].l_read)(tp, uio, flag);
    175 }
    176 
    177 int
    178 pconswrite(dev, uio, flag)
    179 	dev_t dev;
    180 	struct uio *uio;
    181 	int flag;
    182 {
    183 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    184 	struct tty *tp = sc->of_tty;
    185 
    186 	return (*linesw[tp->t_line].l_write)(tp, uio, flag);
    187 }
    188 
    189 int
    190 pconsioctl(dev, cmd, data, flag, p)
    191 	dev_t dev;
    192 	u_long cmd;
    193 	caddr_t data;
    194 	int flag;
    195 	struct proc *p;
    196 {
    197 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    198 	struct tty *tp = sc->of_tty;
    199 	int error;
    200 
    201 	if ((error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p)) >= 0)
    202 		return error;
    203 	if ((error = ttioctl(tp, cmd, data, flag, p)) >= 0)
    204 		return error;
    205 	return ENOTTY;
    206 }
    207 
    208 struct tty *
    209 pconstty(dev)
    210 	dev_t dev;
    211 {
    212 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    213 
    214 	return sc->of_tty;
    215 }
    216 
    217 void
    218 pconsstop(tp, flag)
    219 	struct tty *tp;
    220 	int flag;
    221 {
    222 }
    223 
    224 static void
    225 pconsstart(tp)
    226 	struct tty *tp;
    227 {
    228 	struct clist *cl;
    229 	int s, len;
    230 	u_char buf[OFBURSTLEN];
    231 
    232 	s = spltty();
    233 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
    234 		splx(s);
    235 		return;
    236 	}
    237 	tp->t_state |= TS_BUSY;
    238 	splx(s);
    239 	cl = &tp->t_outq;
    240 	len = q_to_b(cl, buf, OFBURSTLEN);
    241 	OF_write(stdout, buf, len);
    242 	s = spltty();
    243 	tp->t_state &= ~TS_BUSY;
    244 	if (cl->c_cc) {
    245 		tp->t_state |= TS_TIMEOUT;
    246 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
    247 	}
    248 	if (cl->c_cc <= tp->t_lowat) {
    249 		if (tp->t_state & TS_ASLEEP) {
    250 			tp->t_state &= ~TS_ASLEEP;
    251 			wakeup(cl);
    252 		}
    253 		selwakeup(&tp->t_wsel);
    254 	}
    255 	splx(s);
    256 }
    257 
    258 static int
    259 pconsparam(tp, t)
    260 	struct tty *tp;
    261 	struct termios *t;
    262 {
    263 	tp->t_ispeed = t->c_ispeed;
    264 	tp->t_ospeed = t->c_ospeed;
    265 	tp->t_cflag = t->c_cflag;
    266 	return 0;
    267 }
    268 
    269 static void
    270 pcons_poll(aux)
    271 	void *aux;
    272 {
    273 	struct pconssoftc *sc = aux;
    274 	struct tty *tp = sc->of_tty;
    275 	char ch;
    276 #ifdef DDB
    277 static int nplus = 0;
    278 #endif
    279 
    280 
    281 	while (OF_read(stdin, &ch, 1) > 0) {
    282 #ifdef DDB
    283 		if (ch == '+') {
    284 			if (nplus++ > 3) {
    285 				extern int db_active;
    286 
    287 				if (!db_active)
    288 					Debugger();
    289 				else
    290 					/* Debugger is probably hozed */
    291 					callrom();
    292 			}
    293 		} else nplus = 0;
    294 #endif
    295 		if (tp && (tp->t_state & TS_ISOPEN))
    296 			(*linesw[tp->t_line].l_rint)(ch, tp);
    297 	}
    298 	callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    299 }
    300 
    301 int
    302 pconsprobe()
    303 {
    304 	if (!stdin) stdin = OF_stdin();
    305 	if (!stdout) stdout = OF_stdout();
    306 
    307 	return (stdin && stdout);
    308 }
    309 
    310 void
    311 pcons_cnpollc(dev, on)
    312 	dev_t dev;
    313 	int on;
    314 {
    315 	struct pconssoftc *sc = NULL;
    316 
    317 	if (pcons_cd.cd_devs)
    318 		sc = pcons_cd.cd_devs[minor(dev)];
    319 
    320 	if (on) {
    321 		if (!sc) return;
    322 		if (sc->of_flags & OFPOLL)
    323 			callout_stop(&sc->sc_poll_ch);
    324 		sc->of_flags &= ~OFPOLL;
    325 	} else {
    326                 /* Resuming kernel. */
    327 		if (sc && !(sc->of_flags & OFPOLL)) {
    328 			sc->of_flags |= OFPOLL;
    329 			callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    330 		}
    331 	}
    332 }
    333 
    334 void pcons_dopoll __P((void));
    335 void
    336 pcons_dopoll() {
    337 		pcons_poll((void*)pcons_cd.cd_devs[0]);
    338 }
    339