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