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