Home | History | Annotate | Line # | Download | only in dev
mfc.c revision 1.24.4.1
      1 /*	$NetBSD: mfc.c,v 1.24.4.1 2001/10/10 11:55:51 fvdl Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1994 Michael L. Hitch
      5  * Copyright (c) 1982, 1990 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #include "opt_kgdb.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/device.h>
     43 #include <sys/tty.h>
     44 #include <sys/proc.h>
     45 #include <sys/file.h>
     46 #include <sys/malloc.h>
     47 #include <sys/uio.h>
     48 #include <sys/kernel.h>
     49 #include <sys/syslog.h>
     50 #include <sys/queue.h>
     51 #include <sys/vnode.h>
     52 #include <machine/cpu.h>
     53 #include <amiga/amiga/device.h>
     54 #include <amiga/amiga/isr.h>
     55 #include <amiga/amiga/custom.h>
     56 #include <amiga/amiga/cia.h>
     57 #include <amiga/amiga/cc.h>
     58 #include <amiga/dev/zbusvar.h>
     59 
     60 #include <dev/cons.h>
     61 
     62 #include <sys/conf.h>
     63 #include <machine/conf.h>
     64 
     65 #include "mfcs.h"
     66 
     67 #ifndef SEROBUF_SIZE
     68 #define SEROBUF_SIZE	128
     69 #endif
     70 #ifndef SERIBUF_SIZE
     71 #define SERIBUF_SIZE	1024
     72 #endif
     73 
     74 #define splser()	spl6()
     75 
     76 /*
     77  * 68581 DUART registers
     78  */
     79 struct mfc_regs {
     80 	volatile u_char du_mr1a;
     81 #define	du_mr2a		du_mr1a
     82 	u_char pad0;
     83 	volatile u_char du_csra;
     84 #define	du_sra		du_csra
     85 	u_char pad2;
     86 	volatile u_char du_cra;
     87 	u_char pad4;
     88 	volatile u_char du_tba;
     89 #define	du_rba		du_tba
     90 	u_char pad6;
     91 	volatile u_char du_acr;
     92 #define	du_ipcr		du_acr
     93 	u_char pad8;
     94 	volatile u_char du_imr;
     95 #define	du_isr		du_imr
     96 	u_char pad10;
     97 	volatile u_char du_ctur;
     98 #define	du_cmsb		du_ctur
     99 	u_char pad12;
    100 	volatile u_char du_ctlr;
    101 #define	du_clsb		du_ctlr
    102 	u_char pad14;
    103 	volatile u_char du_mr1b;
    104 #define	du_mr2b		du_mr1b
    105 	u_char pad16;
    106 	volatile u_char du_csrb;
    107 #define	du_srb		du_csrb
    108 	u_char pad18;
    109 	volatile u_char du_crb;
    110 	u_char pad20;
    111 	volatile u_char du_tbb;
    112 #define	du_rbb		du_tbb
    113 	u_char pad22;
    114 	volatile u_char du_ivr;
    115 	u_char pad24;
    116 	volatile u_char du_opcr;
    117 #define	du_ip		du_opcr
    118 	u_char pad26;
    119 	volatile u_char du_btst;
    120 #define	du_strc		du_btst
    121 	u_char pad28;
    122 	volatile u_char du_btrst;
    123 #define	du_stpc		du_btrst
    124 	u_char pad30;
    125 };
    126 
    127 /*
    128  * 68681 DUART serial port registers
    129  */
    130 struct duart_regs {
    131 	volatile u_char ch_mr1;
    132 #define	ch_mr2		ch_mr1
    133 	u_char pad0;
    134 	volatile u_char	ch_csr;
    135 #define	ch_sr		ch_csr
    136 	u_char pad1;
    137 	volatile u_char	ch_cr;
    138 	u_char pad2;
    139 	volatile u_char	ch_tb;
    140 #define	ch_rb		ch_tb
    141 	u_char pad3;
    142 };
    143 
    144 struct mfc_softc {
    145 	struct	device sc_dev;
    146 	struct	isr sc_isr;
    147 	struct	mfc_regs *sc_regs;
    148 	u_long	clk_frq;
    149 	u_short	ct_val;
    150 	u_char	ct_usecnt;
    151 	u_char	imask;
    152 	u_char	mfc_iii;
    153 	u_char	last_ip;
    154 };
    155 
    156 #if NMFCS > 0
    157 struct mfcs_softc {
    158 	struct	device sc_dev;
    159 	struct	tty *sc_tty;
    160 	struct	duart_regs *sc_duart;
    161 	struct	mfc_regs *sc_regs;
    162 	struct	mfc_softc *sc_mfc;
    163 	int	swflags;
    164 	long	flags;			/* XXX */
    165 #define CT_USED	1			/* CT in use */
    166 	u_short	*rptr, *wptr, incnt, ovfl;
    167 	u_short	inbuf[SERIBUF_SIZE];
    168 	char	*ptr, *end;
    169 	char	outbuf[SEROBUF_SIZE];
    170 	struct vbl_node vbl_node;
    171 };
    172 #endif
    173 
    174 #if NMFCP > 0
    175 struct mfcp_softc {
    176 };
    177 #endif
    178 
    179 struct mfc_args {
    180 	struct zbus_args zargs;
    181 	char	*subdev;
    182 	char	unit;
    183 };
    184 
    185 int	mfcprint __P((void *auxp, const char *));
    186 void	mfcattach __P((struct device *, struct device *, void *));
    187 int	mfcmatch __P((struct device *, struct cfdata *, void *));
    188 
    189 #if NMFCS > 0
    190 int	mfcsmatch __P((struct device *, struct cfdata *, void *));
    191 void	mfcsattach __P((struct device *, struct device *, void *));
    192 int	mfcsparam __P(( struct tty *, struct termios *));
    193 int	mfcshwiflow __P((struct tty *, int));
    194 void	mfcsstart __P((struct tty *));
    195 int	mfcsmctl __P((struct vnode *, int, int));
    196 void	mfcsxintr __P((int));
    197 void	mfcseint __P((int, int));
    198 void	mfcsmint __P((register int));
    199 #endif
    200 
    201 #if NMFCP > 0
    202 void mfcpattach __P((struct device *, struct device *, void *));
    203 int mfcpmatch __P((struct device *, struct cfdata *, void *));
    204 #endif
    205 int mfcintr __P((void *));
    206 
    207 struct cfattach mfc_ca = {
    208 	sizeof(struct mfc_softc), mfcmatch, mfcattach
    209 };
    210 
    211 #if NMFCS > 0
    212 struct cfattach mfcs_ca = {
    213 	sizeof(struct mfcs_softc), mfcsmatch, mfcsattach
    214 };
    215 
    216 extern struct cfdriver mfcs_cd;
    217 #endif
    218 
    219 #if NMFCP > 0
    220 struct cfattach mfcp_ca = {
    221 	sizeof(struct mfcp_softc, mfcpmatch, mfcpattach
    222 };
    223 #endif
    224 
    225 
    226 int	mfcs_active;
    227 int	mfcsdefaultrate = 38400 /*TTYDEF_SPEED*/;
    228 #define SWFLAGS(dev) (sc->swflags | (((dev) & 0x80) == 0 ? TIOCFLAG_SOFTCAR : 0))
    229 
    230 #ifdef notyet
    231 /*
    232  * MultiFaceCard III, II+ (not supported yet), and
    233  * SerialMaster 500+ (not supported yet)
    234  * baud rate tables for BRG set 1 [not used yet]
    235  */
    236 
    237 struct speedtab mfcs3speedtab1[] = {
    238 	{ 0,		0	},
    239 	{ 100,		0x00	},
    240 	{ 220,		0x11	},
    241 	{ 600,		0x44	},
    242 	{ 1200,		0x55	},
    243 	{ 2400,		0x66	},
    244 	{ 4800,		0x88	},
    245 	{ 9600,		0x99	},
    246 	{ 19200,	0xbb	},
    247 	{ 115200,	0xcc	},
    248 	{ -1,		-1	}
    249 };
    250 
    251 /*
    252  * MultiFaceCard II, I, and SerialMaster 500
    253  * baud rate tables for BRG set 1 [not used yet]
    254  */
    255 
    256 struct speedtab mfcs2speedtab1[] = {
    257 	{ 0,		0	},
    258 	{ 50,		0x00	},
    259 	{ 110,		0x11	},
    260 	{ 300,		0x44	},
    261 	{ 600,		0x55	},
    262 	{ 1200,		0x66	},
    263 	{ 2400,		0x88	},
    264  	{ 4800,		0x99	},
    265 	{ 9600,		0xbb	},
    266 	{ 38400,	0xcc	},
    267 	{ -1,		-1	}
    268 };
    269 #endif
    270 
    271 /*
    272  * MultiFaceCard III, II+ (not supported yet), and
    273  * SerialMaster 500+ (not supported yet)
    274  * baud rate tables for BRG set 2
    275  */
    276 
    277 struct speedtab mfcs3speedtab2[] = {
    278 	{ 0,		0	},
    279 	{ 150,		0x00	},
    280 	{ 200,		0x11	},
    281 	{ 300,		0x33	},
    282 	{ 600,		0x44	},
    283 	{ 1200,		0x55	},
    284 	{ 2400,		0x66	},
    285 	{ 4800,		0x88	},
    286 	{ 9600,		0x99	},
    287 	{ 19200,	0xbb	},
    288 	{ 38400,	0xcc	},
    289 	{ -1,		-1	}
    290 };
    291 
    292 /*
    293  * MultiFaceCard II, I, and SerialMaster 500
    294  * baud rate tables for BRG set 2
    295  */
    296 
    297 struct speedtab mfcs2speedtab2[] = {
    298 	{ 0,		0	},
    299 	{ 75,		0x00	},
    300 	{ 100,		0x11	},
    301 	{ 150,		0x33	},
    302 	{ 300,		0x44	},
    303 	{ 600,		0x55	},
    304 	{ 1200,		0x66	},
    305 	{ 2400,		0x88	},
    306  	{ 4800,		0x99	},
    307 	{ 9600,		0xbb	},
    308 	{ 19200,	0xcc	},
    309 	{ -1,		-1	}
    310 };
    311 
    312 /*
    313  * if we are an bsc/Alf Data MultFaceCard (I, II, and III)
    314  */
    315 int
    316 mfcmatch(pdp, cfp, auxp)
    317 	struct device *pdp;
    318 	struct cfdata *cfp;
    319 	void *auxp;
    320 {
    321 	struct zbus_args *zap;
    322 
    323 	zap = auxp;
    324 	if (zap->manid == 2092 &&
    325 	    (zap->prodid == 16 || zap->prodid == 17 || zap->prodid == 18))
    326 
    327 		return(1);
    328 	return(0);
    329 }
    330 
    331 void
    332 mfcattach(pdp, dp, auxp)
    333 	struct device *pdp, *dp;
    334 	void *auxp;
    335 {
    336 	struct mfc_softc *scc;
    337 	struct zbus_args *zap;
    338 	struct mfc_args ma;
    339 	int unit;
    340 	struct mfc_regs *rp;
    341 
    342 	zap = auxp;
    343 
    344 	printf ("\n");
    345 
    346 	scc = (struct mfc_softc *)dp;
    347 	unit = scc->sc_dev.dv_unit;
    348 	scc->sc_regs = rp = zap->va;
    349 	if (zap->prodid == 18)
    350 		scc->mfc_iii = 3;
    351 	scc->clk_frq = scc->mfc_iii ? 230400 : 115200;
    352 
    353 	rp->du_opcr = 0x00;		/* configure output port? */
    354 	rp->du_btrst = 0x0f;		/* clear modem lines */
    355 	rp->du_ivr = 0;			/* IVR */
    356 	rp->du_imr = 0;			/* IMR */
    357 	rp->du_acr = 0xe0;		/* baud rate generate set 2 */
    358 	rp->du_ctur = 0;
    359 	rp->du_ctlr = 4;
    360 	rp->du_csra = 0xcc;		/* clock select = 38400 */
    361 	rp->du_cra = 0x10;		/* reset mode register ptr */
    362 	rp->du_cra = 0x20;
    363 	rp->du_cra = 0x30;
    364 	rp->du_cra = 0x40;
    365 	rp->du_mr1a = 0x93;		/* MRA1 */
    366 	rp->du_mr2a = 0x17;		/* MRA2 */
    367 	rp->du_csrb = 0xcc;		/* clock select = 38400 */
    368 	rp->du_crb = 0x10;		/* reset mode register ptr */
    369 	rp->du_crb = 0x20;
    370 	rp->du_crb = 0x30;
    371 	rp->du_crb = 0x40;
    372 	rp->du_mr1b = 0x93;		/* MRB1 */
    373 	rp->du_mr2b = 0x17;		/* MRB2 */
    374 	rp->du_cra = 0x05;		/* enable A Rx & Tx */
    375 	rp->du_crb = 0x05;		/* enable B Rx & Tx */
    376 
    377 	scc->sc_isr.isr_intr = mfcintr;
    378 	scc->sc_isr.isr_arg = scc;
    379 	scc->sc_isr.isr_ipl = 6;
    380 	add_isr(&scc->sc_isr);
    381 
    382 	/* configure ports */
    383 	bcopy(zap, &ma.zargs, sizeof(struct zbus_args));
    384 	ma.subdev = "mfcs";
    385 	ma.unit = unit * 2;
    386 	config_found(dp, &ma, mfcprint);
    387 	ma.unit = unit * 2 + 1;
    388 	config_found(dp, &ma, mfcprint);
    389 	ma.subdev = "mfcp";
    390 	ma.unit = unit;
    391 	config_found(dp, &ma, mfcprint);
    392 }
    393 
    394 /*
    395  *
    396  */
    397 int
    398 mfcsmatch(pdp, cfp, auxp)
    399 	struct device *pdp;
    400 	struct cfdata *cfp;
    401 	void *auxp;
    402 {
    403 	struct mfc_args *ma;
    404 
    405 	ma = auxp;
    406 	if (strcmp(ma->subdev, "mfcs") == 0)
    407 		return (1);
    408 	return (0);
    409 }
    410 
    411 void
    412 mfcsattach(pdp, dp, auxp)
    413 	struct device *pdp, *dp;
    414 	void *auxp;
    415 {
    416 	int unit;
    417 	struct mfcs_softc *sc;
    418 	struct mfc_softc *scc;
    419 	struct mfc_args *ma;
    420 	struct mfc_regs *rp;
    421 
    422 	sc = (struct mfcs_softc *) dp;
    423 	scc = (struct mfc_softc *) pdp;
    424 	ma = auxp;
    425 
    426 	if (dp) {
    427 		printf (": input fifo %d output fifo %d\n", SERIBUF_SIZE,
    428 		    SEROBUF_SIZE);
    429 		alloc_sicallback();
    430 	}
    431 
    432 	unit = ma->unit;
    433 	mfcs_active |= 1 << unit;
    434 	sc->rptr = sc->wptr = sc->inbuf;
    435 	sc->sc_mfc = scc;
    436 	sc->sc_regs = rp = scc->sc_regs;
    437 	sc->sc_duart = (struct duart_regs *) ((unit & 1) ? &rp->du_mr1b :
    438 	    &rp->du_mr1a);
    439 	/*
    440 	 * should have only one vbl routine to handle all ports?
    441 	 */
    442 	sc->vbl_node.function = (void (*) (void *)) mfcsmint;
    443 	sc->vbl_node.data = (void *) unit;
    444 	add_vbl_function(&sc->vbl_node, 1, (void *) unit);
    445 }
    446 
    447 /*
    448  * print diag if pnp is NULL else just extra
    449  */
    450 int
    451 mfcprint(auxp, pnp)
    452 	void *auxp;
    453 	const char *pnp;
    454 {
    455 	if (pnp == NULL)
    456 		return(UNCONF);
    457 	return(QUIET);
    458 }
    459 
    460 int
    461 mfcsopen(devvp, flag, mode, p)
    462 	struct vnode *devvp;
    463 	int flag, mode;
    464 	struct proc *p;
    465 {
    466 	struct tty *tp;
    467 	struct mfcs_softc *sc;
    468 	int unit, error, s;
    469 	dev_t dev;
    470 
    471 	dev = vdev_rdev(devvp);
    472 	error = 0;
    473 	unit = dev & 0x1f;
    474 
    475 	if (unit >= mfcs_cd.cd_ndevs || (mfcs_active & (1 << unit)) == 0)
    476 		return (ENXIO);
    477 	sc = mfcs_cd.cd_devs[unit];
    478 
    479 	s = spltty();
    480 
    481 	if (sc->sc_tty)
    482 		tp = sc->sc_tty;
    483 	else {
    484 		tp = sc->sc_tty = ttymalloc();
    485 		tty_attach(tp);
    486 	}
    487 
    488 	tp->t_oproc = (void (*) (struct tty *)) mfcsstart;
    489 	tp->t_param = mfcsparam;
    490 	tp->t_devvp = devvp;
    491 	tp->t_hwiflow = mfcshwiflow;
    492 
    493 	vdev_setprivdata(devvp, sc);
    494 
    495 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
    496 		ttychars(tp);
    497 		if (tp->t_ispeed == 0) {
    498 			/*
    499 			 * only when cleared do we reset to defaults.
    500 			 */
    501 			tp->t_iflag = TTYDEF_IFLAG;
    502 			tp->t_oflag = TTYDEF_OFLAG;
    503 			tp->t_cflag = TTYDEF_CFLAG;
    504 			tp->t_lflag = TTYDEF_LFLAG;
    505 			tp->t_ispeed = tp->t_ospeed = mfcsdefaultrate;
    506 		}
    507 		/*
    508 		 * do these all the time
    509 		 */
    510 		if (sc->swflags & TIOCFLAG_CLOCAL)
    511 			tp->t_cflag |= CLOCAL;
    512 		if (sc->swflags & TIOCFLAG_CRTSCTS)
    513 			tp->t_cflag |= CRTSCTS;
    514 		if (sc->swflags & TIOCFLAG_MDMBUF)
    515 			tp->t_cflag |= MDMBUF;
    516 		mfcsparam(tp, &tp->t_termios);
    517 		ttsetwater(tp);
    518 
    519 		(void)mfcsmctl(devvp, TIOCM_DTR | TIOCM_RTS, DMSET);
    520 		if ((SWFLAGS(dev) & TIOCFLAG_SOFTCAR) ||
    521 		    (mfcsmctl(devvp, 0, DMGET) & TIOCM_CD))
    522 			tp->t_state |= TS_CARR_ON;
    523 		else
    524 			tp->t_state &= ~TS_CARR_ON;
    525 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
    526 		splx(s);
    527 		return(EBUSY);
    528 	}
    529 
    530 	/*
    531 	 * if NONBLOCK requested, ignore carrier
    532 	 */
    533 	if (flag & O_NONBLOCK)
    534 		goto done;
    535 
    536 	/*
    537 	 * block waiting for carrier
    538 	 */
    539 	while ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_cflag & CLOCAL) == 0) {
    540 		tp->t_wopen++;
    541 		error = ttysleep(tp, (caddr_t)&tp->t_rawq,
    542 		    TTIPRI | PCATCH, ttopen, 0);
    543 		tp->t_wopen--;
    544 		if (error) {
    545 			splx(s);
    546 			return(error);
    547 		}
    548 	}
    549 done:
    550 	/* This is a way to handle lost XON characters */
    551 	if ((flag & O_TRUNC) && (tp->t_state & TS_TTSTOP)) {
    552 		tp->t_state &= ~TS_TTSTOP;
    553 	        ttstart (tp);
    554 	}
    555 
    556 	splx(s);
    557 	/*
    558 	 * Reset the tty pointer, as there could have been a dialout
    559 	 * use of the tty with a dialin open waiting.
    560 	 */
    561 	tp->t_devvp = devvp;
    562 	return tp->t_linesw->l_open(devvp, tp);
    563 }
    564 
    565 /*ARGSUSED*/
    566 int
    567 mfcsclose(devvp, flag, mode, p)
    568 	struct vnode *devvp;
    569 	int flag, mode;
    570 	struct proc *p;
    571 {
    572 	struct tty *tp;
    573 	struct mfcs_softc *sc;
    574 	struct mfc_softc *scc;
    575 	int unit;
    576 
    577 	sc = vdev_privdata(devvp);
    578 	scc= sc->sc_mfc;
    579 	unit = vdev_rdev(devvp) & 31;
    580 
    581 	tp = sc->sc_tty;
    582 	tp->t_linesw->l_close(tp, flag);
    583 	sc->sc_duart->ch_cr = 0x70;			/* stop break */
    584 
    585 	scc->imask &= ~(0x7 << ((unit & 1) * 4));
    586 	scc->sc_regs->du_imr = scc->imask;
    587 	if (sc->flags & CT_USED) {
    588 		--scc->ct_usecnt;
    589 		sc->flags &= ~CT_USED;
    590 	}
    591 
    592 	/*
    593 	 * If the device is closed, it's close, no matter whether we deal with
    594 	 * modem control signals nor not.
    595 	 */
    596 #if 0
    597 	if (tp->t_cflag & HUPCL || tp->t_wopen != 0 ||
    598 	    (tp->t_state & TS_ISOPEN) == 0)
    599 #endif
    600 		(void) mfcsmctl(devvp, 0, DMSET);
    601 	ttyclose(tp);
    602 #if not_yet
    603 	if (tp != &mfcs_cons) {
    604 		remove_vbl_function(&sc->vbl_node);
    605 		ttyfree(tp);
    606 		sc->sc_tty = (struct tty *) NULL;
    607 	}
    608 #endif
    609 	return (0);
    610 }
    611 
    612 int
    613 mfcsread(devvp, uio, flag)
    614 	struct vnode *devvp;
    615 	struct uio *uio;
    616 	int flag;
    617 {
    618 	struct mfcs_softc *sc;
    619 	struct tty *tp;
    620 
    621 	sc = vdev_privdata(devvp);
    622 	tp = sc->sc_tty;
    623 
    624 	if (tp == NULL)
    625 		return(ENXIO);
    626 	return tp->t_linesw->l_read(tp, uio, flag);
    627 }
    628 
    629 int
    630 mfcswrite(devvp, uio, flag)
    631 	struct vnode *devvp;
    632 	struct uio *uio;
    633 	int flag;
    634 {
    635 	struct mfcs_softc *sc;
    636 	struct tty *tp;
    637 
    638 	sc = vdev_privdata(devvp);
    639 	tp = sc->sc_tty;
    640 
    641 	if (tp == NULL)
    642 		return(ENXIO);
    643 	return tp->t_linesw->l_write(tp, uio, flag);
    644 }
    645 
    646 int
    647 mfcspoll(devvp, events, p)
    648 	struct vnode *devvp;
    649 	int events;
    650 	struct proc *p;
    651 {
    652 	struct mfcs_softc *sc;
    653 	struct tty *tp;
    654 
    655 	sc = vdev_privdata(devvp);
    656 	tp = sc->sc_tty;
    657 	if (tp == NULL)
    658 		return(ENXIO);
    659 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    660 }
    661 
    662 struct tty *
    663 mfcstty(devvp)
    664 	struct vnode *devvp;
    665 {
    666 	struct mfcs_softc *sc;
    667 
    668 	sc = vdev_privdata(devvp);
    669 
    670 	return (sc->sc_tty);
    671 }
    672 
    673 int
    674 mfcsioctl(devvp, cmd, data, flag, p)
    675 	struct vnode *devvp;
    676 	u_long	cmd;
    677 	caddr_t data;
    678 	int	flag;
    679 	struct proc *p;
    680 {
    681 	register struct tty *tp;
    682 	register int error;
    683 	struct mfcs_softc *sc;
    684 
    685 	sc = vdev_privdata(devvp);
    686 
    687 	tp = sc->sc_tty;
    688 	if (!tp)
    689 		return ENXIO;
    690 
    691 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, p);
    692 	if (error >= 0)
    693 		return(error);
    694 
    695 	error = ttioctl(tp, cmd, data, flag, p);
    696 	if (error >= 0)
    697 		return(error);
    698 
    699 	switch (cmd) {
    700 	case TIOCSBRK:
    701 		sc->sc_duart->ch_cr = 0x60;		/* start break */
    702 		break;
    703 
    704 	case TIOCCBRK:
    705 		sc->sc_duart->ch_cr = 0x70;		/* stop break */
    706 		break;
    707 
    708 	case TIOCSDTR:
    709 		(void) mfcsmctl(devvp, TIOCM_DTR | TIOCM_RTS, DMBIS);
    710 		break;
    711 
    712 	case TIOCCDTR:
    713 		(void) mfcsmctl(devvp, TIOCM_DTR | TIOCM_RTS, DMBIC);
    714 		break;
    715 
    716 	case TIOCMSET:
    717 		(void) mfcsmctl(devvp, *(int *) data, DMSET);
    718 		break;
    719 
    720 	case TIOCMBIS:
    721 		(void) mfcsmctl(devvp, *(int *) data, DMBIS);
    722 		break;
    723 
    724 	case TIOCMBIC:
    725 		(void) mfcsmctl(devvp, *(int *) data, DMBIC);
    726 		break;
    727 
    728 	case TIOCMGET:
    729 		*(int *)data = mfcsmctl(devvp, 0, DMGET);
    730 		break;
    731 	case TIOCGFLAGS:
    732 		*(int *)data = SWFLAGS(vdev_rdev(devvp));
    733 		break;
    734 	case TIOCSFLAGS:
    735 		error = suser(p->p_ucred, &p->p_acflag);
    736 		if (error != 0)
    737 			return(EPERM);
    738 
    739 		sc->swflags = *(int *)data;
    740                 sc->swflags &= /* only allow valid flags */
    741                   (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS);
    742 		/* XXXX need to change duart parameters? */
    743 		break;
    744 	default:
    745 		return(ENOTTY);
    746 	}
    747 
    748 	return(0);
    749 }
    750 
    751 int
    752 mfcsparam(tp, t)
    753 	struct tty *tp;
    754 	struct termios *t;
    755 {
    756 	int cflag, unit, ospeed;
    757 	struct mfcs_softc *sc = vdev_privdata(tp->t_devvp);
    758 	struct mfc_softc *scc= sc->sc_mfc;
    759 
    760 	cflag = t->c_cflag;
    761 	unit = vdev_rdev(tp->t_devvp) & 31;
    762 	if (sc->flags & CT_USED) {
    763 		--scc->ct_usecnt;
    764 		sc->flags &= ~CT_USED;
    765 	}
    766 	ospeed = ttspeedtab(t->c_ospeed, scc->mfc_iii ? mfcs3speedtab2 :
    767 	    mfcs2speedtab2);
    768 
    769 	/*
    770 	 * If Baud Rate Generator can't generate requested speed,
    771 	 * try to use the counter/timer.
    772 	 */
    773 	if (ospeed < 0 && (scc->clk_frq % t->c_ospeed) == 0) {
    774 		ospeed = scc->clk_frq / t->c_ospeed;	/* divisor */
    775 		if (scc->ct_usecnt > 0 && scc->ct_val != ospeed)
    776 			ospeed = -1;
    777 		else {
    778 			scc->sc_regs->du_ctur = ospeed >> 8;
    779 			scc->sc_regs->du_ctlr = ospeed;
    780 			scc->ct_val = ospeed;
    781 			++scc->ct_usecnt;
    782 			sc->flags |= CT_USED;
    783 			ospeed = 0xdd;
    784 		}
    785 	}
    786 	/* XXXX 68681 duart could handle split speeds */
    787 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
    788 		return(EINVAL);
    789 
    790 	/* XXXX handle parity, character size, stop bits, flow control */
    791 
    792 	/*
    793 	 * copy to tty
    794 	 */
    795 	tp->t_ispeed = t->c_ispeed;
    796 	tp->t_ospeed = t->c_ospeed;
    797 	tp->t_cflag = cflag;
    798 
    799 	/*
    800 	 * enable interrupts
    801 	 */
    802 	scc->imask |= (0x2 << ((unit & 1) * 4)) | 0x80;
    803 	scc->sc_regs->du_imr = scc->imask;
    804 #if defined(DEBUG) && 0
    805 	printf("mfcsparam: speed %d => %x ct %d imask %x cflag %x\n",
    806 	    t->c_ospeed, ospeed, scc->ct_val, scc->imask, cflag);
    807 #endif
    808 	if (ospeed == 0)
    809 		(void)mfcsmctl(tp->t_devvp, 0, DMSET);	/* hang up line */
    810 	else {
    811 		/*
    812 		 * (re)enable DTR
    813 		 * and set baud rate. (8 bit mode)
    814 		 */
    815 		(void)mfcsmctl(tp->t_devvp, TIOCM_DTR | TIOCM_RTS, DMSET);
    816 		sc->sc_duart->ch_csr = ospeed;
    817 	}
    818 	return(0);
    819 }
    820 
    821 int
    822 mfcshwiflow(tp, flag)
    823         struct tty *tp;
    824         int flag;
    825 {
    826 	struct mfcs_softc *sc = vdev_privdata(tp->t_devvp);
    827 	int unit = vdev_rdev(tp->t_devvp) & 1;
    828 
    829         if (flag)
    830 		sc->sc_regs->du_btrst = 1 << unit;
    831 	else
    832 		sc->sc_regs->du_btst = 1 << unit;
    833         return 1;
    834 }
    835 
    836 void
    837 mfcsstart(tp)
    838 	struct tty *tp;
    839 {
    840 	int cc, s, unit;
    841 	struct mfcs_softc *sc = vdev_privdata(tp->t_devvp);
    842 	struct mfc_softc *scc= sc->sc_mfc;
    843 
    844 	if ((tp->t_state & TS_ISOPEN) == 0)
    845 		return;
    846 
    847 	unit = vdev_rdev(tp->t_devvp) & 1;
    848 
    849 	s = splser();
    850 	if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))
    851 		goto out;
    852 
    853 	cc = tp->t_outq.c_cc;
    854 	if (cc <= tp->t_lowat) {
    855 		if (tp->t_state & TS_ASLEEP) {
    856 			tp->t_state &= ~TS_ASLEEP;
    857 			wakeup((caddr_t) & tp->t_outq);
    858 		}
    859 		selwakeup(&tp->t_wsel);
    860 	}
    861 	if (cc == 0 || (tp->t_state & TS_BUSY))
    862 		goto out;
    863 
    864 	/*
    865 	 * We only do bulk transfers if using CTSRTS flow control, not for
    866 	 * (probably sloooow) ixon/ixoff devices.
    867 	 */
    868 	if ((tp->t_cflag & CRTSCTS) == 0)
    869 		cc = 1;
    870 
    871 	/*
    872 	 * Limit the amount of output we do in one burst
    873 	 * to prevent hogging the CPU.
    874 	 */
    875 	if (cc > SEROBUF_SIZE)
    876 		cc = SEROBUF_SIZE;
    877 	cc = q_to_b(&tp->t_outq, sc->outbuf, cc);
    878 	if (cc > 0) {
    879 		tp->t_state |= TS_BUSY;
    880 
    881 		sc->ptr = sc->outbuf;
    882 		sc->end = sc->outbuf + cc;
    883 
    884 		/*
    885 		 * Get first character out, then have TBE-interrupts blow out
    886 		 * further characters, until buffer is empty, and TS_BUSY gets
    887 		 * cleared.
    888 		 */
    889 		sc->sc_duart->ch_tb = *sc->ptr++;
    890 		scc->imask |= 1 << (unit * 4);
    891 		sc->sc_regs->du_imr = scc->imask;
    892 	}
    893 out:
    894 	splx(s);
    895 }
    896 
    897 /*
    898  * Stop output on a line.
    899  */
    900 /*ARGSUSED*/
    901 void
    902 mfcsstop(tp, flag)
    903 	struct tty *tp;
    904 	int flag;
    905 {
    906 	int s;
    907 
    908 	s = splser();
    909 	if (tp->t_state & TS_BUSY) {
    910 		if ((tp->t_state & TS_TTSTOP) == 0)
    911 			tp->t_state |= TS_FLUSH;
    912 	}
    913 	splx(s);
    914 }
    915 
    916 int
    917 mfcsmctl(devvp, bits, how)
    918 	struct vnode *devvp;
    919 	int bits, how;
    920 {
    921 	int unit, s;
    922 	u_char ub = 0;
    923 	struct mfcs_softc *sc;
    924 
    925 	sc = vdev_privdata(devvp);
    926 	unit = vdev_rdev(devvp) & 1;
    927 
    928 	/*
    929 	 * convert TIOCM* mask into CIA mask
    930 	 * which is active low
    931 	 */
    932 	if (how != DMGET) {
    933 		/*
    934 		 * need to save current state of DTR & RTS ?
    935 		 */
    936 		if (bits & TIOCM_DTR)
    937 			ub |= 0x04 << unit;
    938 		if (bits & TIOCM_RTS)
    939 			ub |= 0x01 << unit;
    940 	}
    941 	s = splser();
    942 	switch (how) {
    943 	case DMSET:
    944 		sc->sc_regs->du_btst = ub;
    945 		sc->sc_regs->du_btrst = ub ^ (0x05 << unit);
    946 		break;
    947 
    948 	case DMBIC:
    949 		sc->sc_regs->du_btrst = ub;
    950 		ub = ~sc->sc_regs->du_ip;
    951 		break;
    952 
    953 	case DMBIS:
    954 		sc->sc_regs->du_btst = ub;
    955 		ub = ~sc->sc_regs->du_ip;
    956 		break;
    957 
    958 	case DMGET:
    959 		ub = ~sc->sc_regs->du_ip;
    960 		break;
    961 	}
    962 	(void)splx(s);
    963 
    964 	/* XXXX should keep DTR & RTS states in softc? */
    965 	bits = TIOCM_DTR | TIOCM_RTS;
    966 	if (ub & (1 << unit))
    967 		bits |= TIOCM_CTS;
    968 	if (ub & (4 << unit))
    969 		bits |= TIOCM_DSR;
    970 	if (ub & (0x10 << unit))
    971 		bits |= TIOCM_CD;
    972 	/* XXXX RI is not supported on all boards */
    973 	if (sc->sc_regs->pad26 & (1 << unit))
    974 		bits |= TIOCM_RI;
    975 
    976 	return(bits);
    977 }
    978 
    979 /*
    980  * Level 6 interrupt processing for the MultiFaceCard 68681 DUART
    981  */
    982 
    983 int
    984 mfcintr(arg)
    985 	void *arg;
    986 {
    987 	struct mfc_softc *scc = arg;
    988 	struct mfcs_softc *sc;
    989 	struct mfc_regs *regs;
    990 	struct tty *tp;
    991 	int istat, unit;
    992 	u_short c;
    993 
    994 	regs = scc->sc_regs;
    995 	istat = regs->du_isr & scc->imask;
    996 	if (istat == 0)
    997 		return (0);
    998 	unit = scc->sc_dev.dv_unit * 2;
    999 	if (istat & 0x02) {		/* channel A receive interrupt */
   1000 		sc = mfcs_cd.cd_devs[unit];
   1001 		while (1) {
   1002 			c = regs->du_sra << 8;
   1003 			if ((c & 0x0100) == 0)
   1004 				break;
   1005 			c |= regs->du_rba;
   1006 			if (sc->incnt == SERIBUF_SIZE)
   1007 				++sc->ovfl;
   1008 			else {
   1009 				*sc->wptr++ = c;
   1010 				if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
   1011 					sc->wptr = sc->inbuf;
   1012 				++sc->incnt;
   1013 				if (sc->incnt > SERIBUF_SIZE - 16)
   1014 					regs->du_btrst = 1;
   1015 			}
   1016 			if (c & 0x1000)
   1017 				regs->du_cra = 0x40;
   1018 		}
   1019 	}
   1020 	if (istat & 0x20) {		/* channel B receive interrupt */
   1021 		sc = mfcs_cd.cd_devs[unit + 1];
   1022 		while (1) {
   1023 			c = regs->du_srb << 8;
   1024 			if ((c & 0x0100) == 0)
   1025 				break;
   1026 			c |= regs->du_rbb;
   1027 			if (sc->incnt == SERIBUF_SIZE)
   1028 				++sc->ovfl;
   1029 			else {
   1030 				*sc->wptr++ = c;
   1031 				if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
   1032 					sc->wptr = sc->inbuf;
   1033 				++sc->incnt;
   1034 				if (sc->incnt > SERIBUF_SIZE - 16)
   1035 					regs->du_btrst = 2;
   1036 			}
   1037 			if (c & 0x1000)
   1038 				regs->du_crb = 0x40;
   1039 		}
   1040 	}
   1041 	if (istat & 0x01) {		/* channel A transmit interrupt */
   1042 		sc = mfcs_cd.cd_devs[unit];
   1043 		tp = sc->sc_tty;
   1044 		if (sc->ptr == sc->end) {
   1045 			tp->t_state &= ~(TS_BUSY | TS_FLUSH);
   1046 			scc->imask &= ~0x01;
   1047 			regs->du_imr = scc->imask;
   1048 			add_sicallback (tp->t_linesw ?
   1049 			    (sifunc_t)tp->t_linesw->l_start
   1050 			    : (sifunc_t)mfcsstart, tp, NULL);
   1051 
   1052 		}
   1053 		else
   1054 			regs->du_tba = *sc->ptr++;
   1055 	}
   1056 	if (istat & 0x10) {		/* channel B transmit interrupt */
   1057 		sc = mfcs_cd.cd_devs[unit + 1];
   1058 		tp = sc->sc_tty;
   1059 		if (sc->ptr == sc->end) {
   1060 			tp->t_state &= ~(TS_BUSY | TS_FLUSH);
   1061 			scc->imask &= ~0x10;
   1062 			regs->du_imr = scc->imask;
   1063 			add_sicallback (tp->t_linesw ?
   1064 			    (sifunc_t)tp->t_linesw->l_start
   1065 			    : (sifunc_t)mfcsstart, tp, NULL);
   1066 		}
   1067 		else
   1068 			regs->du_tbb = *sc->ptr++;
   1069 	}
   1070 	if (istat & 0x80) {		/* input port change interrupt */
   1071 		c = regs->du_ipcr;
   1072 		printf ("%s: ipcr %02x", scc->sc_dev.dv_xname, c);
   1073 	}
   1074 	return(1);
   1075 }
   1076 
   1077 void
   1078 mfcsxintr(unit)
   1079 	int unit;
   1080 {
   1081 	int s1, s2, ovfl;
   1082 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
   1083 	struct tty *tp = sc->sc_tty;
   1084 
   1085 	/*
   1086 	 * Make sure we're not interrupted by another
   1087 	 * vbl, but allow level6 ints
   1088 	 */
   1089 	s1 = spltty();
   1090 
   1091 	/*
   1092 	 * pass along any acumulated information
   1093 	 * while input is not blocked
   1094 	 */
   1095 	while (sc->incnt && (tp->t_state & TS_TBLOCK) == 0) {
   1096 		/*
   1097 		 * no collision with ser_fastint()
   1098 		 */
   1099 		mfcseint(unit, *sc->rptr++);
   1100 
   1101 		ovfl = 0;
   1102 		/* lock against mfcs_fastint() */
   1103 		s2 = splser();
   1104 		--sc->incnt;
   1105 		if (sc->rptr == sc->inbuf + SERIBUF_SIZE)
   1106 			sc->rptr = sc->inbuf;
   1107 		if (sc->ovfl != 0) {
   1108 			ovfl = sc->ovfl;
   1109 			sc->ovfl = 0;
   1110 		}
   1111 		splx(s2);
   1112 		if (ovfl != 0)
   1113 			log(LOG_WARNING, "%s: %d buffer overflow!\n",
   1114 			    sc->sc_dev.dv_xname, ovfl);
   1115 	}
   1116 	if (sc->incnt == 0 && (tp->t_state & TS_TBLOCK) == 0) {
   1117 		sc->sc_regs->du_btst = 1 << unit;	/* XXXX */
   1118 	}
   1119 	splx(s1);
   1120 }
   1121 
   1122 void
   1123 mfcseint(unit, stat)
   1124 	int unit, stat;
   1125 {
   1126 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
   1127 	struct tty *tp;
   1128 	u_char ch;
   1129 	int c;
   1130 
   1131 	tp = sc->sc_tty;
   1132 	ch = stat & 0xff;
   1133 	c = ch;
   1134 
   1135 	if ((tp->t_state & TS_ISOPEN) == 0) {
   1136 #ifdef KGDB
   1137 		/* we don't care about parity errors */
   1138 		if (kgdb_dev == makedev(sermajor, unit) && c == FRAME_END)
   1139 			kgdb_connect(0);	/* trap into kgdb */
   1140 #endif
   1141 		return;
   1142 	}
   1143 
   1144 	/*
   1145 	 * Check for break and (if enabled) parity error.
   1146 	 */
   1147 	if (stat & 0xc000)
   1148 		c |= TTY_FE;
   1149 	else if (stat & 0x2000)
   1150 			c |= TTY_PE;
   1151 
   1152 	if (stat & 0x1000)
   1153 		log(LOG_WARNING, "%s: fifo overflow\n",
   1154 		    ((struct mfcs_softc *)mfcs_cd.cd_devs[unit])->sc_dev.dv_xname);
   1155 
   1156 	tp->t_linesw->l_rint(c, tp);
   1157 }
   1158 
   1159 /*
   1160  * This interrupt is periodically invoked in the vertical blank
   1161  * interrupt.  It's used to keep track of the modem control lines
   1162  * and (new with the fast_int code) to move accumulated data
   1163  * up into the tty layer.
   1164  */
   1165 void
   1166 mfcsmint(unit)
   1167 	int unit;
   1168 {
   1169 	struct tty *tp;
   1170 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
   1171 	u_char stat, last, istat;
   1172 
   1173 	tp = sc->sc_tty;
   1174 	if (!tp)
   1175 		return;
   1176 
   1177 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
   1178 		sc->rptr = sc->wptr = sc->inbuf;
   1179 		sc->incnt = 0;
   1180 		return;
   1181 	}
   1182 	/*
   1183 	 * empty buffer
   1184 	 */
   1185 	mfcsxintr(unit);
   1186 
   1187 	stat = ~sc->sc_regs->du_ip;
   1188 	last = sc->sc_mfc->last_ip;
   1189 	sc->sc_mfc->last_ip = stat;
   1190 
   1191 	/*
   1192 	 * check whether any interesting signal changed state
   1193 	 */
   1194 	istat = stat ^ last;
   1195 
   1196 	if ((istat & (0x10 << (unit & 1))) && 		/* CD changed */
   1197 	    (SWFLAGS(vdev_rdev(tp->t_devvp)) & TIOCFLAG_SOFTCAR) == 0) {
   1198 		if (stat & (0x10 << (unit & 1)))
   1199 			tp->t_linesw->l_modem(tp, 1);
   1200 		else if (tp->t_linesw->l_modem(tp, 0) == 0) {
   1201 			sc->sc_regs->du_btrst = 0x0a << (unit & 1);
   1202 		}
   1203 	}
   1204 }
   1205