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