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