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