Home | History | Annotate | Line # | Download | only in dev
ser.c revision 1.46
      1 /*	$NetBSD: ser.c,v 1.46 2010/04/09 10:04:35 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Leo Weppelman.
      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  * Copyright (c) 1993, 1994, 1995, 1996, 1997
     33  *      Charles M. Hannum.  All rights reserved.
     34  *
     35  * Interrupt processing and hardware flow control partly based on code from
     36  * Onno van der Linden and Gordon Ross.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. All advertising materials mentioning features or use of this software
     47  *    must display the following acknowledgement:
     48  *      This product includes software developed by Charles M. Hannum.
     49  * 4. The name of the author may not be used to endorse or promote products
     50  *    derived from this software without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     55  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     56  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     57  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     61  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62  */
     63 
     64 /*
     65  * Copyright (c) 1991 The Regents of the University of California.
     66  * All rights reserved.
     67  *
     68  * Redistribution and use in source and binary forms, with or without
     69  * modification, are permitted provided that the following conditions
     70  * are met:
     71  * 1. Redistributions of source code must retain the above copyright
     72  *    notice, this list of conditions and the following disclaimer.
     73  * 2. Redistributions in binary form must reproduce the above copyright
     74  *    notice, this list of conditions and the following disclaimer in the
     75  *    documentation and/or other materials provided with the distribution.
     76  * 3. Neither the name of the University nor the names of its contributors
     77  *    may be used to endorse or promote products derived from this software
     78  *    without specific prior written permission.
     79  *
     80  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     81  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     82  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     83  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     84  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     85  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     86  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     87  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     88  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     89  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     90  * SUCH DAMAGE.
     91  *
     92  *      @(#)com.c       7.5 (Berkeley) 5/16/91
     93  */
     94 
     95 #include <sys/cdefs.h>
     96 __KERNEL_RCSID(0, "$NetBSD: ser.c,v 1.46 2010/04/09 10:04:35 tsutsui Exp $");
     97 
     98 #include "opt_ddb.h"
     99 #include "opt_mbtype.h"
    100 #include "opt_serconsole.h"
    101 
    102 #include <sys/param.h>
    103 #include <sys/systm.h>
    104 #include <sys/ioctl.h>
    105 #include <sys/select.h>
    106 #include <sys/tty.h>
    107 #include <sys/proc.h>
    108 #include <sys/conf.h>
    109 #include <sys/file.h>
    110 #include <sys/uio.h>
    111 #include <sys/kernel.h>
    112 #include <sys/syslog.h>
    113 #include <sys/types.h>
    114 #include <sys/device.h>
    115 #include <sys/kauth.h>
    116 
    117 #include <m68k/asm_single.h>
    118 
    119 #include <machine/iomap.h>
    120 #include <machine/mfp.h>
    121 #include <machine/intr.h>
    122 #include <atari/dev/serreg.h>
    123 
    124 #if !defined(_MILANHW_)
    125 #include <atari/dev/ym2149reg.h>
    126 #else
    127 	/* MILAN has no ym2149 */
    128 #define ym2149_dtr(set) {					\
    129 	if (set)						\
    130 		single_inst_bset_b(MFP->mf_gpip, 0x08);		\
    131 	else							\
    132 		single_inst_bclr_b(MFP->mf_gpip, 0x08);		\
    133 }
    134 
    135 #define ym2149_rts(set) {					\
    136 	if (set)						\
    137 		single_inst_bset_b(MFP->mf_gpip, 0x01);		\
    138 	else							\
    139 		single_inst_bclr_b(MFP->mf_gpip, 0x01);		\
    140 }
    141 #endif /* _MILANHW_ */
    142 
    143 /* #define SER_DEBUG */
    144 
    145 #define	SERUNIT(x)	(minor(x) & 0x7ffff)
    146 #define	SERDIALOUT(x)	(minor(x) & 0x80000)
    147 
    148 /* XXX */
    149 #define	CONSBAUD	9600
    150 #define	CONSCFLAG	TTYDEF_CFLAG
    151 /* end XXX */
    152 
    153 /* Buffer size for character buffer */
    154 #define RXBUFSIZE 2048		/* More than enough..			*/
    155 #define RXBUFMASK (RXBUFSIZE-1)	/* Only iff previous is a power of 2	*/
    156 #define RXHIWAT   (RXBUFSIZE >> 2)
    157 
    158 struct ser_softc {
    159 	struct device	 sc_dev;
    160 	struct tty	*sc_tty;
    161 
    162 	struct callout sc_diag_ch;
    163 
    164 	int		 sc_overflows;
    165 	int		 sc_floods;
    166 	int		 sc_errors;
    167 
    168 	uint8_t		 sc_hwflags;
    169 	uint8_t		 sc_swflags;
    170 
    171 	int		 sc_ospeed;		/* delay + timer-d data	*/
    172 	uint8_t		 sc_imra;
    173 	uint8_t		 sc_imrb;
    174 	uint8_t		 sc_ucr;		/* Uart control		*/
    175 	uint8_t		 sc_msr;		/* Modem status		*/
    176 	uint8_t		 sc_tsr;		/* Tranceiver status	*/
    177 	uint8_t		 sc_rsr;		/* Receiver status	*/
    178 	uint8_t		 sc_mcr;		/* (Pseudo) Modem ctrl. */
    179 
    180 	uint8_t		 sc_msr_delta;
    181 	uint8_t		 sc_msr_mask;
    182 	uint8_t		 sc_mcr_active;
    183 	uint8_t		 sc_mcr_dtr, sc_mcr_rts, sc_msr_cts, sc_msr_dcd;
    184 
    185 	int		 sc_r_hiwat;
    186  	volatile u_int	 sc_rbget;
    187  	volatile u_int	 sc_rbput;
    188 	volatile u_int	 sc_rbavail;
    189  	uint8_t		 sc_rbuf[RXBUFSIZE];
    190 	uint8_t		 sc_lbuf[RXBUFSIZE];
    191 
    192 	volatile uint8_t sc_rx_blocked;
    193 	volatile uint8_t sc_rx_ready;
    194 	volatile uint8_t sc_tx_busy;
    195 	volatile uint8_t sc_tx_done;
    196 	volatile uint8_t sc_tx_stopped;
    197 	volatile uint8_t sc_st_check;
    198 
    199  	uint8_t		*sc_tba;
    200  	int		 sc_tbc;
    201 	int		 sc_heldtbc;
    202 
    203 	volatile uint8_t sc_heldchange;
    204 
    205 	void		*sc_sicookie;
    206 };
    207 
    208 /*
    209  * For sc_hwflags:
    210  */
    211 #define	SER_HW_CONSOLE	0x01
    212 
    213 void	ser_break(struct ser_softc *, int);
    214 void	ser_hwiflow(struct ser_softc *, int);
    215 void	ser_iflush(struct ser_softc *);
    216 void	ser_loadchannelregs(struct ser_softc *);
    217 void	ser_modem(struct ser_softc *, int);
    218 void	serdiag(void *);
    219 int	serhwiflow(struct tty *, int);
    220 void	serinit(int);
    221 void	serinitcons(int);
    222 int	sermintr(void *);
    223 int	sertrintr(void *);
    224 int	serparam(struct tty *, struct termios *);
    225 void	serstart(struct tty *);
    226 
    227 struct consdev;
    228 void	sercnprobe(struct consdev *);
    229 void	sercninit(struct consdev *);
    230 int	sercngetc(dev_t);
    231 void	sercnputc(dev_t, int);
    232 void	sercnpollc(dev_t, int);
    233 
    234 static void sermsrint(struct ser_softc *, struct tty*);
    235 static void serrxint(struct ser_softc *, struct tty*);
    236 static void ser_shutdown(struct ser_softc *);
    237 static int serspeed(long);
    238 static void sersoft(void *);
    239 static void sertxint(struct ser_softc *, struct tty*);
    240 
    241 /*
    242  * Autoconfig stuff
    243  */
    244 static void serattach(struct device *, struct device *, void *);
    245 static int  sermatch(struct device *, struct cfdata *, void *);
    246 
    247 CFATTACH_DECL(ser, sizeof(struct ser_softc),
    248     sermatch, serattach, NULL, NULL);
    249 
    250 extern struct cfdriver ser_cd;
    251 
    252 dev_type_open(seropen);
    253 dev_type_close(serclose);
    254 dev_type_read(serread);
    255 dev_type_write(serwrite);
    256 dev_type_ioctl(serioctl);
    257 dev_type_stop(serstop);
    258 dev_type_tty(sertty);
    259 dev_type_poll(serpoll);
    260 
    261 const struct cdevsw ser_cdevsw = {
    262 	seropen, serclose, serread, serwrite, serioctl,
    263 	serstop, sertty, serpoll, nommap, ttykqfilter, D_TTY
    264 };
    265 
    266 /*ARGSUSED*/
    267 static	int
    268 sermatch(struct device *pdp, struct cfdata *cfp, void *auxp)
    269 {
    270 	static int ser_matched = 0;
    271 
    272 	/* Match at most one ser unit */
    273 	if (strcmp((char *)auxp, "ser") || ser_matched)
    274 		return 0;
    275 
    276 	ser_matched = 1;
    277 	return 1;
    278 }
    279 
    280 /*ARGSUSED*/
    281 static void
    282 serattach(struct device *pdp, struct device *dp, void *auxp)
    283 {
    284 	struct ser_softc *sc = device_private(dp);
    285 
    286 	if (intr_establish(1, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
    287 		printf("serattach: Can't establish interrupt (1)\n");
    288 	if (intr_establish(2, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
    289 		printf("serattach: Can't establish interrupt (2)\n");
    290 	if (intr_establish(14, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
    291 		printf("serattach: Can't establish interrupt (14)\n");
    292 	if (intr_establish(9, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
    293 		printf("serattach: Can't establish interrupt (9)\n");
    294 	if (intr_establish(10, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
    295 		printf("serattach: Can't establish interrupt (10)\n");
    296 	if (intr_establish(11, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
    297 		printf("serattach: Can't establish interrupt (11)\n");
    298 	if (intr_establish(12, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
    299 		printf("serattach: Can't establish interrupt (12)\n");
    300 
    301 	sc->sc_sicookie = softint_establish(SOFTINT_SERIAL, sersoft, sc);
    302 
    303 	ym2149_rts(1);
    304 	ym2149_dtr(1);
    305 
    306 	/*
    307 	 * Enable but mask interrupts...
    308 	 * XXX: Look at edge-sensitivity for DCD/CTS interrupts.
    309 	 */
    310 	MFP->mf_ierb |= IB_SCTS|IB_SDCD;
    311 	MFP->mf_iera |= IA_RRDY|IA_RERR|IA_TRDY|IA_TERR;
    312 	MFP->mf_imrb &= ~(IB_SCTS|IB_SDCD);
    313 	MFP->mf_imra &= ~(IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
    314 
    315 	callout_init(&sc->sc_diag_ch, 0);
    316 
    317 #if SERCONSOLE > 0
    318 	/*
    319 	 * Activate serial console when DCD present...
    320 	 */
    321 	if ((MFP->mf_gpip & MCR_DCD) == 0)
    322 		SET(sc->sc_hwflags, SER_HW_CONSOLE);
    323 #endif /* SERCONSOLE > 0 */
    324 
    325 	printf("\n");
    326 	if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
    327 		serinit(CONSBAUD);
    328 		printf("%s: console\n", sc->sc_dev.dv_xname);
    329 	}
    330 }
    331 
    332 #ifdef SER_DEBUG
    333 void serstatus(struct ser_softc *, char *);
    334 void
    335 serstatus(struct ser_softc *sc, char *str)
    336 {
    337 	struct tty *tp = sc->sc_tty;
    338 
    339 	printf("%s: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
    340 	    sc->sc_dev.dv_xname, str,
    341 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
    342 	    ISSET(sc->sc_msr, MCR_DCD) ? "+" : "-",
    343 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
    344 	    ISSET(sc->sc_mcr, MCR_DTR) ? "+" : "-",
    345 	    sc->sc_tx_stopped ? "+" : "-");
    346 
    347 	printf("%s: %s %scrtscts %scts %sts_ttstop  %srts %srx_blocked\n",
    348 	    sc->sc_dev.dv_xname, str,
    349 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
    350 	    ISSET(sc->sc_msr, MCR_CTS) ? "+" : "-",
    351 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
    352 	    ISSET(sc->sc_mcr, MCR_RTS) ? "+" : "-",
    353 	    sc->sc_rx_blocked ? "+" : "-");
    354 }
    355 #endif /* SER_DEBUG */
    356 
    357 int
    358 seropen(dev_t dev, int flag, int mode, struct lwp *l)
    359 {
    360 	int unit = SERUNIT(dev);
    361 	struct ser_softc *sc;
    362 	struct tty *tp;
    363 	int s, s2;
    364 	int error = 0;
    365 
    366 	sc = device_lookup_private(&ser_cd, unit);
    367 	if (sc == NULL)
    368 		return ENXIO;
    369 
    370 	if (!sc->sc_tty) {
    371 		tp = sc->sc_tty = ttymalloc();
    372 		tty_attach(tp);
    373 	} else
    374 		tp = sc->sc_tty;
    375 
    376 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    377 		return EBUSY;
    378 
    379 	s = spltty();
    380 
    381 	/*
    382 	 * Do the following if this is a first open.
    383 	 */
    384 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    385 		struct termios t;
    386 
    387 		/* Turn on interrupts. */
    388 		sc->sc_imra = IA_RRDY|IA_RERR|IA_TRDY|IA_TERR;
    389 		sc->sc_imrb = IB_SCTS|IB_SDCD;
    390 		single_inst_bset_b(MFP->mf_imra, sc->sc_imra);
    391 		single_inst_bset_b(MFP->mf_imrb, sc->sc_imrb);
    392 
    393 		/* Fetch the current modem control status, needed later. */
    394 		sc->sc_msr = ~MFP->mf_gpip & (IO_SDCD|IO_SCTS|IO_SRI);
    395 
    396 		/* Add some entry points needed by the tty layer. */
    397 		tp->t_oproc = serstart;
    398 		tp->t_param = serparam;
    399 		tp->t_hwiflow = serhwiflow;
    400 		tp->t_dev = dev;
    401 
    402 		/*
    403 		 * Initialize the termios status to the defaults.  Add in the
    404 		 * sticky bits from TIOCSFLAGS.
    405 		 */
    406 		t.c_ispeed = 0;
    407 		if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
    408 			t.c_ospeed = CONSBAUD;
    409 			t.c_cflag  = CONSCFLAG;
    410 		} else {
    411 			t.c_ospeed = TTYDEF_SPEED;
    412 			t.c_cflag = TTYDEF_CFLAG;
    413 		}
    414 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    415 			SET(t.c_cflag, CLOCAL);
    416 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    417 			SET(t.c_cflag, CRTSCTS);
    418 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    419 			SET(t.c_cflag, MDMBUF);
    420 		tp->t_iflag = TTYDEF_IFLAG;
    421 		tp->t_oflag = TTYDEF_OFLAG;
    422 		tp->t_lflag = TTYDEF_LFLAG;
    423 		ttychars(tp);
    424 		(void)serparam(tp, &t);
    425 		ttsetwater(tp);
    426 
    427 		s2 = splhigh();
    428 
    429 		/*
    430 		 * Turn on DTR.  We must always do this, even if carrier is not
    431 		 * present, because otherwise we'd have to use TIOCSDTR
    432 		 * immediately after setting CLOCAL.  We will drop DTR only on
    433 		 * the next high-low transition of DCD, or by explicit request.
    434 		 */
    435 		ser_modem(sc, 1);
    436 
    437 		/* Clear the input ring, and unblock. */
    438 		sc->sc_rbput = sc->sc_rbget = 0;
    439 		sc->sc_rbavail = RXBUFSIZE;
    440 		ser_iflush(sc);
    441 		sc->sc_rx_blocked = 0;
    442 		ser_hwiflow(sc, 0);
    443 
    444 #ifdef SER_DEBUG
    445 		serstatus(sc, "seropen  ");
    446 #endif
    447 
    448 		splx(s2);
    449 	}
    450 
    451 	splx(s);
    452 
    453 	error = ttyopen(tp, SERDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    454 	if (error)
    455 		goto bad;
    456 
    457 	error = (*tp->t_linesw->l_open)(dev, tp);
    458         if (error)
    459 		goto bad;
    460 
    461 	return 0;
    462 
    463 bad:
    464 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    465 		/*
    466 		 * We failed to open the device, and nobody else had it opened.
    467 		 * Clean up the state as appropriate.
    468 		 */
    469 		ser_shutdown(sc);
    470 	}
    471 
    472 	return error;
    473 }
    474 
    475 int
    476 serclose(dev_t dev, int flag, int mode, struct lwp *l)
    477 {
    478 	int unit = SERUNIT(dev);
    479 	struct ser_softc *sc = device_lookup_private(&ser_cd, unit);
    480 	struct tty *tp = sc->sc_tty;
    481 
    482 	/* XXX This is for cons.c. */
    483 	if (!ISSET(tp->t_state, TS_ISOPEN))
    484 		return 0;
    485 
    486 	(*tp->t_linesw->l_close)(tp, flag);
    487 	ttyclose(tp);
    488 
    489 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    490 		/*
    491 		 * Although we got a last close, the device may still be in
    492 		 * use; e.g. if this was the dialout node, and there are still
    493 		 * processes waiting for carrier on the non-dialout node.
    494 		 */
    495 		ser_shutdown(sc);
    496 	}
    497 
    498 	return 0;
    499 }
    500 
    501 int
    502 serread(dev_t dev, struct uio *uio, int flag)
    503 {
    504 	struct ser_softc *sc = device_lookup_private(&ser_cd, SERUNIT(dev));
    505 	struct tty *tp = sc->sc_tty;
    506 
    507 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    508 }
    509 
    510 int
    511 serwrite(dev_t dev, struct uio *uio, int flag)
    512 {
    513 	struct ser_softc *sc = device_lookup_private(&ser_cd, SERUNIT(dev));
    514 	struct tty *tp = sc->sc_tty;
    515 
    516 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    517 }
    518 
    519 int
    520 serpoll(dev_t dev, int events, struct lwp *l)
    521 {
    522 	struct ser_softc *sc = device_lookup_private(&ser_cd, SERUNIT(dev));
    523 	struct tty *tp = sc->sc_tty;
    524 
    525 	return (*tp->t_linesw->l_poll)(tp, events, l);
    526 }
    527 
    528 struct tty *
    529 sertty(dev_t dev)
    530 {
    531 	struct ser_softc *sc = device_lookup_private(&ser_cd, SERUNIT(dev));
    532 	struct tty *tp = sc->sc_tty;
    533 
    534 	return tp;
    535 }
    536 
    537 int
    538 serioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    539 {
    540 	int unit = SERUNIT(dev);
    541 	struct ser_softc *sc = device_lookup_private(&ser_cd, unit);
    542 	struct tty *tp = sc->sc_tty;
    543 	int error;
    544 
    545 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    546 	if (error != EPASSTHROUGH)
    547 		return error;
    548 
    549 	error = ttioctl(tp, cmd, data, flag, l);
    550 	if (error != EPASSTHROUGH)
    551 		return error;
    552 
    553 	switch (cmd) {
    554 	case TIOCSBRK:
    555 		ser_break(sc, 1);
    556 		break;
    557 
    558 	case TIOCCBRK:
    559 		ser_break(sc, 0);
    560 		break;
    561 
    562 	case TIOCSDTR:
    563 		ser_modem(sc, 1);
    564 		break;
    565 
    566 	case TIOCCDTR:
    567 		ser_modem(sc, 0);
    568 		break;
    569 
    570 	case TIOCGFLAGS:
    571 		*(int *)data = sc->sc_swflags;
    572 		break;
    573 
    574 	case TIOCSFLAGS:
    575 		error = kauth_authorize_device_tty(l->l_cred,
    576 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    577 		if (error)
    578 			return error;
    579 		sc->sc_swflags = *(int *)data;
    580 		break;
    581 
    582 	case TIOCMSET:
    583 	case TIOCMBIS:
    584 	case TIOCMBIC:
    585 	case TIOCMGET:
    586 	default:
    587 		return EPASSTHROUGH;
    588 	}
    589 
    590 #ifdef SER_DEBUG
    591 	serstatus(sc, "serioctl ");
    592 #endif
    593 
    594 	return 0;
    595 }
    596 
    597 void
    598 ser_break(struct ser_softc *sc, int onoff)
    599 {
    600 	int s;
    601 
    602 	s = splhigh();
    603 	if (onoff)
    604 		SET(sc->sc_tsr, TSR_SBREAK);
    605 	else
    606 		CLR(sc->sc_tsr, TSR_SBREAK);
    607 
    608 	if (!sc->sc_heldchange) {
    609 		if (sc->sc_tx_busy) {
    610 			sc->sc_heldtbc = sc->sc_tbc;
    611 			sc->sc_tbc = 0;
    612 			sc->sc_heldchange = 1;
    613 		} else
    614 			ser_loadchannelregs(sc);
    615 	}
    616 	splx(s);
    617 }
    618 
    619 void
    620 ser_modem(struct ser_softc *sc, int onoff)
    621 {
    622 	int s;
    623 
    624 	s = splhigh();
    625 	if (onoff)
    626 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
    627 	else
    628 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
    629 
    630 	if (!sc->sc_heldchange) {
    631 		if (sc->sc_tx_busy) {
    632 			sc->sc_heldtbc = sc->sc_tbc;
    633 			sc->sc_tbc = 0;
    634 			sc->sc_heldchange = 1;
    635 		} else
    636 			ser_loadchannelregs(sc);
    637 	}
    638 	splx(s);
    639 }
    640 
    641 int
    642 serparam(struct tty *tp, struct termios *t)
    643 {
    644 	struct ser_softc *sc =
    645 	    device_lookup_private(&ser_cd, SERUNIT(tp->t_dev));
    646 	int ospeed = serspeed(t->c_ospeed);
    647 	uint8_t ucr;
    648 	int s;
    649 
    650 
    651 	/* check requested parameters */
    652 	if (ospeed < 0)
    653 		return EINVAL;
    654 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    655 		return EINVAL;
    656 
    657 	sc->sc_rsr = RSR_ENAB;
    658 	sc->sc_tsr = TSR_ENAB;
    659 
    660 	ucr = UCR_CLKDIV;
    661 
    662 	switch (ISSET(t->c_cflag, CSIZE)) {
    663 	case CS5:
    664 		SET(ucr, UCR_5BITS);
    665 		break;
    666 	case CS6:
    667 		SET(ucr, UCR_6BITS);
    668 		break;
    669 	case CS7:
    670 		SET(ucr, UCR_7BITS);
    671 		break;
    672 	case CS8:
    673 		SET(ucr, UCR_8BITS);
    674 		break;
    675 	}
    676 	if (ISSET(t->c_cflag, PARENB)) {
    677 		SET(ucr, UCR_PENAB);
    678 		if (!ISSET(t->c_cflag, PARODD))
    679 			SET(ucr, UCR_PEVEN);
    680 	}
    681 	if (ISSET(t->c_cflag, CSTOPB))
    682 		SET(ucr, UCR_STOPB2);
    683 	else
    684 		SET(ucr, UCR_STOPB1);
    685 
    686 	s = splhigh();
    687 
    688 	sc->sc_ucr = ucr;
    689 
    690 	/*
    691 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    692 	 * is always active.
    693 	 */
    694 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    695 	    ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
    696 		SET(t->c_cflag, CLOCAL);
    697 		CLR(t->c_cflag, HUPCL);
    698 	}
    699 
    700 	/*
    701 	 * If we're not in a mode that assumes a connection is present, then
    702 	 * ignore carrier changes.
    703 	 */
    704 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
    705 		sc->sc_msr_dcd = 0;
    706 	else
    707 		sc->sc_msr_dcd = MCR_DCD;
    708 	/*
    709 	 * Set the flow control pins depending on the current flow control
    710 	 * mode.
    711 	 */
    712 	if (ISSET(t->c_cflag, CRTSCTS)) {
    713 		sc->sc_mcr_dtr = MCR_DTR;
    714 		sc->sc_mcr_rts = MCR_RTS;
    715 		sc->sc_msr_cts = MCR_CTS;
    716 		sc->sc_r_hiwat = RXHIWAT;
    717 	} else if (ISSET(t->c_cflag, MDMBUF)) {
    718 		/*
    719 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
    720 		 * carrier detection.
    721 		 */
    722 		sc->sc_mcr_dtr = 0;
    723 		sc->sc_mcr_rts = MCR_DTR;
    724 		sc->sc_msr_cts = MCR_DCD;
    725 		sc->sc_r_hiwat = RXHIWAT;
    726 	} else {
    727 		/*
    728 		 * If no flow control, then always set RTS.  This will make
    729 		 * the other side happy if it mistakenly thinks we're doing
    730 		 * RTS/CTS flow control.
    731 		 */
    732 		sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
    733 		sc->sc_mcr_rts = 0;
    734 		sc->sc_msr_cts = 0;
    735 		sc->sc_r_hiwat = 0;
    736 		if (ISSET(sc->sc_mcr, MCR_DTR))
    737 			SET(sc->sc_mcr, MCR_RTS);
    738 		else
    739 			CLR(sc->sc_mcr, MCR_RTS);
    740 	}
    741 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
    742 
    743 #if 0
    744 	if (ospeed == 0)
    745 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
    746 	else
    747 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
    748 #endif
    749 
    750 	sc->sc_ospeed = ospeed;
    751 
    752 	/* and copy to tty */
    753 	tp->t_ispeed = 0;
    754 	tp->t_ospeed = t->c_ospeed;
    755 	tp->t_cflag = t->c_cflag;
    756 
    757 	if (!sc->sc_heldchange) {
    758 		if (sc->sc_tx_busy) {
    759 			sc->sc_heldtbc = sc->sc_tbc;
    760 			sc->sc_tbc = 0;
    761 			sc->sc_heldchange = 1;
    762 		} else
    763 			ser_loadchannelregs(sc);
    764 	}
    765 
    766 	splx(s);
    767 
    768 	/*
    769 	 * Update the tty layer's idea of the carrier bit, in case we changed
    770 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that if we
    771 	 * lose carrier while carrier detection is on.
    772 	 */
    773 	(void)(*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MCR_DCD));
    774 
    775 #ifdef SER_DEBUG
    776 	serstatus(sc, "serparam ");
    777 #endif
    778 
    779 	/* XXXXX FIX ME */
    780 	/* Block or unblock as needed. */
    781 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    782 		if (sc->sc_rx_blocked) {
    783 			sc->sc_rx_blocked = 0;
    784 			ser_hwiflow(sc, 0);
    785 		}
    786 		if (sc->sc_tx_stopped) {
    787 			sc->sc_tx_stopped = 0;
    788 			serstart(tp);
    789 		}
    790 	} else {
    791 #if 0
    792 		sermsrint(sc, tp);
    793 #endif
    794 	}
    795 
    796 	return 0;
    797 }
    798 
    799 void
    800 ser_iflush(struct ser_softc *sc)
    801 {
    802 	uint8_t tmp;
    803 
    804 	/* flush any pending I/O */
    805 	while (ISSET(MFP->mf_rsr, RSR_CIP|RSR_BFULL))
    806 		tmp = MFP->mf_udr;
    807 }
    808 
    809 void
    810 ser_loadchannelregs(struct ser_softc *sc)
    811 {
    812 
    813 	/* XXXXX necessary? */
    814 	ser_iflush(sc);
    815 
    816 	/*
    817 	 * No interrupts please...
    818 	 */
    819 	if ((MFP->mf_imra & (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR)) != sc->sc_imra) {
    820 		printf("loadchannelregs: mf_imra: %x sc_imra: %x\n",
    821 		    (u_int)MFP->mf_imra, (u_int)sc->sc_imra);
    822 	}
    823 	if ((MFP->mf_imrb & (IB_SCTS|IB_SDCD)) != sc->sc_imrb) {
    824 		printf("loadchannelregs: mf_imrb: %x sc_imrb: %x\n",
    825 		     (u_int)MFP->mf_imrb, (u_int)sc->sc_imrb);
    826 	}
    827 	single_inst_bclr_b(MFP->mf_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
    828 	single_inst_bclr_b(MFP->mf_imrb, IB_SCTS|IB_SDCD);
    829 
    830 	MFP->mf_ucr = sc->sc_ucr;
    831 	MFP->mf_rsr = sc->sc_rsr;
    832 	MFP->mf_tsr = sc->sc_tsr;
    833 
    834 	single_inst_bclr_b(MFP->mf_tcdcr, 0x07);
    835 	MFP->mf_tddr  = sc->sc_ospeed;
    836 	single_inst_bset_b(MFP->mf_tcdcr, (sc->sc_ospeed >> 8) & 0x0f);
    837 
    838 	sc->sc_mcr_active = sc->sc_mcr;
    839 
    840 	if (machineid & ATARI_HADES) {
    841 		/* PCB fault, wires exchanged..... */
    842 		ym2149_rts(!(sc->sc_mcr_active & MCR_DTR));
    843 		ym2149_dtr(!(sc->sc_mcr_active & MCR_RTS));
    844 	} else {
    845 		ym2149_rts(!(sc->sc_mcr_active & MCR_RTS));
    846 		ym2149_dtr(!(sc->sc_mcr_active & MCR_DTR));
    847 	}
    848 
    849 	single_inst_bset_b(MFP->mf_imra, sc->sc_imra);
    850 	single_inst_bset_b(MFP->mf_imrb, sc->sc_imrb);
    851 }
    852 
    853 int
    854 serhwiflow(struct tty *tp, int block)
    855 {
    856 	struct ser_softc *sc =
    857 	    device_lookup_private(&ser_cd, SERUNIT(tp->t_dev));
    858 	int s;
    859 
    860 	if (sc->sc_mcr_rts == 0)
    861 		return 0;
    862 
    863 	s = splhigh();
    864 	if (block) {
    865 		/*
    866 		 * The tty layer is asking us to block input.
    867 		 * If we already did it, just return TRUE.
    868 		 */
    869 		if (sc->sc_rx_blocked)
    870 			goto out;
    871 		sc->sc_rx_blocked = 1;
    872 	} else {
    873 		/*
    874 		 * The tty layer is asking us to resume input.
    875 		 * The input ring is always empty by now.
    876 		 */
    877 		sc->sc_rx_blocked = 0;
    878 	}
    879 	ser_hwiflow(sc, block);
    880 out:
    881 	splx(s);
    882 	return 1;
    883 }
    884 
    885 /*
    886  * (un)block input via hw flowcontrol
    887  */
    888 void
    889 ser_hwiflow(struct ser_softc *sc, int block)
    890 {
    891 
    892 	if (sc->sc_mcr_rts == 0)
    893 		return;
    894 
    895 	if (block) {
    896 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
    897 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
    898 	} else {
    899 		SET(sc->sc_mcr, sc->sc_mcr_rts);
    900 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
    901 	}
    902 	if (machineid & ATARI_HADES) {
    903 		/* PCB fault, wires exchanged..... */
    904 		ym2149_dtr(sc->sc_mcr_active & MCR_RTS);
    905 	}
    906 	else {
    907 		ym2149_rts(sc->sc_mcr_active & MCR_RTS);
    908 	}
    909 }
    910 
    911 void
    912 serstart(struct tty *tp)
    913 {
    914 	struct ser_softc *sc =
    915 	    device_lookup_private(&ser_cd, SERUNIT(tp->t_dev));
    916 	int s;
    917 
    918 	s = spltty();
    919 	if (ISSET(tp->t_state, TS_BUSY))
    920 		goto out;
    921 	if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP))
    922 		goto stopped;
    923 
    924 	if (sc->sc_tx_stopped)
    925 		goto stopped;
    926 	if (!ttypull(tp))
    927 		goto stopped;
    928 
    929 	/* Grab the first contiguous region of buffer space. */
    930 	{
    931 		uint8_t *tba;
    932 		int tbc;
    933 
    934 		tba = tp->t_outq.c_cf;
    935 		tbc = ndqb(&tp->t_outq, 0);
    936 
    937 		(void)splhigh();
    938 
    939 		sc->sc_tba = tba;
    940 		sc->sc_tbc = tbc;
    941 	}
    942 
    943 	SET(tp->t_state, TS_BUSY);
    944 	sc->sc_tx_busy = 1;
    945 
    946 	/* Enable transmit completion interrupts if necessary. */
    947 	if (!ISSET(sc->sc_imra, IA_TRDY)) {
    948 		SET(sc->sc_imra, IA_TRDY|IA_TERR);
    949 		single_inst_bset_b(MFP->mf_imra, IA_TRDY|IA_TERR);
    950 	}
    951 
    952 	/* Output the first char */
    953 	MFP->mf_udr = *sc->sc_tba;
    954 	sc->sc_tbc--;
    955 	sc->sc_tba++;
    956 
    957 	splx(s);
    958 	return;
    959 
    960 stopped:
    961 	/* Disable transmit completion interrupts if necessary. */
    962 	if (ISSET(sc->sc_imra, IA_TRDY)) {
    963 		CLR(sc->sc_imra, IA_TRDY|IA_TERR);
    964 		single_inst_bclr_b(MFP->mf_imra, IA_TRDY|IA_TERR);
    965 	}
    966 out:
    967 	splx(s);
    968 	return;
    969 }
    970 
    971 /*
    972  * Stop output on a line.
    973  */
    974 void
    975 serstop(struct tty *tp, int flag)
    976 {
    977 	struct ser_softc *sc =
    978 	    device_lookup_private(&ser_cd, SERUNIT(tp->t_dev));
    979 	int s;
    980 
    981 	s = splhigh();
    982 	if (ISSET(tp->t_state, TS_BUSY)) {
    983 		/* Stop transmitting at the next chunk. */
    984 		sc->sc_tbc = 0;
    985 		sc->sc_heldtbc = 0;
    986 		if (!ISSET(tp->t_state, TS_TTSTOP))
    987 			SET(tp->t_state, TS_FLUSH);
    988 	}
    989 	splx(s);
    990 }
    991 
    992 void
    993 serdiag(void *arg)
    994 {
    995 	struct ser_softc *sc = arg;
    996 	int overflows, floods;
    997 	int s;
    998 
    999 	s = splhigh();
   1000 	overflows = sc->sc_overflows;
   1001 	sc->sc_overflows = 0;
   1002 	floods = sc->sc_floods;
   1003 	sc->sc_floods = 0;
   1004 	sc->sc_errors = 0;
   1005 	splx(s);
   1006 
   1007 	log(LOG_WARNING,
   1008 	    "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1009 	    sc->sc_dev.dv_xname,
   1010 	    overflows, overflows == 1 ? "" : "s",
   1011 	    floods, floods == 1 ? "" : "s");
   1012 }
   1013 
   1014 static void
   1015 ser_shutdown(struct ser_softc *sc)
   1016 {
   1017 	int s;
   1018 	struct tty *tp = sc->sc_tty;
   1019 
   1020 
   1021 	s = splhigh();
   1022 
   1023 	/* If we were asserting flow control, then deassert it. */
   1024 	sc->sc_rx_blocked = 1;
   1025 	ser_hwiflow(sc, 1);
   1026 
   1027 	/* Clear any break condition set with TIOCSBRK. */
   1028 	ser_break(sc, 0);
   1029 
   1030 	/*
   1031 	 * Hang up if necessary.  Wait a bit, so the other side has time to
   1032 	 * notice even if we immediately open the port again.
   1033 	 */
   1034 	if (ISSET(tp->t_cflag, HUPCL)) {
   1035 		ser_modem(sc, 0);
   1036 		(void)tsleep(sc, TTIPRI, ttclos, hz);
   1037 	}
   1038 
   1039 	/* Turn off interrupts. */
   1040 	CLR(sc->sc_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
   1041 	CLR(sc->sc_imrb, IB_SCTS|IB_SDCD);
   1042 	single_inst_bclr_b(MFP->mf_imrb, IB_SCTS|IB_SDCD);
   1043 	single_inst_bclr_b(MFP->mf_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
   1044 	splx(s);
   1045 }
   1046 
   1047 static void
   1048 serrxint(struct ser_softc *sc, struct tty *tp)
   1049 {
   1050 	u_int get, cc, scc;
   1051 	int code;
   1052 	uint8_t rsr;
   1053 	int s;
   1054 	static int lsrmap[8] = {
   1055 		0,      TTY_PE,
   1056 		TTY_FE, TTY_PE|TTY_FE,
   1057 		TTY_FE, TTY_PE|TTY_FE,
   1058 		TTY_FE, TTY_PE|TTY_FE
   1059 	};
   1060 
   1061 	get = sc->sc_rbget;
   1062 	scc = cc = RXBUFSIZE - sc->sc_rbavail;
   1063 
   1064 	if (cc == RXBUFSIZE) {
   1065 		sc->sc_floods++;
   1066 		if (sc->sc_errors++ == 0)
   1067 			callout_reset(&sc->sc_diag_ch, 60 * hz, serdiag, sc);
   1068 	}
   1069 
   1070 	while (cc--) {
   1071 		rsr = sc->sc_lbuf[get];
   1072 		if (ISSET(rsr, RSR_BREAK)) {
   1073 #ifdef DDB
   1074 			if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE))
   1075 				Debugger();
   1076 #endif
   1077 		} else if (ISSET(rsr, RSR_OERR)) {
   1078 			sc->sc_overflows++;
   1079 			if (sc->sc_errors++ == 0)
   1080 				callout_reset(&sc->sc_diag_ch, 60 * hz,
   1081 				    serdiag, sc);
   1082 		}
   1083 		code = sc->sc_rbuf[get] |
   1084 		    lsrmap[(rsr & (RSR_BREAK|RSR_FERR|RSR_PERR)) >> 3];
   1085 		(*tp->t_linesw->l_rint)(code, tp);
   1086 		get = (get + 1) & RXBUFMASK;
   1087 	}
   1088 
   1089 	sc->sc_rbget = get;
   1090 	s = splhigh();
   1091 	sc->sc_rbavail += scc;
   1092 	/*
   1093 	 * Buffers should be ok again, release possible block, but only if the
   1094 	 * tty layer isn't blocking too.
   1095 	 */
   1096 	if (sc->sc_rx_blocked && !ISSET(tp->t_state, TS_TBLOCK)) {
   1097 		sc->sc_rx_blocked = 0;
   1098 		ser_hwiflow(sc, 0);
   1099 	}
   1100 	splx(s);
   1101 }
   1102 
   1103 static void
   1104 sertxint(struct ser_softc *sc, struct tty *tp)
   1105 {
   1106 
   1107 	CLR(tp->t_state, TS_BUSY);
   1108 	if (ISSET(tp->t_state, TS_FLUSH))
   1109 		CLR(tp->t_state, TS_FLUSH);
   1110 	else
   1111 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1112 	(*tp->t_linesw->l_start)(tp);
   1113 }
   1114 
   1115 static void
   1116 sermsrint(struct ser_softc *sc, struct tty *tp)
   1117 {
   1118 	uint8_t msr, delta;
   1119 	int s;
   1120 
   1121 	s = splhigh();
   1122 	msr = sc->sc_msr;
   1123 	delta = sc->sc_msr_delta;
   1124 	sc->sc_msr_delta = 0;
   1125 	splx(s);
   1126 
   1127 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1128 		/*
   1129 		 * Inform the tty layer that carrier detect changed.
   1130 		 */
   1131 		(void)(*tp->t_linesw->l_modem)(tp, ISSET(msr, MCR_DCD));
   1132 	}
   1133 
   1134 	if (ISSET(delta, sc->sc_msr_cts)) {
   1135 		/* Block or unblock output according to flow control. */
   1136 		if (ISSET(msr, sc->sc_msr_cts)) {
   1137 			sc->sc_tx_stopped = 0;
   1138 			(*tp->t_linesw->l_start)(tp);
   1139 		} else {
   1140 			sc->sc_tx_stopped = 1;
   1141 			serstop(tp, 0);
   1142 		}
   1143 	}
   1144 
   1145 #ifdef SER_DEBUG
   1146 	serstatus(sc, "sermsrint");
   1147 #endif
   1148 }
   1149 
   1150 void
   1151 sersoft(void *arg)
   1152 {
   1153 	struct ser_softc *sc = arg;
   1154 	struct tty *tp;
   1155 
   1156 	tp = sc->sc_tty;
   1157 	if (tp == NULL)
   1158 		return;
   1159 
   1160 	if (!ISSET(tp->t_state, TS_ISOPEN) && (tp->t_wopen == 0))
   1161 		return;
   1162 
   1163 	if (sc->sc_rx_ready) {
   1164 		sc->sc_rx_ready = 0;
   1165 		serrxint(sc, tp);
   1166 	}
   1167 
   1168 	if (sc->sc_st_check) {
   1169 		sc->sc_st_check = 0;
   1170 		sermsrint(sc, tp);
   1171 	}
   1172 
   1173 	if (sc->sc_tx_done) {
   1174 		sc->sc_tx_done = 0;
   1175 		sertxint(sc, tp);
   1176 	}
   1177 }
   1178 
   1179 int
   1180 sermintr(void *arg)
   1181 {
   1182 	struct ser_softc *sc = arg;
   1183 	uint8_t msr, delta;
   1184 
   1185 	msr = ~MFP->mf_gpip;
   1186 	delta = msr ^ sc->sc_msr;
   1187 	sc->sc_msr  = sc->sc_msr & ~(MCR_CTS|MCR_DCD|MCR_RI);
   1188 	sc->sc_msr |= msr & (MCR_CTS|MCR_DCD|MCR_RI);
   1189 
   1190 	if (ISSET(delta, sc->sc_msr_mask)) {
   1191 		sc->sc_msr_delta |= delta;
   1192 
   1193 		/*
   1194 		 * Stop output immediately if we lose the output
   1195 		 * flow control signal or carrier detect.
   1196 		 */
   1197 		if (ISSET(~msr, sc->sc_msr_mask)) {
   1198 			sc->sc_tbc = 0;
   1199 			sc->sc_heldtbc = 0;
   1200 #ifdef SER_DEBUG
   1201 			serstatus(sc, "sermintr  ");
   1202 #endif
   1203 		}
   1204 
   1205 		sc->sc_st_check = 1;
   1206 	}
   1207 	softint_schedule(sc->sc_sicookie);
   1208 	return 1;
   1209 }
   1210 
   1211 int
   1212 sertrintr(void *arg)
   1213 {
   1214 	struct ser_softc *sc = arg;
   1215 	u_int put, cc;
   1216 	uint8_t rsr, tsr;
   1217 
   1218 	put = sc->sc_rbput;
   1219 	cc = sc->sc_rbavail;
   1220 
   1221 	rsr = MFP->mf_rsr;
   1222 	if (ISSET(rsr, RSR_BFULL|RSR_BREAK)) {
   1223 		for (; ISSET(rsr, RSR_BFULL|RSR_BREAK) && cc > 0; cc--) {
   1224 			sc->sc_rbuf[put] = MFP->mf_udr;
   1225 			sc->sc_lbuf[put] = rsr;
   1226 			put = (put + 1) & RXBUFMASK;
   1227 			if ((rsr & RSR_BREAK) && (MFP->mf_rsr & RSR_BREAK))
   1228 				rsr = 0;
   1229 			else
   1230 				rsr = MFP->mf_rsr;
   1231 		}
   1232 		/*
   1233 		 * Current string of incoming characters ended because
   1234 		 * no more data was available. Schedule a receive event
   1235 		 * if any data was received. Drop any characters that
   1236 		 * we couldn't handle.
   1237 		 */
   1238 		sc->sc_rbput    = put;
   1239 		sc->sc_rbavail  = cc;
   1240 		sc->sc_rx_ready = 1;
   1241 		/*
   1242 		 * See if we are in danger of overflowing a buffer. If
   1243 		 * so, use hardware flow control to ease the pressure.
   1244 		 */
   1245 		if (sc->sc_rx_blocked == 0 &&
   1246 		    cc < sc->sc_r_hiwat) {
   1247 			sc->sc_rx_blocked = 1;
   1248 			ser_hwiflow(sc, 1);
   1249 		}
   1250 		/*
   1251 		 * If we're out of space, throw away any further input.
   1252 		 */
   1253 		if (!cc) {
   1254 			while (ISSET(rsr, RSR_BFULL|RSR_BREAK)) {
   1255 				rsr = MFP->mf_udr;
   1256 				rsr = MFP->mf_rsr;
   1257 			}
   1258 		}
   1259 	}
   1260 
   1261 	/*
   1262 	 * Done handling any receive interrupts. See if data can be
   1263 	 * transmitted as well. Schedule tx done event if no data left
   1264 	 * and tty was marked busy.
   1265 	 */
   1266 	tsr = MFP->mf_tsr;
   1267 	if (ISSET(tsr, TSR_BE)) {
   1268 		/*
   1269 		 * If we've delayed a parameter change, do it now, and restart
   1270 		 * output.
   1271 		 */
   1272 		if (sc->sc_heldchange) {
   1273 			ser_loadchannelregs(sc);
   1274 			sc->sc_heldchange = 0;
   1275 			sc->sc_tbc = sc->sc_heldtbc;
   1276 			sc->sc_heldtbc = 0;
   1277 		}
   1278 		/* Output the next character, if any. */
   1279 		if (sc->sc_tbc > 0) {
   1280 			MFP->mf_udr = *sc->sc_tba;
   1281 			sc->sc_tbc--;
   1282 			sc->sc_tba++;
   1283 		} else if (sc->sc_tx_busy) {
   1284 			sc->sc_tx_busy = 0;
   1285 			sc->sc_tx_done = 1;
   1286 		}
   1287 	}
   1288 	softint_schedule(sc->sc_sicookie);
   1289 	return 1;
   1290 }
   1291 
   1292 static int
   1293 serspeed(long speed)
   1294 {
   1295 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
   1296 
   1297 	int div, x, err;
   1298 
   1299 	if (speed <= 0)
   1300 		return -1;
   1301 
   1302 	for (div = 4; div <= 64; div *= 4) {
   1303 		x = divrnd((SER_FREQ / div), speed);
   1304 
   1305 		/*
   1306 		 * The value must fit in the timer-d dataregister. If
   1307 		 * not, try another delay-mode.
   1308 		 */
   1309 		if ((x / 2) > 255)
   1310 			continue;
   1311 
   1312 		/*
   1313 		 * Baudrate to high for the interface or cannot be made
   1314 		 * within tolerance.
   1315 		 */
   1316 		if (x <= 0)
   1317 			return -1;
   1318 
   1319 		err = divrnd((SER_FREQ / div) * 1000, speed * x) - 1000;
   1320 		if (err < 0)
   1321 			err = -err;
   1322 		if (err > SER_TOLERANCE)
   1323 			continue;
   1324 
   1325 		/*
   1326 		 * Translate 'div' to delay-code
   1327 		 */
   1328 		if (div == 4)
   1329 			div = 1;
   1330 		else if (div == 16)
   1331 			div = 3;
   1332 		else if (div == 64)
   1333 			div = 5;
   1334 
   1335 		return (x / 2) | (div << 8);
   1336 	}
   1337 	return -1;
   1338 
   1339 #undef	divrnd
   1340 }
   1341 
   1342 /*
   1343  * Following are all routines needed for SER to act as console
   1344  */
   1345 #include <dev/cons.h>
   1346 
   1347 void
   1348 sercnprobe(struct consdev *cp)
   1349 {
   1350 
   1351 	/*
   1352 	 * Activate serial console when DCD present...
   1353 	 */
   1354 	if (MFP->mf_gpip & MCR_DCD) {
   1355 		cp->cn_pri = CN_DEAD;
   1356 		return;
   1357 	}
   1358 
   1359 	/* initialize required fields */
   1360 	/* XXX: LWP What unit? */
   1361 	cp->cn_dev = makedev(cdevsw_lookup_major(&ser_cdevsw), 0);
   1362 #if SERCONSOLE > 0
   1363 	cp->cn_pri = CN_REMOTE;	/* Force a serial port console */
   1364 #else
   1365 	cp->cn_pri = CN_NORMAL;
   1366 #endif /* SERCONSOLE > 0 */
   1367 }
   1368 
   1369 void
   1370 sercninit(struct consdev *cp)
   1371 {
   1372 
   1373 	serinitcons(CONSBAUD);
   1374 }
   1375 
   1376 /*
   1377  * Initialize UART to known state.
   1378  */
   1379 void
   1380 serinit(int baud)
   1381 {
   1382 	int ospeed = serspeed(baud);
   1383 
   1384 	MFP->mf_ucr = UCR_CLKDIV|UCR_8BITS|UCR_STOPB1;
   1385 	MFP->mf_rsr = RSR_ENAB;
   1386 	MFP->mf_tsr = TSR_ENAB;
   1387 
   1388 	single_inst_bclr_b(MFP->mf_tcdcr, 0x07);
   1389 	MFP->mf_tddr  = ospeed;
   1390 	single_inst_bset_b(MFP->mf_tcdcr, (ospeed >> 8) & 0x0f);
   1391 }
   1392 
   1393 /*
   1394  * Set UART for console use. Do normal init, then enable interrupts.
   1395  */
   1396 void
   1397 serinitcons(int baud)
   1398 {
   1399 
   1400 	serinit(baud);
   1401 
   1402 	/* Set rts/dtr */
   1403 	ym2149_rts(0);
   1404 	ym2149_dtr(0);
   1405 
   1406 	single_inst_bset_b(MFP->mf_imra, (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR));
   1407 }
   1408 
   1409 int
   1410 sercngetc(dev_t dev)
   1411 {
   1412 	uint8_t stat, c;
   1413 	int s;
   1414 
   1415 	s = splhigh();
   1416 	while (!ISSET(stat = MFP->mf_rsr, RSR_BFULL)) {
   1417 		if (!ISSET(stat, RSR_ENAB)) /* XXX */
   1418 			MFP->mf_rsr |= RSR_ENAB;
   1419 		if (stat & (RSR_FERR|RSR_PERR|RSR_OERR))
   1420 			c = MFP->mf_udr;
   1421 	}
   1422 	c = MFP->mf_udr;
   1423 	splx(s);
   1424 	return c;
   1425 }
   1426 
   1427 #if 1
   1428 u_int s_imra;
   1429 u_int s_stat1, s_stat2, s_stat3;
   1430 #endif
   1431 void
   1432 sercnputc(dev_t dev, int c)
   1433 {
   1434 	int timo;
   1435 	uint8_t stat, imra;
   1436 
   1437 	/* Mask serial interrupts */
   1438 	imra  = MFP->mf_imra & (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
   1439 	single_inst_bclr_b(MFP->mf_imra, imra);
   1440 #if 1
   1441 	s_imra = imra;
   1442 #endif
   1443 
   1444 	/* wait for any pending transmission to finish */
   1445 	timo = 50000;
   1446 #if 1
   1447 	s_stat1 = MFP->mf_tsr;
   1448 #endif
   1449 	while (!ISSET(stat = MFP->mf_tsr, TSR_BE) && --timo)
   1450 		;
   1451 	MFP->mf_udr = c;
   1452 	/* wait for this transmission to complete */
   1453 	timo = 1500000;
   1454 #if 1
   1455 	s_stat2 = MFP->mf_tsr;
   1456 #endif
   1457 	while (!ISSET(stat = MFP->mf_tsr, TSR_BE) && --timo)
   1458 		;
   1459 
   1460 #if 1
   1461 	s_stat3 = MFP->mf_tsr;
   1462 #endif
   1463 	/* Clear pending serial interrupts and re-enable */
   1464 	MFP->mf_ipra = (uint8_t)~imra;
   1465 	single_inst_bset_b(MFP->mf_imra, imra);
   1466 }
   1467 
   1468 void
   1469 sercnpollc(dev_t dev, int on)
   1470 {
   1471 
   1472 }
   1473