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