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