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