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