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