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