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