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