Home | History | Annotate | Line # | Download | only in dev
pcons.c revision 1.3
      1 /*	$NetBSD: pcons.c,v 1.3 2002/09/27 20:36:19 thorpej 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/promlib.h>
     52 #include <machine/cpu.h>
     53 #include <machine/psl.h>
     54 
     55 #include <dev/cons.h>
     56 
     57 #include <sun2/dev/cons.h>
     58 
     59 static int pconsmatch __P((struct device *, struct cfdata *, void *));
     60 static void pconsattach __P((struct device *, struct device *, void *));
     61 
     62 const struct cfattach pcons_ca = {
     63 	sizeof(struct pconssoftc), pconsmatch, pconsattach
     64 };
     65 
     66 extern struct cfdriver pcons_cd;
     67 static struct cnm_state pcons_cnm_state;
     68 
     69 static int pconsprobe __P((void));
     70 extern struct consdev *cn_tab;
     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, D_TTY
     83 };
     84 
     85 static int
     86 pconsmatch(parent, match, aux)
     87 	struct device *parent;
     88 	struct cfdata *match;
     89 	void *aux;
     90 {
     91 	struct mainbus_attach_args *ma = aux;
     92 	extern int  prom_cngetc __P((dev_t));
     93 
     94 	/* Only attach if no other console has attached. */
     95 	return ((ma->ma_name != NULL) &&
     96 		(strcmp("pcons", ma->ma_name) == 0) &&
     97 		(cn_tab->cn_getc == prom_cngetc));
     98 
     99 }
    100 
    101 static void
    102 pconsattach(parent, self, aux)
    103 	struct device *parent, *self;
    104 	void *aux;
    105 {
    106 	struct pconssoftc *sc = (struct pconssoftc *) 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);
    115 }
    116 
    117 static void pconsstart __P((struct tty *));
    118 static int pconsparam __P((struct tty *, struct termios *));
    119 static void pcons_poll __P((void *));
    120 
    121 int
    122 pconsopen(dev, flag, mode, p)
    123 	dev_t dev;
    124 	int flag, mode;
    125 	struct proc *p;
    126 {
    127 	struct pconssoftc *sc;
    128 	int unit = minor(dev);
    129 	struct tty *tp;
    130 
    131 	if (unit >= pcons_cd.cd_ndevs)
    132 		return ENXIO;
    133 	sc = pcons_cd.cd_devs[unit];
    134 	if (!sc)
    135 		return ENXIO;
    136 	if (!(tp = sc->of_tty))
    137 		sc->of_tty = tp = ttymalloc();
    138 	tp->t_oproc = pconsstart;
    139 	tp->t_param = pconsparam;
    140 	tp->t_dev = dev;
    141 	cn_tab->cn_dev = dev;
    142 	if (!(tp->t_state & TS_ISOPEN)) {
    143 		ttychars(tp);
    144 		tp->t_iflag = TTYDEF_IFLAG;
    145 		tp->t_oflag = TTYDEF_OFLAG;
    146 		tp->t_cflag = TTYDEF_CFLAG;
    147 		tp->t_lflag = TTYDEF_LFLAG;
    148 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    149 		pconsparam(tp, &tp->t_termios);
    150 		ttsetwater(tp);
    151 	} else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
    152 		return EBUSY;
    153 	tp->t_state |= TS_CARR_ON;
    154 
    155 	if (!(sc->of_flags & OFPOLL)) {
    156 		sc->of_flags |= OFPOLL;
    157 		callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    158 	}
    159 
    160 	return (*tp->t_linesw->l_open)(dev, tp);
    161 }
    162 
    163 int
    164 pconsclose(dev, flag, mode, p)
    165 	dev_t dev;
    166 	int flag, mode;
    167 	struct proc *p;
    168 {
    169 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    170 	struct tty *tp = sc->of_tty;
    171 
    172 	callout_stop(&sc->sc_poll_ch);
    173 	sc->of_flags &= ~OFPOLL;
    174 	(*tp->t_linesw->l_close)(tp, flag);
    175 	ttyclose(tp);
    176 	return 0;
    177 }
    178 
    179 int
    180 pconsread(dev, uio, flag)
    181 	dev_t dev;
    182 	struct uio *uio;
    183 	int flag;
    184 {
    185 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    186 	struct tty *tp = sc->of_tty;
    187 
    188 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    189 }
    190 
    191 int
    192 pconswrite(dev, uio, flag)
    193 	dev_t dev;
    194 	struct uio *uio;
    195 	int flag;
    196 {
    197 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    198 	struct tty *tp = sc->of_tty;
    199 
    200 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    201 }
    202 
    203 int
    204 pconspoll(dev, events, p)
    205 	dev_t dev;
    206 	int events;
    207 	struct proc *p;
    208 {
    209 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    210 	struct tty *tp = sc->of_tty;
    211 
    212 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    213 }
    214 
    215 int
    216 pconsioctl(dev, cmd, data, flag, p)
    217 	dev_t dev;
    218 	u_long cmd;
    219 	caddr_t data;
    220 	int flag;
    221 	struct proc *p;
    222 {
    223 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    224 	struct tty *tp = sc->of_tty;
    225 	int error;
    226 
    227 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) >= 0)
    228 		return error;
    229 	if ((error = ttioctl(tp, cmd, data, flag, p)) >= 0)
    230 		return error;
    231 	return ENOTTY;
    232 }
    233 
    234 struct tty *
    235 pconstty(dev)
    236 	dev_t dev;
    237 {
    238 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    239 
    240 	return sc->of_tty;
    241 }
    242 
    243 static void
    244 pconsstart(tp)
    245 	struct tty *tp;
    246 {
    247 	struct clist *cl;
    248 	int s, len;
    249 	u_char buf[OFBURSTLEN];
    250 
    251 	s = spltty();
    252 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
    253 		splx(s);
    254 		return;
    255 	}
    256 	tp->t_state |= TS_BUSY;
    257 	splx(s);
    258 	cl = &tp->t_outq;
    259 	len = q_to_b(cl, buf, OFBURSTLEN);
    260 	prom_putstr(buf, len);
    261 	s = spltty();
    262 	tp->t_state &= ~TS_BUSY;
    263 	if (cl->c_cc) {
    264 		tp->t_state |= TS_TIMEOUT;
    265 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
    266 	}
    267 	if (cl->c_cc <= tp->t_lowat) {
    268 		if (tp->t_state & TS_ASLEEP) {
    269 			tp->t_state &= ~TS_ASLEEP;
    270 			wakeup(cl);
    271 		}
    272 		selwakeup(&tp->t_wsel);
    273 	}
    274 	splx(s);
    275 }
    276 
    277 static int
    278 pconsparam(tp, t)
    279 	struct tty *tp;
    280 	struct termios *t;
    281 {
    282 	tp->t_ispeed = t->c_ispeed;
    283 	tp->t_ospeed = t->c_ospeed;
    284 	tp->t_cflag = t->c_cflag;
    285 	return 0;
    286 }
    287 
    288 static void
    289 pcons_poll(aux)
    290 	void *aux;
    291 {
    292 	struct pconssoftc *sc = aux;
    293 	struct tty *tp = sc->of_tty;
    294 	int c;
    295 	char ch;
    296 
    297 	while ((c = prom_peekchar()) >= 0) {
    298 		ch = c;
    299 		cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
    300 		if (tp && (tp->t_state & TS_ISOPEN))
    301 			(*tp->t_linesw->l_rint)(ch, tp);
    302 	}
    303 	callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    304 }
    305 
    306 int
    307 pconsprobe()
    308 {
    309 	switch (prom_version()) {
    310 #ifdef	PROM_OLDMON
    311 	case PROM_OLDMON:
    312 	case PROM_OBP_V0:
    313 		return (1);
    314 #endif	/* PROM_OLDMON */
    315 #ifdef	PROM_OBP_V2
    316 	case PROM_OBP_V2:
    317 	case PROM_OBP_V3:
    318 	case PROM_OPENFIRM:
    319 		return (prom_stdin() && prom_stdout());
    320 #endif	/* PROM_OBP_V2 */
    321 	default: break;
    322 	}
    323 	return (0);
    324 }
    325 
    326 void
    327 pcons_cnpollc(dev, on)
    328 	dev_t dev;
    329 	int on;
    330 {
    331 	struct pconssoftc *sc = NULL;
    332 
    333 	if (pcons_cd.cd_devs)
    334 		sc = pcons_cd.cd_devs[minor(dev)];
    335 
    336 	if (on) {
    337 		if (!sc) return;
    338 		if (sc->of_flags & OFPOLL)
    339 			callout_stop(&sc->sc_poll_ch);
    340 		sc->of_flags &= ~OFPOLL;
    341 	} else {
    342                 /* Resuming kernel. */
    343 		if (sc && !(sc->of_flags & OFPOLL)) {
    344 			sc->of_flags |= OFPOLL;
    345 			callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    346 		}
    347 	}
    348 }
    349 
    350 void pcons_dopoll __P((void));
    351 void
    352 pcons_dopoll() {
    353 		pcons_poll((void*)pcons_cd.cd_devs[0]);
    354 }
    355