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