Home | History | Annotate | Line # | Download | only in dev
mfc.c revision 1.23
      1 /*	$NetBSD: mfc.c,v 1.23 2001/05/02 10:32:13 scw 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 int
    631 mfcspoll(dev, events, p)
    632 	dev_t dev;
    633 	int events;
    634 	struct proc *p;
    635 {
    636 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
    637 	struct tty *tp = sc->sc_tty;
    638 
    639 	if (tp == NULL)
    640 		return(ENXIO);
    641 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    642 }
    643 
    644 struct tty *
    645 mfcstty(dev)
    646 	dev_t dev;
    647 {
    648 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
    649 
    650 	return (sc->sc_tty);
    651 }
    652 
    653 int
    654 mfcsioctl(dev, cmd, data, flag, p)
    655 	dev_t	dev;
    656 	u_long	cmd;
    657 	caddr_t data;
    658 	int	flag;
    659 	struct proc *p;
    660 {
    661 	register struct tty *tp;
    662 	register int error;
    663 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
    664 
    665 	tp = sc->sc_tty;
    666 	if (!tp)
    667 		return ENXIO;
    668 
    669 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, p);
    670 	if (error >= 0)
    671 		return(error);
    672 
    673 	error = ttioctl(tp, cmd, data, flag, p);
    674 	if (error >= 0)
    675 		return(error);
    676 
    677 	switch (cmd) {
    678 	case TIOCSBRK:
    679 		sc->sc_duart->ch_cr = 0x60;		/* start break */
    680 		break;
    681 
    682 	case TIOCCBRK:
    683 		sc->sc_duart->ch_cr = 0x70;		/* stop break */
    684 		break;
    685 
    686 	case TIOCSDTR:
    687 		(void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS);
    688 		break;
    689 
    690 	case TIOCCDTR:
    691 		(void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC);
    692 		break;
    693 
    694 	case TIOCMSET:
    695 		(void) mfcsmctl(dev, *(int *) data, DMSET);
    696 		break;
    697 
    698 	case TIOCMBIS:
    699 		(void) mfcsmctl(dev, *(int *) data, DMBIS);
    700 		break;
    701 
    702 	case TIOCMBIC:
    703 		(void) mfcsmctl(dev, *(int *) data, DMBIC);
    704 		break;
    705 
    706 	case TIOCMGET:
    707 		*(int *)data = mfcsmctl(dev, 0, DMGET);
    708 		break;
    709 	case TIOCGFLAGS:
    710 		*(int *)data = SWFLAGS(dev);
    711 		break;
    712 	case TIOCSFLAGS:
    713 		error = suser(p->p_ucred, &p->p_acflag);
    714 		if (error != 0)
    715 			return(EPERM);
    716 
    717 		sc->swflags = *(int *)data;
    718                 sc->swflags &= /* only allow valid flags */
    719                   (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS);
    720 		/* XXXX need to change duart parameters? */
    721 		break;
    722 	default:
    723 		return(ENOTTY);
    724 	}
    725 
    726 	return(0);
    727 }
    728 
    729 int
    730 mfcsparam(tp, t)
    731 	struct tty *tp;
    732 	struct termios *t;
    733 {
    734 	int cflag, unit, ospeed;
    735 	struct mfcs_softc *sc = mfcs_cd.cd_devs[tp->t_dev & 31];
    736 	struct mfc_softc *scc= sc->sc_mfc;
    737 
    738 	cflag = t->c_cflag;
    739 	unit = tp->t_dev & 31;
    740 	if (sc->flags & CT_USED) {
    741 		--scc->ct_usecnt;
    742 		sc->flags &= ~CT_USED;
    743 	}
    744 	ospeed = ttspeedtab(t->c_ospeed, scc->mfc_iii ? mfcs3speedtab2 :
    745 	    mfcs2speedtab2);
    746 
    747 	/*
    748 	 * If Baud Rate Generator can't generate requested speed,
    749 	 * try to use the counter/timer.
    750 	 */
    751 	if (ospeed < 0 && (scc->clk_frq % t->c_ospeed) == 0) {
    752 		ospeed = scc->clk_frq / t->c_ospeed;	/* divisor */
    753 		if (scc->ct_usecnt > 0 && scc->ct_val != ospeed)
    754 			ospeed = -1;
    755 		else {
    756 			scc->sc_regs->du_ctur = ospeed >> 8;
    757 			scc->sc_regs->du_ctlr = ospeed;
    758 			scc->ct_val = ospeed;
    759 			++scc->ct_usecnt;
    760 			sc->flags |= CT_USED;
    761 			ospeed = 0xdd;
    762 		}
    763 	}
    764 	/* XXXX 68681 duart could handle split speeds */
    765 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
    766 		return(EINVAL);
    767 
    768 	/* XXXX handle parity, character size, stop bits, flow control */
    769 
    770 	/*
    771 	 * copy to tty
    772 	 */
    773 	tp->t_ispeed = t->c_ispeed;
    774 	tp->t_ospeed = t->c_ospeed;
    775 	tp->t_cflag = cflag;
    776 
    777 	/*
    778 	 * enable interrupts
    779 	 */
    780 	scc->imask |= (0x2 << ((unit & 1) * 4)) | 0x80;
    781 	scc->sc_regs->du_imr = scc->imask;
    782 #if defined(DEBUG) && 0
    783 	printf("mfcsparam: speed %d => %x ct %d imask %x cflag %x\n",
    784 	    t->c_ospeed, ospeed, scc->ct_val, scc->imask, cflag);
    785 #endif
    786 	if (ospeed == 0)
    787 		(void)mfcsmctl(tp->t_dev, 0, DMSET);	/* hang up line */
    788 	else {
    789 		/*
    790 		 * (re)enable DTR
    791 		 * and set baud rate. (8 bit mode)
    792 		 */
    793 		(void)mfcsmctl(tp->t_dev, TIOCM_DTR | TIOCM_RTS, DMSET);
    794 		sc->sc_duart->ch_csr = ospeed;
    795 	}
    796 	return(0);
    797 }
    798 
    799 int
    800 mfcshwiflow(tp, flag)
    801         struct tty *tp;
    802         int flag;
    803 {
    804 	struct mfcs_softc *sc = mfcs_cd.cd_devs[tp->t_dev & 31];
    805 	int unit = tp->t_dev & 1;
    806 
    807         if (flag)
    808 		sc->sc_regs->du_btrst = 1 << unit;
    809 	else
    810 		sc->sc_regs->du_btst = 1 << unit;
    811         return 1;
    812 }
    813 
    814 void
    815 mfcsstart(tp)
    816 	struct tty *tp;
    817 {
    818 	int cc, s, unit;
    819 	struct mfcs_softc *sc = mfcs_cd.cd_devs[tp->t_dev & 31];
    820 	struct mfc_softc *scc= sc->sc_mfc;
    821 
    822 	if ((tp->t_state & TS_ISOPEN) == 0)
    823 		return;
    824 
    825 	unit = tp->t_dev & 1;
    826 
    827 	s = splser();
    828 	if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))
    829 		goto out;
    830 
    831 	cc = tp->t_outq.c_cc;
    832 	if (cc <= tp->t_lowat) {
    833 		if (tp->t_state & TS_ASLEEP) {
    834 			tp->t_state &= ~TS_ASLEEP;
    835 			wakeup((caddr_t) & tp->t_outq);
    836 		}
    837 		selwakeup(&tp->t_wsel);
    838 	}
    839 	if (cc == 0 || (tp->t_state & TS_BUSY))
    840 		goto out;
    841 
    842 	/*
    843 	 * We only do bulk transfers if using CTSRTS flow control, not for
    844 	 * (probably sloooow) ixon/ixoff devices.
    845 	 */
    846 	if ((tp->t_cflag & CRTSCTS) == 0)
    847 		cc = 1;
    848 
    849 	/*
    850 	 * Limit the amount of output we do in one burst
    851 	 * to prevent hogging the CPU.
    852 	 */
    853 	if (cc > SEROBUF_SIZE)
    854 		cc = SEROBUF_SIZE;
    855 	cc = q_to_b(&tp->t_outq, sc->outbuf, cc);
    856 	if (cc > 0) {
    857 		tp->t_state |= TS_BUSY;
    858 
    859 		sc->ptr = sc->outbuf;
    860 		sc->end = sc->outbuf + cc;
    861 
    862 		/*
    863 		 * Get first character out, then have TBE-interrupts blow out
    864 		 * further characters, until buffer is empty, and TS_BUSY gets
    865 		 * cleared.
    866 		 */
    867 		sc->sc_duart->ch_tb = *sc->ptr++;
    868 		scc->imask |= 1 << (unit * 4);
    869 		sc->sc_regs->du_imr = scc->imask;
    870 	}
    871 out:
    872 	splx(s);
    873 }
    874 
    875 /*
    876  * Stop output on a line.
    877  */
    878 /*ARGSUSED*/
    879 void
    880 mfcsstop(tp, flag)
    881 	struct tty *tp;
    882 	int flag;
    883 {
    884 	int s;
    885 
    886 	s = splser();
    887 	if (tp->t_state & TS_BUSY) {
    888 		if ((tp->t_state & TS_TTSTOP) == 0)
    889 			tp->t_state |= TS_FLUSH;
    890 	}
    891 	splx(s);
    892 }
    893 
    894 int
    895 mfcsmctl(dev, bits, how)
    896 	dev_t dev;
    897 	int bits, 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(arg)
    962 	void *arg;
    963 {
    964 	struct mfc_softc *scc = arg;
    965 	struct mfcs_softc *sc;
    966 	struct mfc_regs *regs;
    967 	struct tty *tp;
    968 	int istat, unit;
    969 	u_short c;
    970 
    971 	regs = scc->sc_regs;
    972 	istat = regs->du_isr & scc->imask;
    973 	if (istat == 0)
    974 		return (0);
    975 	unit = scc->sc_dev.dv_unit * 2;
    976 	if (istat & 0x02) {		/* channel A receive interrupt */
    977 		sc = mfcs_cd.cd_devs[unit];
    978 		while (1) {
    979 			c = regs->du_sra << 8;
    980 			if ((c & 0x0100) == 0)
    981 				break;
    982 			c |= regs->du_rba;
    983 			if (sc->incnt == SERIBUF_SIZE)
    984 				++sc->ovfl;
    985 			else {
    986 				*sc->wptr++ = c;
    987 				if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
    988 					sc->wptr = sc->inbuf;
    989 				++sc->incnt;
    990 				if (sc->incnt > SERIBUF_SIZE - 16)
    991 					regs->du_btrst = 1;
    992 			}
    993 			if (c & 0x1000)
    994 				regs->du_cra = 0x40;
    995 		}
    996 	}
    997 	if (istat & 0x20) {		/* channel B receive interrupt */
    998 		sc = mfcs_cd.cd_devs[unit + 1];
    999 		while (1) {
   1000 			c = regs->du_srb << 8;
   1001 			if ((c & 0x0100) == 0)
   1002 				break;
   1003 			c |= regs->du_rbb;
   1004 			if (sc->incnt == SERIBUF_SIZE)
   1005 				++sc->ovfl;
   1006 			else {
   1007 				*sc->wptr++ = c;
   1008 				if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
   1009 					sc->wptr = sc->inbuf;
   1010 				++sc->incnt;
   1011 				if (sc->incnt > SERIBUF_SIZE - 16)
   1012 					regs->du_btrst = 2;
   1013 			}
   1014 			if (c & 0x1000)
   1015 				regs->du_crb = 0x40;
   1016 		}
   1017 	}
   1018 	if (istat & 0x01) {		/* channel A transmit interrupt */
   1019 		sc = mfcs_cd.cd_devs[unit];
   1020 		tp = sc->sc_tty;
   1021 		if (sc->ptr == sc->end) {
   1022 			tp->t_state &= ~(TS_BUSY | TS_FLUSH);
   1023 			scc->imask &= ~0x01;
   1024 			regs->du_imr = scc->imask;
   1025 			add_sicallback (tp->t_linesw ?
   1026 			    (sifunc_t)tp->t_linesw->l_start
   1027 			    : (sifunc_t)mfcsstart, tp, NULL);
   1028 
   1029 		}
   1030 		else
   1031 			regs->du_tba = *sc->ptr++;
   1032 	}
   1033 	if (istat & 0x10) {		/* channel B transmit interrupt */
   1034 		sc = mfcs_cd.cd_devs[unit + 1];
   1035 		tp = sc->sc_tty;
   1036 		if (sc->ptr == sc->end) {
   1037 			tp->t_state &= ~(TS_BUSY | TS_FLUSH);
   1038 			scc->imask &= ~0x10;
   1039 			regs->du_imr = scc->imask;
   1040 			add_sicallback (tp->t_linesw ?
   1041 			    (sifunc_t)tp->t_linesw->l_start
   1042 			    : (sifunc_t)mfcsstart, tp, NULL);
   1043 		}
   1044 		else
   1045 			regs->du_tbb = *sc->ptr++;
   1046 	}
   1047 	if (istat & 0x80) {		/* input port change interrupt */
   1048 		c = regs->du_ipcr;
   1049 		printf ("%s: ipcr %02x", scc->sc_dev.dv_xname, c);
   1050 	}
   1051 	return(1);
   1052 }
   1053 
   1054 void
   1055 mfcsxintr(unit)
   1056 	int unit;
   1057 {
   1058 	int s1, s2, ovfl;
   1059 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
   1060 	struct tty *tp = sc->sc_tty;
   1061 
   1062 	/*
   1063 	 * Make sure we're not interrupted by another
   1064 	 * vbl, but allow level6 ints
   1065 	 */
   1066 	s1 = spltty();
   1067 
   1068 	/*
   1069 	 * pass along any acumulated information
   1070 	 * while input is not blocked
   1071 	 */
   1072 	while (sc->incnt && (tp->t_state & TS_TBLOCK) == 0) {
   1073 		/*
   1074 		 * no collision with ser_fastint()
   1075 		 */
   1076 		mfcseint(unit, *sc->rptr++);
   1077 
   1078 		ovfl = 0;
   1079 		/* lock against mfcs_fastint() */
   1080 		s2 = splser();
   1081 		--sc->incnt;
   1082 		if (sc->rptr == sc->inbuf + SERIBUF_SIZE)
   1083 			sc->rptr = sc->inbuf;
   1084 		if (sc->ovfl != 0) {
   1085 			ovfl = sc->ovfl;
   1086 			sc->ovfl = 0;
   1087 		}
   1088 		splx(s2);
   1089 		if (ovfl != 0)
   1090 			log(LOG_WARNING, "%s: %d buffer overflow!\n",
   1091 			    sc->sc_dev.dv_xname, ovfl);
   1092 	}
   1093 	if (sc->incnt == 0 && (tp->t_state & TS_TBLOCK) == 0) {
   1094 		sc->sc_regs->du_btst = 1 << unit;	/* XXXX */
   1095 	}
   1096 	splx(s1);
   1097 }
   1098 
   1099 void
   1100 mfcseint(unit, stat)
   1101 	int unit, stat;
   1102 {
   1103 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
   1104 	struct tty *tp;
   1105 	u_char ch;
   1106 	int c;
   1107 
   1108 	tp = sc->sc_tty;
   1109 	ch = stat & 0xff;
   1110 	c = ch;
   1111 
   1112 	if ((tp->t_state & TS_ISOPEN) == 0) {
   1113 #ifdef KGDB
   1114 		/* we don't care about parity errors */
   1115 		if (kgdb_dev == makedev(sermajor, unit) && c == FRAME_END)
   1116 			kgdb_connect(0);	/* trap into kgdb */
   1117 #endif
   1118 		return;
   1119 	}
   1120 
   1121 	/*
   1122 	 * Check for break and (if enabled) parity error.
   1123 	 */
   1124 	if (stat & 0xc000)
   1125 		c |= TTY_FE;
   1126 	else if (stat & 0x2000)
   1127 			c |= TTY_PE;
   1128 
   1129 	if (stat & 0x1000)
   1130 		log(LOG_WARNING, "%s: fifo overflow\n",
   1131 		    ((struct mfcs_softc *)mfcs_cd.cd_devs[unit])->sc_dev.dv_xname);
   1132 
   1133 	tp->t_linesw->l_rint(c, tp);
   1134 }
   1135 
   1136 /*
   1137  * This interrupt is periodically invoked in the vertical blank
   1138  * interrupt.  It's used to keep track of the modem control lines
   1139  * and (new with the fast_int code) to move accumulated data
   1140  * up into the tty layer.
   1141  */
   1142 void
   1143 mfcsmint(unit)
   1144 	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