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