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