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