Home | History | Annotate | Line # | Download | only in dev
siotty.c revision 1.47
      1 /* $NetBSD: siotty.c,v 1.47 2020/12/29 17:17:14 tsutsui Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tohru Nishimura.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     33 
     34 __KERNEL_RCSID(0, "$NetBSD: siotty.c,v 1.47 2020/12/29 17:17:14 tsutsui Exp $");
     35 
     36 #include "opt_ddb.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/device.h>
     41 #include <sys/conf.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/proc.h>
     44 #include <sys/tty.h>
     45 #include <sys/uio.h>
     46 #include <sys/callout.h>
     47 #include <sys/fcntl.h>
     48 #include <dev/cons.h>
     49 #include <sys/kauth.h>
     50 #include <sys/kmem.h>
     51 
     52 #include <machine/cpu.h>
     53 
     54 #include <luna68k/dev/sioreg.h>
     55 #include <luna68k/dev/siovar.h>
     56 #include <luna68k/dev/syscn.h>
     57 
     58 #include "ioconf.h"
     59 
     60 #define	TIOCM_BREAK 01000 /* non standard use */
     61 
     62 static const uint8_t ch0_regs[6] = {
     63 	WR0_RSTINT,				/* reset E/S interrupt */
     64 	WR1_RXALLS | WR1_TXENBL,		/* Rx per char, Tx */
     65 	0,					/* */
     66 	WR3_RX8BIT | WR3_RXENBL,		/* Rx */
     67 	WR4_BAUD96 | WR4_STOP1,			/* Tx/Rx */
     68 	WR5_TX8BIT | WR5_TXENBL | WR5_DTR | WR5_RTS, /* Tx */
     69 };
     70 
     71 static const struct speedtab siospeedtab[] = {
     72 	{ 2400,	WR4_BAUD24, },
     73 	{ 4800,	WR4_BAUD48, },
     74 	{ 9600,	WR4_BAUD96, },
     75 	{ -1,	0, },
     76 };
     77 
     78 struct siotty_softc {
     79 	device_t	sc_dev;
     80 	struct tty	*sc_tty;
     81 	struct sioreg	*sc_ctl;
     82 	u_int		sc_flags;
     83 	uint8_t		sc_wr[6];
     84 	void		*sc_si;		/* software interrupt handler */
     85 	u_int		sc_hwflags;
     86 #define	SIOTTY_HW_CONSOLE	0x0001
     87 
     88 	uint8_t		*sc_rbuf;
     89 	uint8_t		*sc_rbufend;
     90 	uint8_t	* volatile sc_rbget;
     91 	uint8_t	* volatile sc_rbput;
     92 	volatile u_int	sc_rbavail;
     93 
     94 	uint8_t		*sc_tba;
     95 	u_int		sc_tbc;
     96 
     97 	bool		sc_rx_ready;
     98 	bool		sc_tx_busy;
     99 	bool		sc_tx_done;
    100 };
    101 
    102 #define	SIOTTY_RING_SIZE	2048
    103 u_int siotty_rbuf_size = SIOTTY_RING_SIZE;
    104 
    105 static struct cnm_state	siotty_cnm_state;
    106 
    107 #include "siotty.h"
    108 static void siostart(struct tty *);
    109 static int  sioparam(struct tty *, struct termios *);
    110 static void siottyintr(void *);
    111 static void siottysoft(void *);
    112 static void siotty_rxsoft(struct siotty_softc *, struct tty *);
    113 static void siotty_txsoft(struct siotty_softc *, struct tty *);
    114 static int  siomctl(struct siotty_softc *, int, int);
    115 
    116 static int  siotty_match(device_t, cfdata_t, void *);
    117 static void siotty_attach(device_t, device_t, void *);
    118 
    119 CFATTACH_DECL_NEW(siotty, sizeof(struct siotty_softc),
    120     siotty_match, siotty_attach, NULL, NULL);
    121 
    122 static dev_type_open(sioopen);
    123 static dev_type_close(sioclose);
    124 static dev_type_read(sioread);
    125 static dev_type_write(siowrite);
    126 static dev_type_ioctl(sioioctl);
    127 static dev_type_stop(siostop);
    128 static dev_type_tty(siotty);
    129 static dev_type_poll(siopoll);
    130 
    131 const struct cdevsw siotty_cdevsw = {
    132 	.d_open = sioopen,
    133 	.d_close = sioclose,
    134 	.d_read = sioread,
    135 	.d_write = siowrite,
    136 	.d_ioctl = sioioctl,
    137 	.d_stop = siostop,
    138 	.d_tty = siotty,
    139 	.d_poll = siopoll,
    140 	.d_mmap = nommap,
    141 	.d_kqfilter = ttykqfilter,
    142 	.d_discard = nodiscard,
    143 	.d_flag = D_TTY
    144 };
    145 
    146 static int
    147 siotty_match(device_t parent, cfdata_t cf, void *aux)
    148 {
    149 	struct sio_attach_args *args = aux;
    150 
    151 	if (args->channel != 0) /* XXX allow tty on Ch.B XXX */
    152 		return 0;
    153 	return 1;
    154 }
    155 
    156 static void
    157 siotty_attach(device_t parent, device_t self, void *aux)
    158 {
    159 	struct sio_softc *siosc = device_private(parent);
    160 	struct siotty_softc *sc = device_private(self);
    161 	struct sio_attach_args *args = aux;
    162 	int channel;
    163 	struct tty *tp;
    164 
    165 	sc->sc_dev = self;
    166 	channel = args->channel;
    167 	sc->sc_ctl = &siosc->sc_ctl[channel];
    168 	memcpy(sc->sc_wr, ch0_regs, sizeof(ch0_regs));
    169 	siosc->sc_intrhand[channel].ih_func = siottyintr;
    170 	siosc->sc_intrhand[channel].ih_arg = sc;
    171 	if (args->hwflags == 1)
    172 		sc->sc_hwflags |= SIOTTY_HW_CONSOLE;
    173 
    174 	if ((sc->sc_hwflags & SIOTTY_HW_CONSOLE) != 0) {
    175 		aprint_normal(" (console)");
    176 		sc->sc_flags = TIOCFLAG_SOFTCAR;
    177 	} else {
    178 		setsioreg(sc->sc_ctl, WR0, WR0_CHANRST);
    179 		setsioreg(sc->sc_ctl, WR2A, WR2_VEC86 | WR2_INTR_1);
    180 		setsioreg(sc->sc_ctl, WR2B, 0);
    181 		setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
    182 		setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]);
    183 		setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]);
    184 		setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]);
    185 		setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
    186 	}
    187 	setsioreg(sc->sc_ctl, WR1, sc->sc_wr[WR1]); /* now interrupt driven */
    188 
    189 	aprint_normal("\n");
    190 
    191 	sc->sc_rbuf = kmem_alloc(siotty_rbuf_size * 2, KM_SLEEP);
    192 	sc->sc_rbufend = sc->sc_rbuf + (siotty_rbuf_size * 2);
    193 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    194 	sc->sc_rbavail = siotty_rbuf_size;
    195 
    196 	tp = tty_alloc();
    197 	tp->t_oproc = siostart;
    198 	tp->t_param = sioparam;
    199 	tp->t_hwiflow = NULL /* XXX siohwiflow XXX */;
    200 	if ((sc->sc_hwflags & SIOTTY_HW_CONSOLE) != 0)
    201 		tp->t_dev = cn_tab->cn_dev;
    202 	sc->sc_tty = tp;
    203 
    204 	tty_attach(tp);
    205 
    206 	sc->sc_si = softint_establish(SOFTINT_SERIAL, siottysoft, sc);
    207 }
    208 
    209 /*--------------------  low level routine --------------------*/
    210 
    211 static void
    212 siottyintr(void *arg)
    213 {
    214 	struct siotty_softc *sc;
    215 	struct sioreg *sio;
    216 	uint8_t *put, *end;
    217 	uint8_t c;
    218 	uint16_t rr;
    219 	int cc;
    220 
    221 	sc = arg;
    222 	end = sc->sc_rbufend;
    223 	put = sc->sc_rbput;
    224 	cc = sc->sc_rbavail;
    225 
    226 	sio = sc->sc_ctl;
    227 	rr = getsiocsr(sio);
    228 	if ((rr & RR_BREAK) != 0) {
    229 		sio->sio_cmd = WR0_RSTINT;
    230 		cn_check_magic(sc->sc_tty->t_dev, CNC_BREAK, siotty_cnm_state);
    231 	}
    232 	if ((rr & RR_RXRDY) != 0) {
    233 		do {
    234 			if (cc > 0) {
    235 				c = sio->sio_data;
    236 				cn_check_magic(sc->sc_tty->t_dev, c,
    237 				    siotty_cnm_state);
    238 				put[0] = c;
    239 				put[1] = rr & 0xff;
    240 				put += 2;
    241 				if (put >= end)
    242 					put = sc->sc_rbuf;
    243 				cc--;
    244 			}
    245 			if ((rr & (RR_FRAMING | RR_OVERRUN | RR_PARITY)) != 0)
    246 				sio->sio_cmd = WR0_ERRRST;
    247 
    248 			sc->sc_rbput = put;
    249 			sc->sc_rbavail = cc;
    250 			sc->sc_rx_ready = true;
    251 		} while (((rr = getsiocsr(sio)) & RR_RXRDY) != 0);
    252 	}
    253 	if ((rr & RR_TXRDY) != 0) {
    254 		sio->sio_cmd = WR0_RSTPEND;
    255 		if (sc->sc_tbc > 0) {
    256 			sio->sio_data = *sc->sc_tba;
    257 			sc->sc_tba++;
    258 			sc->sc_tbc--;
    259 		} else {
    260 			if (sc->sc_tx_busy) {
    261 				sc->sc_tx_busy = false;
    262 				sc->sc_tx_done = true;
    263 			}
    264 		}
    265 	}
    266 	softint_schedule(sc->sc_si);
    267 }
    268 
    269 static void
    270 siottysoft(void *arg)
    271 {
    272 	struct siotty_softc *sc;
    273 	struct tty *tp;
    274 
    275 	sc = arg;
    276 	tp = sc->sc_tty;
    277 
    278 	if (sc->sc_rx_ready) {
    279 		sc->sc_rx_ready = false;
    280 		siotty_rxsoft(sc, tp);
    281 	}
    282 	if (sc->sc_tx_done) {
    283 		sc->sc_tx_done = false;
    284 		siotty_txsoft(sc, tp);
    285 	}
    286 }
    287 
    288 static void
    289 siotty_rxsoft(struct siotty_softc *sc, struct tty *tp)
    290 {
    291 	uint8_t *get, *end;
    292 	u_int cc, scc;
    293 	unsigned int code;
    294 	uint8_t stat;
    295 	int s;
    296 
    297 	end = sc->sc_rbufend;
    298 	get = sc->sc_rbget;
    299 	scc = cc = siotty_rbuf_size - sc->sc_rbavail;
    300 
    301 	if (cc == siotty_rbuf_size) {
    302 		printf("%s: rx buffer overflow\n", device_xname(sc->sc_dev));
    303 	}
    304 
    305 	while (cc > 0) {
    306 		code = get[0];
    307 		stat = get[1];
    308 		if ((stat & RR_FRAMING) != 0)
    309 			code |= TTY_FE;
    310 		else if ((stat & RR_PARITY) != 0)
    311 			code |= TTY_PE;
    312 
    313 		(*tp->t_linesw->l_rint)(code, tp);
    314 		get += 2;
    315 		if (get >= end)
    316 			get = sc->sc_rbuf;
    317 		cc--;
    318 	}
    319 
    320 	if (cc != scc) {
    321 		s = splserial();
    322 		sc->sc_rbget = get;
    323 		sc->sc_rbavail += scc - cc;
    324 		splx(s);
    325 	}
    326 }
    327 
    328 static void
    329 siotty_txsoft(struct siotty_softc *sc, struct tty *tp)
    330 {
    331 
    332 	tp->t_state &= ~TS_BUSY;
    333 	if ((tp->t_state & TS_FLUSH) != 0)
    334 		tp->t_state &= ~TS_FLUSH;
    335 	else
    336 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
    337 	(*tp->t_linesw->l_start)(tp);
    338 }
    339 
    340 static void
    341 siostart(struct tty *tp)
    342 {
    343 	struct siotty_softc *sc;
    344 	int s;
    345 	uint8_t *tba;
    346 	int tbc;
    347 
    348 	sc = device_lookup_private(&siotty_cd, minor(tp->t_dev));
    349 	s = splserial();
    350 	if ((tp->t_state & (TS_BUSY|TS_TIMEOUT|TS_TTSTOP)) != 0)
    351 		goto out;
    352 	if (!ttypull(tp))
    353 		goto out;
    354 	tp->t_state |= TS_BUSY;
    355 
    356 	tba = tp->t_outq.c_cf;
    357 	tbc = ndqb(&tp->t_outq, 0);
    358 
    359 	sc->sc_tba = tba;
    360 	sc->sc_tbc = tbc;
    361 	sc->sc_tx_busy = true;
    362 
    363 	sc->sc_ctl->sio_data = *sc->sc_tba;
    364 	sc->sc_tba++;
    365 	sc->sc_tbc--;
    366 out:
    367 	splx(s);
    368 }
    369 
    370 static void
    371 siostop(struct tty *tp, int flag)
    372 {
    373 	int s;
    374 
    375 	s = splserial();
    376 	if (TS_BUSY == (tp->t_state & (TS_BUSY|TS_TTSTOP))) {
    377 		/*
    378 		 * Device is transmitting; must stop it.
    379 		 */
    380 		tp->t_state |= TS_FLUSH;
    381 	}
    382 	splx(s);
    383 }
    384 
    385 static int
    386 sioparam(struct tty *tp, struct termios *t)
    387 {
    388 	struct siotty_softc *sc;
    389 	int wr4, s;
    390 
    391 	sc = device_lookup_private(&siotty_cd, minor(tp->t_dev));
    392 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    393 		return EINVAL;
    394 	wr4 = ttspeedtab(t->c_ospeed, siospeedtab);
    395 	if (wr4 < 0)
    396 		return EINVAL;
    397 
    398 	if ((sc->sc_flags & TIOCFLAG_SOFTCAR) != 0) {
    399 		t->c_cflag |= CLOCAL;
    400 		t->c_cflag &= ~HUPCL;
    401 	}
    402 	if ((sc->sc_flags & TIOCFLAG_CLOCAL) != 0)
    403 		t->c_cflag |= CLOCAL;
    404 
    405 	/*
    406 	 * If there were no changes, don't do anything.  This avoids dropping
    407 	 * input and improves performance when all we did was frob things like
    408 	 * VMIN and VTIME.
    409 	 */
    410 	if (tp->t_ospeed == t->c_ospeed && tp->t_cflag == t->c_cflag)
    411 		return 0;
    412 
    413 	tp->t_ispeed = t->c_ispeed;
    414 	tp->t_ospeed = t->c_ospeed;
    415 	tp->t_cflag = t->c_cflag;
    416 
    417 	sc->sc_wr[WR3] &= 0x3f;
    418 	sc->sc_wr[WR5] &= 0x9f;
    419 	switch (tp->t_cflag & CSIZE) {
    420 	case CS7:
    421 		sc->sc_wr[WR3] |= WR3_RX7BIT; sc->sc_wr[WR5] |= WR5_TX7BIT;
    422 		break;
    423 	case CS8:
    424 		sc->sc_wr[WR3] |= WR3_RX8BIT; sc->sc_wr[WR5] |= WR5_TX8BIT;
    425 		break;
    426 	}
    427 	if ((tp->t_cflag & PARENB) != 0) {
    428 		wr4 |= WR4_PARENAB;
    429 		if ((tp->t_cflag & PARODD) == 0)
    430 			wr4 |= WR4_EPARITY;
    431 	}
    432 	wr4 |= (tp->t_cflag & CSTOPB) ? WR4_STOP2 : WR4_STOP1;
    433 	sc->sc_wr[WR4] = wr4;
    434 
    435 	s = splserial();
    436 	setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]);
    437 	setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]);
    438 	setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]);
    439 	splx(s);
    440 
    441 	return 0;
    442 }
    443 
    444 static int
    445 siomctl(struct siotty_softc *sc, int control, int op)
    446 {
    447 	int val, s;
    448 	uint8_t wr5;
    449 	uint16_t rr;
    450 
    451 	val = 0;
    452 	if ((control & TIOCM_BREAK) != 0)
    453 		val |= WR5_BREAK;
    454 	if ((control & TIOCM_DTR) != 0)
    455 		val |= WR5_DTR;
    456 	if ((control & TIOCM_RTS) != 0)
    457 		val |= WR5_RTS;
    458 	s = splserial();
    459 	wr5 = sc->sc_wr[WR5];
    460 	switch (op) {
    461 	case DMSET:
    462 		wr5 &= ~(WR5_BREAK|WR5_DTR|WR5_RTS);
    463 		/* FALLTHRU */
    464 	case DMBIS:
    465 		wr5 |= val;
    466 		break;
    467 	case DMBIC:
    468 		wr5 &= ~val;
    469 		break;
    470 	case DMGET:
    471 		val = 0;
    472 		rr = getsiocsr(sc->sc_ctl);
    473 		if ((wr5 & WR5_DTR) != 0)
    474 			val |= TIOCM_DTR;
    475 		if ((wr5 & WR5_RTS) != 0)
    476 			val |= TIOCM_RTS;
    477 		if ((rr & RR_CTS) != 0)
    478 			val |= TIOCM_CTS;
    479 		if ((rr & RR_DCD) != 0)
    480 			val |= TIOCM_CD;
    481 		goto done;
    482 	}
    483 	sc->sc_wr[WR5] = wr5;
    484 	setsioreg(sc->sc_ctl, WR5, wr5);
    485 	val = 0;
    486  done:
    487 	splx(s);
    488 	return val;
    489 }
    490 
    491 /*--------------------  cdevsw[] interface --------------------*/
    492 
    493 static int
    494 sioopen(dev_t dev, int flag, int mode, struct lwp *l)
    495 {
    496 	struct siotty_softc *sc;
    497 	struct tty *tp;
    498 	int error;
    499 	int s;
    500 
    501 	sc = device_lookup_private(&siotty_cd, minor(dev));
    502 	if (sc == NULL)
    503 		return ENXIO;
    504 
    505 	tp = sc->sc_tty;
    506 
    507 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    508 		return EBUSY;
    509 
    510 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
    511 		struct termios t;
    512 
    513 		tp->t_dev = dev;
    514 		t.c_ispeed = t.c_ospeed = TTYDEF_SPEED;
    515 		t.c_cflag = TTYDEF_CFLAG;
    516 		tp->t_ospeed = 0; /* force register update */
    517 		(void)sioparam(tp, &t);
    518 		tp->t_iflag = TTYDEF_IFLAG;
    519 		tp->t_oflag = TTYDEF_OFLAG;
    520 		tp->t_lflag = TTYDEF_LFLAG;
    521 		ttychars(tp);
    522 		ttsetwater(tp);
    523 		/* raise RTS and DTR here; but, DTR lead is not wired */
    524 		/* then check DCD condition; but, DCD lead is not wired */
    525 #if 0
    526 		if ((sc->sc_flags & TIOCFLAG_SOFTCAR) != 0
    527 		    || (tp->t_cflag & MDMBUF) != 0
    528 		    || (getsiocsr(sc->sc_ctl) & RR_DCD) != 0)
    529 			tp->t_state |= TS_CARR_ON;
    530 		else
    531 			tp->t_state &= ~TS_CARR_ON;
    532 #else
    533 		tp->t_state |= TS_CARR_ON; /* assume detected all the time */
    534 #endif
    535 
    536 		s = splserial();
    537 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    538 		sc->sc_rbavail = siotty_rbuf_size;
    539 		splx(s);
    540 	}
    541 
    542 	error = ttyopen(tp, 0, (flag & O_NONBLOCK));
    543 	if (error > 0)
    544 		return error;
    545 	return (*tp->t_linesw->l_open)(dev, tp);
    546 }
    547 
    548 static int
    549 sioclose(dev_t dev, int flag, int mode, struct lwp *l)
    550 {
    551 	struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
    552 	struct tty *tp = sc->sc_tty;
    553 	int s;
    554 
    555 	(*tp->t_linesw->l_close)(tp, flag);
    556 
    557 	s = splserial();
    558 	siomctl(sc, TIOCM_BREAK, DMBIC);
    559 #if 0 /* because unable to feed DTR signal */
    560 	if ((tp->t_cflag & HUPCL) != 0
    561 	    || tp->t_wopen || (tp->t_state & TS_ISOPEN) == 0) {
    562 		siomctl(sc, TIOCM_DTR, DMBIC);
    563 		/* Yield CPU time to others for 1 second, then ... */
    564 		siomctl(sc, TIOCM_DTR, DMBIS);
    565 	}
    566 #endif
    567 	splx(s);
    568 	return ttyclose(tp);
    569 }
    570 
    571 static int
    572 sioread(dev_t dev, struct uio *uio, int flag)
    573 {
    574 	struct siotty_softc *sc;
    575 	struct tty *tp;
    576 
    577 	sc = device_lookup_private(&siotty_cd, minor(dev));
    578 	tp = sc->sc_tty;
    579 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    580 }
    581 
    582 static int
    583 siowrite(dev_t dev, struct uio *uio, int flag)
    584 {
    585 	struct siotty_softc *sc;
    586 	struct tty *tp;
    587 
    588 	sc = device_lookup_private(&siotty_cd, minor(dev));
    589 	tp = sc->sc_tty;
    590 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    591 }
    592 
    593 static int
    594 siopoll(dev_t dev, int events, struct lwp *l)
    595 {
    596 	struct siotty_softc *sc;
    597 	struct tty *tp;
    598 
    599 	sc = device_lookup_private(&siotty_cd, minor(dev));
    600 	tp = sc->sc_tty;
    601 	return (*tp->t_linesw->l_poll)(tp, events, l);
    602 }
    603 
    604 static int
    605 sioioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    606 {
    607 	struct siotty_softc *sc;
    608 	struct tty *tp;
    609 	int error;
    610 
    611 	sc = device_lookup_private(&siotty_cd, minor(dev));
    612 	tp = sc->sc_tty;
    613 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    614 	if (error != EPASSTHROUGH)
    615 		return error;
    616 
    617 	error = ttioctl(tp, cmd, data, flag, l);
    618 	if (error != EPASSTHROUGH)
    619 		return error;
    620 
    621 	/* the last resort for TIOC ioctl tranversing */
    622 	switch (cmd) {
    623 	case TIOCSBRK: /* Set the hardware into BREAK condition */
    624 		siomctl(sc, TIOCM_BREAK, DMBIS);
    625 		break;
    626 	case TIOCCBRK: /* Clear the hardware BREAK condition */
    627 		siomctl(sc, TIOCM_BREAK, DMBIC);
    628 		break;
    629 	case TIOCSDTR: /* Assert DTR signal */
    630 		siomctl(sc, TIOCM_DTR|TIOCM_RTS, DMBIS);
    631 		break;
    632 	case TIOCCDTR: /* Clear DTR signal */
    633 		siomctl(sc, TIOCM_DTR|TIOCM_RTS, DMBIC);
    634 		break;
    635 	case TIOCMSET: /* Set modem state replacing current one */
    636 		siomctl(sc, *(int *)data, DMSET);
    637 		break;
    638 	case TIOCMGET: /* Return current modem state */
    639 		*(int *)data = siomctl(sc, 0, DMGET);
    640 		break;
    641 	case TIOCMBIS: /* Set individual bits of modem state */
    642 		siomctl(sc, *(int *)data, DMBIS);
    643 		break;
    644 	case TIOCMBIC: /* Clear individual bits of modem state */
    645 		siomctl(sc, *(int *)data, DMBIC);
    646 		break;
    647 	case TIOCSFLAGS: /* Instruct how serial port behaves */
    648 		sc->sc_flags = *(int *)data;
    649 		break;
    650 	case TIOCGFLAGS: /* Return current serial port state */
    651 		*(int *)data = sc->sc_flags;
    652 		break;
    653 	default:
    654 		return EPASSTHROUGH;
    655 	}
    656 	return 0;
    657 }
    658 
    659 /* ARSGUSED */
    660 static struct tty *
    661 siotty(dev_t dev)
    662 {
    663 	struct siotty_softc *sc;
    664 
    665 	sc = device_lookup_private(&siotty_cd, minor(dev));
    666 	return sc->sc_tty;
    667 }
    668 
    669 /*--------------------  miscelleneous routine --------------------*/
    670 
    671 /* EXPORT */ void
    672 setsioreg(struct sioreg *sio, int regno, int val)
    673 {
    674 
    675 	if (regno != 0)
    676 		sio->sio_cmd = regno;	/* DELAY(); */
    677 	sio->sio_cmd = val;		/* DELAY(); */
    678 }
    679 
    680 /* EXPORT */ uint16_t
    681 getsiocsr(struct sioreg *sio)
    682 {
    683 	int val;
    684 
    685 	val = sio->sio_stat << 8;	/* DELAY(); */
    686 	sio->sio_cmd = 1;		/* DELAY(); */
    687 	val |= sio->sio_stat;		/* DELAY(); */
    688 	return val;
    689 }
    690 
    691 /*---------------------  console interface ----------------------*/
    692 
    693 struct consdev syscons = {
    694 	NULL,
    695 	NULL,
    696 	syscngetc,
    697 	syscnputc,
    698 	nullcnpollc,
    699 	NULL,
    700 	NULL,
    701 	NULL,
    702 	NODEV,
    703 	CN_REMOTE,
    704 };
    705 
    706 /* EXPORT */ void
    707 syscninit(int channel)
    708 {
    709 /*
    710  * Channel A is immediately initialized with 9600N1 right after cold
    711  * boot/reset/poweron.  ROM monitor emits one line message on CH.A.
    712  */
    713 	struct sioreg *sio;
    714 	sio = (struct sioreg *)0x51000000 + channel;
    715 
    716 	syscons.cn_dev = makedev(cdevsw_lookup_major(&siotty_cdevsw),
    717 				 channel);
    718 	cn_tab = &syscons;
    719 	cn_init_magic(&siotty_cnm_state);
    720 	cn_set_magic("\047\001");
    721 
    722 	setsioreg(sio, WR0, WR0_CHANRST);
    723 	setsioreg(sio, WR2A, WR2_VEC86 | WR2_INTR_1);
    724 	setsioreg(sio, WR2B, 0);
    725 	setsioreg(sio, WR0, ch0_regs[WR0]);
    726 	setsioreg(sio, WR4, ch0_regs[WR4]);
    727 	setsioreg(sio, WR3, ch0_regs[WR3]);
    728 	setsioreg(sio, WR5, ch0_regs[WR5]);
    729 	setsioreg(sio, WR0, ch0_regs[WR0]);
    730 }
    731 
    732 /* EXPORT */ int
    733 syscngetc(dev_t dev)
    734 {
    735 	struct sioreg *sio;
    736 	int s, c;
    737 
    738 	sio = (struct sioreg *)0x51000000 + ((int)dev & 0x1);
    739 	s = splhigh();
    740 	while ((getsiocsr(sio) & RR_RXRDY) == 0)
    741 		continue;
    742 	c = sio->sio_data;
    743 	splx(s);
    744 
    745 	return c;
    746 }
    747 
    748 /* EXPORT */ void
    749 syscnputc(dev_t dev, int c)
    750 {
    751 	struct sioreg *sio;
    752 	int s;
    753 
    754 	sio = (struct sioreg *)0x51000000 + ((int)dev & 0x1);
    755 	s = splhigh();
    756 	while ((getsiocsr(sio) & RR_TXRDY) == 0)
    757 		continue;
    758 	sio->sio_cmd = WR0_RSTPEND;
    759 	sio->sio_data = c;
    760 	splx(s);
    761 }
    762