Home | History | Annotate | Line # | Download | only in ixp12x0
ixp12x0_com.c revision 1.10
      1 /*	$NetBSD: ixp12x0_com.c,v 1.10 2003/02/17 20:51:52 ichiro Exp $ */
      2 /*
      3  * Copyright (c) 1998, 1999, 2001, 2002 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Ichiro FUKUHARA and Naoto Shimazaki.
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by IWAMOTO Toshihiro.
     11  *
     12  * This code is derived from software contributed to The NetBSD Foundation
     13  * by Charles M. Hannum.
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  * 3. All advertising materials mentioning features or use of this software
     24  *    must display the following acknowledgement:
     25  *        This product includes software developed by the NetBSD
     26  *        Foundation, Inc. and its contributors.
     27  * 4. Neither the name of The NetBSD Foundation nor the names of its
     28  *    contributors may be used to endorse or promote products derived
     29  *    from this software without specific prior written permission.
     30  *
     31  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     32  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     33  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     34  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     35  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     36  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     37  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     39  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  * POSSIBILITY OF SUCH DAMAGE.
     42  */
     43 
     44 /*
     45  * Copyright (c) 1991 The Regents of the University of California.
     46  * All rights reserved.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. All advertising materials mentioning features or use of this software
     57  *    must display the following acknowledgement:
     58  *      This product includes software developed by the University of
     59  *      California, Berkeley and its contributors.
     60  * 4. Neither the name of the University nor the names of its contributors
     61  *    may be used to endorse or promote products derived from this software
     62  *    without specific prior written permission.
     63  *
     64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     74  * SUCH DAMAGE.
     75  *
     76  *      @(#)com.c       7.5 (Berkeley) 5/16/91
     77  */
     78 
     79 
     80 #include "opt_com.h"
     81 #include "opt_ddb.h"
     82 #include "opt_kgdb.h"
     83 
     84 #include "rnd.h"
     85 #if NRND > 0 && defined(RND_COM)
     86 #include <sys/rnd.h>
     87 #endif
     88 
     89 #include <sys/param.h>
     90 #include <sys/systm.h>
     91 #include <sys/types.h>
     92 #include <sys/conf.h>
     93 #include <sys/file.h>
     94 #include <sys/device.h>
     95 #include <sys/kernel.h>
     96 #include <sys/malloc.h>
     97 #include <sys/tty.h>
     98 #include <sys/uio.h>
     99 #include <sys/vnode.h>
    100 
    101 #include <machine/intr.h>
    102 #include <machine/bus.h>
    103 
    104 #include <arm/ixp12x0/ixp12x0_comreg.h>
    105 #include <arm/ixp12x0/ixp12x0_comvar.h>
    106 #include <arm/ixp12x0/ixp12x0reg.h>
    107 #include <arm/ixp12x0/ixp12x0var.h>
    108 
    109 #include <arm/ixp12x0/ixpsipvar.h>
    110 
    111 #include <dev/cons.h>
    112 #include "ixpcom.h"
    113 
    114 static int	ixpcomparam(struct tty *, struct termios *);
    115 static void	ixpcomstart(struct tty *);
    116 static int	ixpcomhwiflow(struct tty *, int);
    117 
    118 static u_int	cflag2cr(tcflag_t);
    119 static void	ixpcom_iflush(struct ixpcom_softc *);
    120 static void	ixpcom_set_cr(struct ixpcom_softc *);
    121 
    122 int             ixpcomcngetc(dev_t);
    123 void            ixpcomcnputc(dev_t, int);
    124 void            ixpcomcnpollc(dev_t, int);
    125 
    126 static void	ixpcomsoft(void* arg);
    127 inline static void	ixpcom_txsoft(struct ixpcom_softc *, struct tty *);
    128 inline static void	ixpcom_rxsoft(struct ixpcom_softc *, struct tty *);
    129 
    130 void            ixpcomcnprobe(struct consdev *);
    131 void            ixpcomcninit(struct consdev *);
    132 
    133 u_int32_t	ixpcom_cr = 0;		/* tell cr to *_intr.c */
    134 u_int32_t	ixpcom_imask = 0;	/* intrrupt mask from *_intr.c */
    135 
    136 
    137 static struct ixpcom_cons_softc {
    138 	bus_space_tag_t		sc_iot;
    139 	bus_space_handle_t	sc_ioh;
    140 	int			sc_ospeed;
    141 	tcflag_t		sc_cflag;
    142 } ixpcomcn_sc;
    143 
    144 static struct cnm_state ixpcom_cnm_state;
    145 
    146 struct ixpcom_softc* ixpcom_sc = NULL;
    147 
    148 extern struct cfdriver ixpcom_cd;
    149 
    150 dev_type_open(ixpcomopen);
    151 dev_type_close(ixpcomclose);
    152 dev_type_read(ixpcomread);
    153 dev_type_write(ixpcomwrite);
    154 dev_type_stop(ixpcomstop);
    155 dev_type_tty(ixpcomtty);
    156 dev_type_poll(ixpcompoll);
    157 
    158 const struct cdevsw ixpcom_cdevsw = {
    159 	ixpcomopen, ixpcomclose, ixpcomread, ixpcomwrite, nullioctl,
    160 	ixpcomstop, ixpcomtty, ixpcompoll, nommap, ttykqfilter, D_TTY
    161 };
    162 
    163 struct consdev ixpcomcons = {
    164 	NULL, NULL, ixpcomcngetc, ixpcomcnputc, ixpcomcnpollc, NULL,
    165 	NODEV, CN_NORMAL
    166 };
    167 
    168 #ifndef DEFAULT_COMSPEED
    169 #define DEFAULT_COMSPEED 38400
    170 #endif
    171 
    172 #define COMUNIT_MASK    0x7ffff
    173 #define COMDIALOUT_MASK 0x80000
    174 
    175 #define COMUNIT(x)	(minor(x) & COMUNIT_MASK)
    176 #define COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
    177 
    178 #define COM_ISALIVE(sc)	((sc)->enabled != 0 && \
    179 			ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
    180 
    181 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
    182 
    183 #define	COM_LOCK(sc);
    184 #define	COM_UNLOCK(sc);
    185 
    186 #define SET(t, f)	(t) |= (f)
    187 #define CLR(t, f)	(t) &= ~(f)
    188 #define ISSET(t, f)	((t) & (f))
    189 
    190 #define CFLAGS2CR_MASK	(CR_PE | CR_OES | CR_SBS | CR_DSS | CR_BRD)
    191 
    192 void
    193 ixpcom_attach_subr(sc)
    194 	struct ixpcom_softc *sc;
    195 {
    196 	struct tty *tp;
    197 
    198 	ixpcom_sc = sc;
    199 
    200 	/* force to use ixpcom0 for console */
    201 	if (sc->sc_dev.dv_unit == 0) {
    202 		sc->sc_speed = IXPCOMSPEED2BRD(ixpcomcn_sc.sc_ospeed);
    203 
    204 		/* Make sure the console is always "hardwired". */
    205 		/* XXX IXPCOM_SR should be checked  */
    206 		delay(10000);	/* wait for output to finish */
    207 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    208 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    209 	}
    210 
    211 	tp = ttymalloc();
    212 	tp->t_oproc = ixpcomstart;
    213 	tp->t_param = ixpcomparam;
    214 	tp->t_hwiflow = ixpcomhwiflow;
    215 
    216 	sc->sc_tty = tp;
    217 	sc->sc_rbuf = malloc(IXPCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
    218 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    219 	sc->sc_rbavail = IXPCOM_RING_SIZE;
    220 	if (sc->sc_rbuf == NULL) {
    221 		printf("%s: unable to allocate ring buffer\n",
    222 		    sc->sc_dev.dv_xname);
    223 		return;
    224 	}
    225 	sc->sc_ebuf = sc->sc_rbuf + (IXPCOM_RING_SIZE << 1);
    226 	sc->sc_tbc = 0;
    227 
    228 	sc->sc_rie = sc->sc_xie = 0;
    229 	ixpcom_cr = IXPCOMSPEED2BRD(DEFAULT_COMSPEED)
    230 		| CR_UE | sc->sc_rie | sc->sc_xie | DSS_8BIT;
    231 
    232 	tty_attach(tp);
    233 
    234 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    235 		int maj;
    236 
    237 		/* locate the major number */
    238 		maj = cdevsw_lookup_major(&ixpcom_cdevsw);
    239 
    240 		cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
    241 
    242 		delay(10000); /* XXX */
    243 		printf("%s: console\n", sc->sc_dev.dv_xname);
    244 		delay(10000); /* XXX */
    245 	}
    246 
    247 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, ixpcomsoft, sc);
    248 
    249 #if NRND > 0 && defined(RND_COM)
    250 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    251 			  RND_TYPE_TTY, 0);
    252 #endif
    253 
    254 	/* if there are no enable/disable functions, assume the device
    255 	   is always enabled */
    256 	if (!sc->enable)
    257 		sc->enabled = 1;
    258 
    259 	/* XXX configure register */
    260 	/* xxx_config(sc) */
    261 
    262 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    263 }
    264 
    265 static int
    266 ixpcomparam(tp, t)
    267 	struct tty *tp;
    268 	struct termios *t;
    269 {
    270 	struct ixpcom_softc *sc
    271 		= device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
    272 	u_int32_t cr;
    273 	int s;
    274 
    275 	if (COM_ISALIVE(sc) == 0)
    276 		return (EIO);
    277 
    278 	cr = IXPCOMSPEED2BRD(t->c_ospeed);
    279 
    280 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    281 		return (EINVAL);
    282 
    283 	/*
    284 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    285 	 * is always active.
    286 	 */
    287 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    288 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    289 		SET(t->c_cflag, CLOCAL);
    290 		CLR(t->c_cflag, HUPCL);
    291 	}
    292 
    293 	/*
    294 	 * If there were no changes, don't do anything.  This avoids dropping
    295 	 * input and improves performance when all we did was frob things like
    296 	 * VMIN and VTIME.
    297 	 */
    298 	if (tp->t_ospeed == t->c_ospeed &&
    299 	    tp->t_cflag == t->c_cflag)
    300 		return (0);
    301 
    302 	cr |= cflag2cr(t->c_cflag);
    303 
    304 	s = splserial();
    305 	COM_LOCK(sc);
    306 
    307 	ixpcom_cr = (ixpcom_cr & ~CFLAGS2CR_MASK) | cr;
    308 
    309 	/*
    310 	 * ixpcom don't have any hardware flow control.
    311 	 * we skip it.
    312 	 */
    313 
    314 	/* And copy to tty. */
    315 	tp->t_ispeed = 0;
    316 	tp->t_ospeed = t->c_ospeed;
    317 	tp->t_cflag = t->c_cflag;
    318 
    319 	if (!sc->sc_heldchange) {
    320 		if (sc->sc_tx_busy) {
    321 			sc->sc_heldtbc = sc->sc_tbc;
    322 			sc->sc_tbc = 0;
    323 			sc->sc_heldchange = 1;
    324 		} else
    325 			ixpcom_set_cr(sc);
    326 	}
    327 
    328 	COM_UNLOCK(sc);
    329 	splx(s);
    330 
    331 	/*
    332 	 * Update the tty layer's idea of the carrier bit.
    333 	 * We tell tty the carrier is always on.
    334 	 */
    335 	(void) (*tp->t_linesw->l_modem)(tp, 1);
    336 
    337 #ifdef COM_DEBUG
    338 	if (com_debug)
    339 		comstatus(sc, "comparam ");
    340 #endif
    341 
    342 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    343 		if (sc->sc_tx_stopped) {
    344 			sc->sc_tx_stopped = 0;
    345 			ixpcomstart(tp);
    346 		}
    347 	}
    348 
    349 	return (0);
    350 }
    351 
    352 static int
    353 ixpcomhwiflow(tp, block)
    354 	struct tty *tp;
    355 	int block;
    356 {
    357 	return (0);
    358 }
    359 
    360 static void
    361 ixpcom_filltx(struct ixpcom_softc *sc)
    362 {
    363 	bus_space_tag_t iot = sc->sc_iot;
    364 	bus_space_handle_t ioh = sc->sc_ioh;
    365 	int n;
    366 
    367 	n = 0;
    368         while (bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXR) {
    369 		if (n >= sc->sc_tbc)
    370 			break;
    371 		bus_space_write_4(iot, ioh, IXPCOM_DR,
    372 				  0xff & *(sc->sc_tba + n));
    373 		n++;
    374         }
    375         sc->sc_tbc -= n;
    376         sc->sc_tba += n;
    377 }
    378 
    379 static void
    380 ixpcomstart(tp)
    381 	struct tty *tp;
    382 {
    383 	struct ixpcom_softc *sc
    384 		= device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
    385 	int s;
    386 
    387 	if (COM_ISALIVE(sc) == 0)
    388 		return;
    389 
    390 	s = spltty();
    391 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    392 		goto out;
    393 	if (sc->sc_tx_stopped)
    394 		goto out;
    395 
    396 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    397 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    398 			CLR(tp->t_state, TS_ASLEEP);
    399 			wakeup(&tp->t_outq);
    400 		}
    401 		selwakeup(&tp->t_wsel);
    402 		if (tp->t_outq.c_cc == 0)
    403 			goto out;
    404 	}
    405 
    406 	/* Grab the first contiguous region of buffer space. */
    407 	{
    408 		u_char *tba;
    409 		int tbc;
    410 
    411 		tba = tp->t_outq.c_cf;
    412 		tbc = ndqb(&tp->t_outq, 0);
    413 
    414 		(void)splserial();
    415 		COM_LOCK(sc);
    416 
    417 		sc->sc_tba = tba;
    418 		sc->sc_tbc = tbc;
    419 	}
    420 
    421 	SET(tp->t_state, TS_BUSY);
    422 	sc->sc_tx_busy = 1;
    423 
    424 	/* Enable transmit completion interrupts if necessary. */
    425 	if (!ISSET(sc->sc_xie, CR_XIE)) {
    426 		SET(sc->sc_xie, CR_XIE);
    427 		ixpcom_set_cr(sc);
    428 	}
    429 
    430 	/* Output the first chunk of the contiguous buffer. */
    431 	ixpcom_filltx(sc);
    432 
    433 	COM_UNLOCK(sc);
    434 out:
    435 	splx(s);
    436 	return;
    437 }
    438 
    439 static void
    440 ixpcom_break(struct ixpcom_softc *sc, int onoff)
    441 {
    442 	if (onoff)
    443 		SET(ixpcom_cr, CR_BRK);
    444 	else
    445 		CLR(ixpcom_cr, CR_BRK);
    446 	if (!sc->sc_heldchange) {
    447 		if (sc->sc_tx_busy) {
    448 			sc->sc_heldtbc = sc->sc_tbc;
    449 			sc->sc_tbc = 0;
    450 			sc->sc_heldchange = 1;
    451 		} else
    452 			ixpcom_set_cr(sc);
    453 	}
    454 }
    455 
    456 static void
    457 ixpcom_shutdown(struct ixpcom_softc *sc)
    458 {
    459 	int s;
    460 
    461 	s = splserial();
    462 	COM_LOCK(sc);
    463 
    464 	/* Clear any break condition set with TIOCSBRK. */
    465 	ixpcom_break(sc, 0);
    466 
    467 	/* Turn off interrupts. */
    468 	sc->sc_rie = sc->sc_xie = 0;
    469 	ixpcom_set_cr(sc);
    470 
    471 	if (sc->disable) {
    472 #ifdef DIAGNOSTIC
    473 		if (!sc->enabled)
    474 			panic("ixpcom_shutdown: not enabled?");
    475 #endif
    476 		(*sc->disable)(sc);
    477 		sc->enabled = 0;
    478 	}
    479 	COM_UNLOCK(sc);
    480 	splx(s);
    481 }
    482 
    483 int
    484 ixpcomopen(dev, flag, mode, p)
    485 	dev_t dev;
    486 	int flag, mode;
    487 	struct proc *p;
    488 {
    489 	struct ixpcom_softc *sc;
    490 	struct tty *tp;
    491 	int s, s2;
    492 	int error;
    493 
    494 	sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
    495 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    496 		sc->sc_rbuf == NULL)
    497 		return (ENXIO);
    498 
    499 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    500 		return (ENXIO);
    501 
    502 #ifdef KGDB
    503 	/*
    504 	 * If this is the kgdb port, no other use is permitted.
    505 	 */
    506 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
    507 		return (EBUSY);
    508 #endif
    509 
    510 	tp = sc->sc_tty;
    511 
    512 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    513 	    ISSET(tp->t_state, TS_XCLUDE) &&
    514 		p->p_ucred->cr_uid != 0)
    515 		return (EBUSY);
    516 
    517 	s = spltty();
    518 
    519 	/*
    520 	 * Do the following iff this is a first open.
    521 	 */
    522 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    523 		struct termios t;
    524 
    525 		tp->t_dev = dev;
    526 
    527 		s2 = splserial();
    528 		COM_LOCK(sc);
    529 
    530 		if (sc->enable) {
    531 			if ((*sc->enable)(sc)) {
    532 				COM_UNLOCK(sc);
    533 				splx(s2);
    534 				splx(s);
    535 				printf("%s: device enable failed\n",
    536 				       sc->sc_dev.dv_xname);
    537 				return (EIO);
    538 			}
    539 			sc->enabled = 1;
    540 #if 0
    541 /* XXXXXXXXXXXXXXX */
    542 			com_config(sc);
    543 #endif
    544 		}
    545 
    546 		/* Turn on interrupts. */
    547 		SET(sc->sc_rie, CR_RIE);
    548 		ixpcom_set_cr(sc);
    549 
    550 #if 0
    551 		/* Fetch the current modem control status, needed later. */
    552 		sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
    553 
    554 		/* Clear PPS capture state on first open. */
    555 		sc->sc_ppsmask = 0;
    556 		sc->ppsparam.mode = 0;
    557 #endif
    558 
    559 		COM_UNLOCK(sc);
    560 		splx(s2);
    561 
    562 		/*
    563 		 * Initialize the termios status to the defaults.  Add in the
    564 		 * sticky bits from TIOCSFLAGS.
    565 		 */
    566 		t.c_ispeed = 0;
    567 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    568 			t.c_ospeed = ixpcomcn_sc.sc_ospeed;
    569 			t.c_cflag = ixpcomcn_sc.sc_cflag;
    570 		} else {
    571 			t.c_ospeed = TTYDEF_SPEED;
    572 			t.c_cflag = TTYDEF_CFLAG;
    573 		}
    574 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    575 			SET(t.c_cflag, CLOCAL);
    576 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    577 			SET(t.c_cflag, CRTSCTS);
    578 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    579 			SET(t.c_cflag, MDMBUF);
    580 		/* Make sure ixpcomparam() will do something. */
    581 		tp->t_ospeed = 0;
    582 		(void) ixpcomparam(tp, &t);
    583 		tp->t_iflag = TTYDEF_IFLAG;
    584 		tp->t_oflag = TTYDEF_OFLAG;
    585 		tp->t_lflag = TTYDEF_LFLAG;
    586 		ttychars(tp);
    587 		ttsetwater(tp);
    588 
    589 		s2 = splserial();
    590 		COM_LOCK(sc);
    591 
    592 		/* Clear the input ring, and unblock. */
    593 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    594 		sc->sc_rbavail = IXPCOM_RING_SIZE;
    595 		ixpcom_iflush(sc);
    596 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    597 
    598 #ifdef COM_DEBUG
    599 		if (ixpcom_debug)
    600 			comstatus(sc, "ixpcomopen  ");
    601 #endif
    602 
    603 		COM_UNLOCK(sc);
    604 		splx(s2);
    605 	}
    606 
    607 	splx(s);
    608 
    609 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    610 	if (error)
    611 		goto bad;
    612 
    613 	error = (*tp->t_linesw->l_open)(dev, tp);
    614 	if (error)
    615 		goto bad;
    616 
    617 	return (0);
    618 
    619 bad:
    620 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    621 		/*
    622 		 * We failed to open the device, and nobody else had it opened.
    623 		 * Clean up the state as appropriate.
    624 		 */
    625 		ixpcom_shutdown(sc);
    626 	}
    627 
    628 	return (error);
    629 }
    630 
    631 int
    632 ixpcomclose(dev, flag, mode, p)
    633 	dev_t dev;
    634 	int flag, mode;
    635 	struct proc *p;
    636 {
    637 	struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
    638 	struct tty *tp = sc->sc_tty;
    639 
    640 	/* XXX This is for cons.c. */
    641 	if (!ISSET(tp->t_state, TS_ISOPEN))
    642 		return (0);
    643 
    644 	(*tp->t_linesw->l_close)(tp, flag);
    645 	ttyclose(tp);
    646 
    647 	if (COM_ISALIVE(sc) == 0)
    648 		return (0);
    649 
    650 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    651 		/*
    652 		 * Although we got a last close, the device may still be in
    653 		 * use; e.g. if this was the dialout node, and there are still
    654 		 * processes waiting for carrier on the non-dialout node.
    655 		 */
    656 		ixpcom_shutdown(sc);
    657 	}
    658 
    659 	return (0);
    660 }
    661 
    662 int
    663 ixpcomread(dev, uio, flag)
    664 	dev_t dev;
    665 	struct uio *uio;
    666 	int flag;
    667 {
    668 	struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
    669 	struct tty *tp = sc->sc_tty;
    670 
    671 	if (COM_ISALIVE(sc) == 0)
    672 		return (EIO);
    673 
    674 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    675 }
    676 
    677 int
    678 ixpcomwrite(dev, uio, flag)
    679 	dev_t dev;
    680 	struct uio *uio;
    681 	int flag;
    682 {
    683 	struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
    684 	struct tty *tp = sc->sc_tty;
    685 
    686 	if (COM_ISALIVE(sc) == 0)
    687 		return (EIO);
    688 
    689 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    690 }
    691 
    692 int
    693 ixpcompoll(dev, events, p)
    694 	dev_t dev;
    695 	int events;
    696 	struct proc *p;
    697 {
    698 	struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
    699 	struct tty *tp = sc->sc_tty;
    700 
    701 	if (COM_ISALIVE(sc) == 0)
    702 		return (EIO);
    703 
    704 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    705 }
    706 
    707 struct tty *
    708 ixpcomtty(dev)
    709 	dev_t dev;
    710 {
    711 	struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
    712 	struct tty *tp = sc->sc_tty;
    713 
    714 	return (tp);
    715 }
    716 
    717 /*
    718  * Stop output on a line.
    719  */
    720 void
    721 ixpcomstop(tp, flag)
    722 	struct tty *tp;
    723 	int flag;
    724 {
    725 	struct ixpcom_softc *sc
    726 		= device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
    727 	int s;
    728 
    729 	s = splserial();
    730 	COM_LOCK(sc);
    731 	if (ISSET(tp->t_state, TS_BUSY)) {
    732 		/* Stop transmitting at the next chunk. */
    733 		sc->sc_tbc = 0;
    734 		sc->sc_heldtbc = 0;
    735 		if (!ISSET(tp->t_state, TS_TTSTOP))
    736 			SET(tp->t_state, TS_FLUSH);
    737 	}
    738 	COM_UNLOCK(sc);
    739 	splx(s);
    740 }
    741 
    742 static u_int
    743 cflag2cr(cflag)
    744 	tcflag_t cflag;
    745 {
    746 	u_int cr;
    747 
    748 	cr  = (cflag & PARENB) ? CR_PE : 0;
    749 	cr |= (cflag & PARODD) ? CR_OES : 0;
    750 	cr |= (cflag & CSTOPB) ? CR_SBS : 0;
    751 	cr |= ((cflag & CSIZE) == CS8) ? DSS_8BIT : 0;
    752 
    753 	return (cr);
    754 }
    755 
    756 static void
    757 ixpcom_iflush(sc)
    758 	struct ixpcom_softc *sc;
    759 {
    760 	bus_space_tag_t iot = sc->sc_iot;
    761 	bus_space_handle_t ioh = sc->sc_ioh;
    762 #ifdef DIAGNOSTIC
    763 	int reg;
    764 #endif
    765 	int timo;
    766 
    767 #ifdef DIAGNOSTIC
    768 	reg = 0xffff;
    769 #endif
    770 	timo = 50000;
    771 	/* flush any pending I/O */
    772 	while (ISSET(bus_space_read_4(iot, ioh, IXPCOM_SR), SR_RXR)
    773 	       && --timo)
    774 #ifdef DIAGNOSTIC
    775 		reg =
    776 #else
    777 			(void)
    778 #endif
    779 			bus_space_read_4(iot, ioh, IXPCOM_DR);
    780 #ifdef DIAGNOSTIC
    781 	if (!timo)
    782 		printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
    783 		       reg);
    784 #endif
    785 }
    786 
    787 static void
    788 ixpcom_set_cr(struct ixpcom_softc *sc)
    789 {
    790 	/* XXX */
    791 	ixpcom_cr &= ~(CR_RIE | CR_XIE);
    792 	ixpcom_cr |= sc->sc_rie | sc->sc_xie;
    793 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXPCOM_CR,
    794 			  ixpcom_cr & ~ixpcom_imask);
    795 }
    796 
    797 int
    798 ixpcomcnattach(iot, iobase, ospeed, cflag)
    799 	bus_space_tag_t iot;
    800 	bus_addr_t iobase;
    801 	int ospeed;
    802 	tcflag_t cflag;
    803 {
    804 	int cr;
    805 
    806 	cn_tab = &ixpcomcons;
    807 	cn_init_magic(&ixpcom_cnm_state);
    808 	/*
    809 	 * XXX
    810 	 *
    811 	 * ixpcom cannot detect a break.  It can only detect framing
    812 	 * errors.  And, a sequence of "framing error, framing error"
    813 	 * meens a break.  So I made this hack:
    814 	 *
    815 	 * 1. Set default magic is a sequence of "BREAK, BREAK".
    816 	 * 2. Tell cn_check_magic() a "framing error" as BREAK.
    817 	 *
    818 	 * see ixpcom_intr() too.
    819 	 *
    820 	 */
    821 	/* default magic is a couple of BREAK. */
    822 	cn_set_magic("\047\001\047\001");
    823 
    824 	ixpcomcn_sc.sc_iot = iot;
    825 	ixpcomcn_sc.sc_ioh = iobase;
    826 	ixpcomcn_sc.sc_ospeed = ospeed;
    827 	ixpcomcn_sc.sc_cflag = cflag;
    828 
    829 	cr = cflag2cr(cflag);
    830 	cr |= IXPCOMSPEED2BRD(ospeed);
    831 	cr |= CR_UE;
    832 	ixpcom_cr = cr;
    833 
    834 	/* enable the UART */
    835 	bus_space_write_4(iot, iobase, IXPCOM_CR, cr);
    836 
    837 	return (0);
    838 }
    839 
    840 void
    841 ixpcomcnprobe(cp)
    842 	struct consdev *cp;
    843 {
    844 	cp->cn_pri = CN_REMOTE;
    845 }
    846 
    847 void
    848 ixpcomcnpollc(dev, on)
    849 	dev_t dev;
    850 	int on;
    851 {
    852 }
    853 
    854 void
    855 ixpcomcnputc(dev, c)
    856 	dev_t dev;
    857 	int c;
    858 {
    859 	int			s;
    860 	bus_space_tag_t		iot = ixpcomcn_sc.sc_iot;
    861 	bus_space_handle_t	ioh = ixpcomcn_sc.sc_ioh;
    862 
    863 	s = splserial();
    864 
    865 	while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXR))
    866 		;
    867 
    868 	bus_space_write_4(iot, ioh, IXPCOM_DR, c);
    869 
    870 #ifdef DEBUG
    871 	if (c == '\r') {
    872 		while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXE))
    873 			;
    874 	}
    875 #endif
    876 
    877 	splx(s);
    878 }
    879 
    880 int
    881 ixpcomcngetc(dev)
    882         dev_t dev;
    883 {
    884 	int			c;
    885 	int			s;
    886 	bus_space_tag_t		iot = ixpcomcn_sc.sc_iot;
    887 	bus_space_handle_t	ioh = ixpcomcn_sc.sc_ioh;
    888 
    889         s = splserial();
    890 
    891 	while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_RXR))
    892 		;
    893 
    894 	c = bus_space_read_4(iot, ioh, IXPCOM_DR);
    895 	c &= 0xff;
    896 	splx(s);
    897 
    898 	return (c);
    899 }
    900 
    901 inline static void
    902 ixpcom_txsoft(sc, tp)
    903 	struct ixpcom_softc *sc;
    904 	struct tty *tp;
    905 {
    906 	CLR(tp->t_state, TS_BUSY);
    907 	if (ISSET(tp->t_state, TS_FLUSH))
    908 		CLR(tp->t_state, TS_FLUSH);
    909         else
    910 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
    911 	(*tp->t_linesw->l_start)(tp);
    912 }
    913 
    914 inline static void
    915 ixpcom_rxsoft(sc, tp)
    916 	struct ixpcom_softc *sc;
    917 	struct tty *tp;
    918 {
    919 	int (*rint) __P((int c, struct tty *tp)) = tp->t_linesw->l_rint;
    920 	u_char *get, *end;
    921 	u_int cc, scc;
    922 	u_char lsr;
    923 	int code;
    924 	int s;
    925 
    926 	end = sc->sc_ebuf;
    927 	get = sc->sc_rbget;
    928 	scc = cc = IXPCOM_RING_SIZE - sc->sc_rbavail;
    929 #if 0
    930 	if (cc == IXPCOM_RING_SIZE) {
    931 		sc->sc_floods++;
    932 		if (sc->sc_errors++ == 0)
    933 			callout_reset(&sc->sc_diag_callout, 60 * hz,
    934 			    comdiag, sc);
    935 	}
    936 #endif
    937 	while (cc) {
    938 		code = get[0];
    939 		lsr = get[1];
    940 		if (ISSET(lsr, DR_ROR | DR_FRE | DR_PRE)) {
    941 #if 0
    942 			if (ISSET(lsr, DR_ROR)) {
    943 				sc->sc_overflows++;
    944 				if (sc->sc_errors++ == 0)
    945 					callout_reset(&sc->sc_diag_callout,
    946 					    60 * hz, comdiag, sc);
    947 			}
    948 #endif
    949 			if (ISSET(lsr, DR_FRE))
    950 				SET(code, TTY_FE);
    951 			if (ISSET(lsr, DR_PRE))
    952 				SET(code, TTY_PE);
    953 		}
    954 		if ((*rint)(code, tp) == -1) {
    955 			/*
    956 			 * The line discipline's buffer is out of space.
    957 			 */
    958 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
    959 				/*
    960 				 * We're either not using flow control, or the
    961 				 * line discipline didn't tell us to block for
    962 				 * some reason.  Either way, we have no way to
    963 				 * know when there's more space available, so
    964 				 * just drop the rest of the data.
    965 				 */
    966 				get += cc << 1;
    967 				if (get >= end)
    968 					get -= IXPCOM_RING_SIZE << 1;
    969 				cc = 0;
    970 			} else {
    971 				/*
    972 				 * Don't schedule any more receive processing
    973 				 * until the line discipline tells us there's
    974 				 * space available (through comhwiflow()).
    975 				 * Leave the rest of the data in the input
    976 				 * buffer.
    977 				 */
    978 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
    979 			}
    980 			break;
    981 		}
    982 		get += 2;
    983 		if (get >= end)
    984 			get = sc->sc_rbuf;
    985 		cc--;
    986 	}
    987 
    988 	if (cc != scc) {
    989 		sc->sc_rbget = get;
    990 		s = splserial();
    991 		COM_LOCK(sc);
    992 
    993 		cc = sc->sc_rbavail += scc - cc;
    994 		/* Buffers should be ok again, release possible block. */
    995 		if (cc >= 1) {
    996 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
    997 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
    998 				SET(sc->sc_rie, CR_RIE);
    999 				ixpcom_set_cr(sc);
   1000 			}
   1001 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1002 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1003 #if 0
   1004 				com_hwiflow(sc);
   1005 #endif
   1006 			}
   1007 		}
   1008 		COM_UNLOCK(sc);
   1009 		splx(s);
   1010 	}
   1011 }
   1012 
   1013 static void
   1014 ixpcomsoft(void* arg)
   1015 {
   1016 	struct ixpcom_softc *sc = arg;
   1017 
   1018 	if (COM_ISALIVE(sc) == 0)
   1019 		return;
   1020 
   1021 	if (sc->sc_rx_ready) {
   1022 		sc->sc_rx_ready = 0;
   1023 		ixpcom_rxsoft(sc, sc->sc_tty);
   1024 	}
   1025 	if (sc->sc_tx_done) {
   1026 		sc->sc_tx_done = 0;
   1027 		ixpcom_txsoft(sc, sc->sc_tty);
   1028 	}
   1029 }
   1030 
   1031 int
   1032 ixpcomintr(void* arg)
   1033 {
   1034 	struct ixpcom_softc *sc = arg;
   1035 	bus_space_tag_t iot = sc->sc_iot;
   1036 	bus_space_handle_t ioh = sc->sc_ioh;
   1037 	u_char *put, *end;
   1038 	u_int cc;
   1039 	u_int cr;
   1040 	u_int sr;
   1041 	u_int32_t c;
   1042 
   1043 	if (COM_ISALIVE(sc) == 0)
   1044 		return (0);
   1045 
   1046 	COM_LOCK(sc);
   1047 	cr = bus_space_read_4(iot, ioh, IXPCOM_CR) & CR_UE;
   1048 
   1049 	if (!cr) {
   1050 		COM_UNLOCK(sc);
   1051 		return (0);
   1052 	}
   1053 
   1054 	sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
   1055 	if (!ISSET(sr, SR_TXR | SR_RXR))
   1056 		return (0);
   1057 
   1058 	/*
   1059 	 * XXX
   1060 	 *
   1061 	 * ixpcom cannot detect a break.  It can only detect framing
   1062 	 * errors.  And, a sequence of "framing error, framing error"
   1063 	 * meens a break.  So I made this hack:
   1064 	 *
   1065 	 * 1. Set default magic is a sequence of "BREAK, BREAK".
   1066 	 * 2. Tell cn_check_magic() a "framing error" as BREAK.
   1067 	 *
   1068 	 * see ixpcomcnattach() too.
   1069 	 *
   1070 	 */
   1071 	if (ISSET(sr, SR_FRE)) {
   1072 		cn_check_magic(sc->sc_tty->t_dev,
   1073 			       CNC_BREAK, ixpcom_cnm_state);
   1074 	}
   1075 
   1076 	end = sc->sc_ebuf;
   1077 	put = sc->sc_rbput;
   1078 	cc = sc->sc_rbavail;
   1079 
   1080 	if (ISSET(sr, SR_RXR)) {
   1081 		if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1082 			while (cc > 0) {
   1083 				if (!ISSET(sr, SR_RXR))
   1084 					break;
   1085 				c = bus_space_read_4(iot, ioh, IXPCOM_DR);
   1086 				if (ISSET(c, DR_FRE)) {
   1087 					cn_check_magic(sc->sc_tty->t_dev,
   1088 						       CNC_BREAK,
   1089 						       ixpcom_cnm_state);
   1090 				}
   1091 				put[0] = c & 0xff;
   1092 				put[1] = (c >> 8) & 0xff;
   1093 				cn_check_magic(sc->sc_tty->t_dev,
   1094 					       put[0], ixpcom_cnm_state);
   1095 				put += 2;
   1096 				if (put >= end)
   1097 					put = sc->sc_rbuf;
   1098 				cc--;
   1099 
   1100 				sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
   1101 			}
   1102 
   1103 			/*
   1104 			 * Current string of incoming characters ended because
   1105 			 * no more data was available or we ran out of space.
   1106 			 * Schedule a receive event if any data was received.
   1107 			 * If we're out of space, turn off receive interrupts.
   1108 			 */
   1109 			sc->sc_rbput = put;
   1110 			sc->sc_rbavail = cc;
   1111 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1112 				sc->sc_rx_ready = 1;
   1113 
   1114 			/*
   1115 			 * See if we are in danger of overflowing a buffer. If
   1116 			 * so, use hardware flow control to ease the pressure.
   1117 			 */
   1118 
   1119 			/* but ixpcom cannot. X-( */
   1120 
   1121 			/*
   1122 			 * If we're out of space, disable receive interrupts
   1123 			 * until the queue has drained a bit.
   1124 			 */
   1125 			if (!cc) {
   1126 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1127 				CLR(sc->sc_rie, CR_RIE);
   1128 				ixpcom_set_cr(sc);
   1129 			}
   1130 		} else {
   1131 #ifdef DIAGNOSTIC
   1132 			panic("ixpcomintr: we shouldn't reach here");
   1133 #endif
   1134 			CLR(sc->sc_rie, CR_RIE);
   1135 			ixpcom_set_cr(sc);
   1136 		}
   1137 	}
   1138 
   1139 	/*
   1140 	 * Done handling any receive interrupts. See if data can be
   1141 	 * transmitted as well. Schedule tx done event if no data left
   1142 	 * and tty was marked busy.
   1143 	 */
   1144 	sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
   1145 	if (ISSET(sr, SR_TXR)) {
   1146 		/*
   1147 		 * If we've delayed a parameter change, do it now, and restart
   1148 		 * output.
   1149 		 */
   1150 		if (sc->sc_heldchange) {
   1151 			ixpcom_set_cr(sc);
   1152 			sc->sc_heldchange = 0;
   1153 			sc->sc_tbc = sc->sc_heldtbc;
   1154 			sc->sc_heldtbc = 0;
   1155 		}
   1156 
   1157 		/* Output the next chunk of the contiguous buffer, if any. */
   1158 		if (sc->sc_tbc > 0) {
   1159 			ixpcom_filltx(sc);
   1160 		} else {
   1161 			/* Disable transmit completion interrupts if necessary. */
   1162 			if (ISSET(sc->sc_xie, CR_XIE)) {
   1163 				CLR(sc->sc_xie, CR_XIE);
   1164 				ixpcom_set_cr(sc);
   1165 			}
   1166 			if (sc->sc_tx_busy) {
   1167 				sc->sc_tx_busy = 0;
   1168 				sc->sc_tx_done = 1;
   1169 			}
   1170 		}
   1171 	}
   1172 	COM_UNLOCK(sc);
   1173 
   1174 	/* Wake up the poller. */
   1175 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1176 	softintr_schedule(sc->sc_si);
   1177 #else
   1178 	setsoftserial();
   1179 #endif
   1180 
   1181 #if NRND > 0 && defined(RND_COM)
   1182 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   1183 #endif
   1184 	return (1);
   1185 }
   1186 
   1187