Home | History | Annotate | Line # | Download | only in windermere
wmcom.c revision 1.2
      1 /*      $NetBSD: wmcom.c,v 1.2 2014/03/16 05:20:23 dholland Exp $      */
      2 /*
      3  * Copyright (c) 2012 KIYOHARA Takashi
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: wmcom.c,v 1.2 2014/03/16 05:20:23 dholland Exp $");
     29 
     30 #include "rnd.h"
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/conf.h>
     35 #include <sys/device.h>
     36 #include <sys/errno.h>
     37 #include <sys/fcntl.h>
     38 #include <sys/intr.h>
     39 #include <sys/kauth.h>
     40 #include <sys/lwp.h>
     41 #include <sys/malloc.h>
     42 #include <sys/systm.h>
     43 #include <sys/termios.h>
     44 #include <sys/tty.h>
     45 #include <sys/types.h>
     46 
     47 #include <epoc32/windermere/windermerereg.h>
     48 #include <epoc32/windermere/windermerevar.h>
     49 
     50 #include <dev/cons.h>
     51 
     52 #ifdef RND_COM
     53 #include <sys/rnd.h>
     54 #endif
     55 
     56 #include "ioconf.h"
     57 #include "locators.h"
     58 
     59 #define COMUNIT_MASK	0x7ffff
     60 #define COMDIALOUT_MASK	0x80000
     61 
     62 #define COMUNIT(x)	(minor(x) & COMUNIT_MASK)
     63 #define COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
     64 
     65 #define WMCOM_RING_SIZE	2048
     66 
     67 struct wmcom_softc {
     68 	device_t sc_dev;
     69 	bus_space_tag_t sc_iot;
     70 	bus_space_handle_t sc_ioh;
     71 
     72 	void *sc_si;
     73 
     74 	struct tty *sc_tty;
     75 
     76 	u_char *sc_tba;
     77 	u_int sc_tbc;
     78 	u_char *sc_rbuf;
     79 	char *volatile sc_rbget;
     80 	char *volatile sc_rbput;
     81 	volatile int sc_rbavail;
     82 
     83 	int sc_tx_done;
     84 	int sc_rx_ready;
     85 
     86 	int sc_hwflags;
     87 #define COM_HW_CONSOLE	(1 << 0)
     88 #define COM_HW_DEV_OK	(1 << 1)
     89 #define COM_HW_KGDB	(1 << 2)
     90 	int sc_swflags;
     91 
     92 	int sc_flags;
     93 #define WMCOM_IRDA	(1 << 0)
     94 
     95 #ifdef RND_COM
     96 	krandsource_t rnd_source;
     97 #endif
     98 };
     99 
    100 static int wmcom_match(device_t, cfdata_t, void *);
    101 static void wmcom_attach(device_t, device_t, void *);
    102 
    103 static int wmcom_intr(void *);
    104 static void wmcom_soft(void *);
    105 
    106 static void wmcom_start(struct tty *);
    107 static int wmcom_param(struct tty *, struct termios *);
    108 static int wmcom_hwiflow(struct tty *, int);
    109 
    110 dev_type_open(wmcomopen);
    111 dev_type_close(wmcomclose);
    112 dev_type_read(wmcomread);
    113 dev_type_write(wmcomwrite);
    114 dev_type_ioctl(wmcomioctl);
    115 dev_type_stop(wmcomstop);
    116 dev_type_tty(wmcomtty);
    117 dev_type_poll(wmcompoll);
    118 
    119 static void wmcom_iflush(struct wmcom_softc *);
    120 static void wmcom_shutdown(struct wmcom_softc *);
    121 static void wmcom_break(struct wmcom_softc *, int);
    122 
    123 static void wmcom_rxsoft(struct wmcom_softc *, struct tty *);
    124 
    125 static inline uint32_t wmcom_rate2lcr(int);
    126 static uint8_t wmcom_cflag2fcr(tcflag_t);
    127 
    128 static int wmcom_cngetc(dev_t);
    129 static void wmcom_cnputc(dev_t, int);
    130 static void wmcom_cnpollc(dev_t, int);
    131 
    132 CFATTACH_DECL_NEW(wmcom, sizeof(struct wmcom_softc),
    133     wmcom_match, wmcom_attach, NULL, NULL);
    134 
    135 const struct cdevsw wmcom_cdevsw = {
    136 	.d_open = wmcomopen,
    137 	.d_close = wmcomclose,
    138 	.d_read = wmcomread,
    139 	.d_write = wmcomwrite,
    140 	.d_ioctl = wmcomioctl,
    141 	.d_stop = wmcomstop,
    142 	.d_tty = wmcomtty,
    143 	.d_poll = wmcompoll,
    144 	.d_mmap = nommap,
    145 	.d_kqfilter = ttykqfilter,
    146 	.d_flag = D_TTY
    147 };
    148 
    149 static struct cnm_state wmcom_cnm_state;
    150 static vaddr_t wmcom_cnaddr;
    151 static int wmcom_cnrate;
    152 static tcflag_t wmcom_cncflag;
    153 
    154 
    155 /* ARGSUSED */
    156 static int
    157 wmcom_match(device_t parent, cfdata_t match, void *aux)
    158 {
    159 	struct windermere_attach_args *aa = aux;
    160 
    161 	/* Wildcard not accept */
    162 	if (aa->aa_offset == WINDERMERECF_OFFSET_DEFAULT ||
    163 	    aa->aa_irq == WINDERMERECF_IRQ_DEFAULT)
    164 		return 0;
    165 
    166 	aa->aa_size = UART_SIZE;
    167 	return 1;
    168 }
    169 
    170 /* ARGSUSED */
    171 static void
    172 wmcom_attach(device_t parent, device_t self, void *aux)
    173 {
    174 	struct wmcom_softc *sc = device_private(self);
    175 	struct windermere_attach_args *aa = aux;
    176 
    177 	aprint_naive("\n");
    178 	aprint_normal("\n");
    179 
    180 	sc->sc_dev = self;
    181 	if (windermere_bus_space_subregion(aa->aa_iot, *aa->aa_ioh,
    182 				aa->aa_offset, aa->aa_size, &sc->sc_ioh) != 0) {
    183 		aprint_error_dev(self, "can't map registers\n");
    184 		return;
    185 	}
    186 	sc->sc_iot = aa->aa_iot;
    187 	if (intr_establish(aa->aa_irq, IPL_SERIAL, 0, wmcom_intr, sc) == NULL) {
    188 		aprint_error_dev(self, "can't establish interrupt\n");
    189 		return;
    190 	}
    191 
    192 	if (aa->aa_offset == (wmcom_cnaddr & 0xfff))
    193 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    194 
    195 	if (aa->aa_offset == WINDERMERE_COM0_OFFSET)
    196 		SET(sc->sc_flags, WMCOM_IRDA);
    197 
    198 	sc->sc_tty = tty_alloc();
    199 	sc->sc_tty->t_oproc = wmcom_start;
    200 	sc->sc_tty->t_param = wmcom_param;
    201 	sc->sc_tty->t_hwiflow = wmcom_hwiflow;
    202 
    203 	sc->sc_tbc = 0;
    204 	sc->sc_rbuf = malloc(WMCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
    205 	if (sc->sc_rbuf == NULL) {
    206 		aprint_error_dev(self, "unable to allocate ring buffer\n");
    207 		return;
    208 	}
    209 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    210 	sc->sc_rbavail = WMCOM_RING_SIZE;
    211 
    212 	tty_attach(sc->sc_tty);
    213 
    214 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    215 		int maj = cdevsw_lookup_major(&wmcom_cdevsw);
    216 
    217 		sc->sc_tty->t_dev = makedev(maj, device_unit(sc->sc_dev));
    218 		cn_tab->cn_dev = sc->sc_tty->t_dev;
    219 
    220 		aprint_normal_dev(self, "console\n");
    221 	}
    222 
    223 	sc->sc_si = softint_establish(SOFTINT_SERIAL, wmcom_soft, sc);
    224 
    225 #ifdef RND_COM
    226 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    227 	    RND_TYPE_TTY, 0);
    228 #endif
    229 
    230 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    231 }
    232 
    233 static int
    234 wmcom_intr(void *arg)
    235 {
    236 	struct wmcom_softc *sc = arg;
    237 	bus_space_tag_t iot = sc->sc_iot;
    238 	bus_space_handle_t ioh = sc->sc_ioh;
    239 	int cc;
    240 	uint32_t data;
    241 	uint8_t fr, intm;
    242 	u_char *put;
    243 
    244 	if (!device_is_active(sc->sc_dev))
    245 		return 0;
    246 
    247 	fr = bus_space_read_1(iot, ioh, UARTFR);
    248 	intm = bus_space_read_1(iot, ioh, UARTINTM);
    249 	if (bus_space_read_1(iot, ioh, UARTINT) & INT_RXINT) {
    250 		put = sc->sc_rbput;
    251 		cc = sc->sc_rbavail;
    252 		while (cc > 0) {
    253 			if (ISSET(fr, FR_RXFE))
    254 				break;
    255 			data = bus_space_read_4(iot, ioh, UARTDR);
    256 			cn_check_magic(sc->sc_tty->t_dev, data & 0xff,
    257 			    wmcom_cnm_state);
    258 
    259 			put[0] = data & 0xff;
    260 			put[1] = (data >> 8) & 0xff;
    261 			put += 2;
    262 			if (put >= sc->sc_rbuf + (WMCOM_RING_SIZE << 1))
    263 				put = sc->sc_rbuf;
    264 			cc--;
    265 			sc->sc_rx_ready = 1;
    266 
    267 			fr = bus_space_read_1(iot, ioh, UARTFR);
    268 		}
    269 
    270 		/*
    271 		 * Current string of incoming characters ended because
    272 		 * no more data was available or we ran out of space.
    273 		 * Schedule a receive event if any data was received.
    274 		 * If we're out of space, turn off receive interrupts.
    275 		 */
    276 		sc->sc_rbput = put;
    277 		sc->sc_rbavail = cc;
    278 
    279 		/*
    280 		 * See if we are in danger of overflowing a buffer. If
    281 		 * so, use hardware flow control to ease the pressure.
    282 		 */
    283 
    284 		/* but wmcom cannot. X-( */
    285 
    286 		/*
    287 		 * If we're out of space, disable receive interrupts
    288 		 * until the queue has drained a bit.
    289 		 */
    290 		if (cc <= 0)
    291 			CLR(intm, INT_RXINT);
    292 	}
    293 
    294 	/*
    295 	 * Done handling any receive interrupts. See if data can be
    296 	 * transmitted as well. Schedule tx done event if no data left
    297 	 * and tty was marked busy.
    298 	 */
    299 
    300 	if (!ISSET(fr, FR_TXFF)) {
    301 		/* Output the next chunk of the contiguous buffer, if any. */
    302 		if (sc->sc_tbc > 0) {
    303 			while (sc->sc_tbc > 0 && !ISSET(fr, FR_TXFF)) {
    304 				bus_space_write_1(iot, ioh, UARTDR,
    305 				    *sc->sc_tba);
    306 				sc->sc_tba++;
    307 				sc->sc_tbc--;
    308 				fr = bus_space_read_1(iot, ioh, UARTFR);
    309 			}
    310 		} else if (!ISSET(fr, FR_BUSY) && ISSET(intm, INT_TXINT)) {
    311 			CLR(intm, INT_TXINT);
    312 			sc->sc_tx_done = 1;
    313 		}
    314 	}
    315 
    316 	bus_space_write_1(iot, ioh, UARTINTM, intm);
    317 
    318 	/* Wake up the poller. */
    319 	softint_schedule(sc->sc_si);
    320 
    321 	return 1;
    322 }
    323 
    324 static void
    325 wmcom_soft(void *arg)
    326 {
    327 	struct wmcom_softc *sc = arg;
    328 	struct tty *tp = sc->sc_tty;
    329 
    330 	if (!device_is_active(sc->sc_dev))
    331 		return;
    332 
    333 	if (sc->sc_rx_ready) {
    334 		sc->sc_rx_ready = 0;
    335 		wmcom_rxsoft(sc, tp);
    336 	}
    337 	if (sc->sc_tx_done) {
    338 		sc->sc_tx_done = 0;
    339 		CLR(tp->t_state, TS_BUSY);
    340 		if (ISSET(tp->t_state, TS_FLUSH))
    341 			CLR(tp->t_state, TS_FLUSH);
    342 		else
    343 			ndflush(&tp->t_outq,
    344 			    (int)(sc->sc_tba - tp->t_outq.c_cf));
    345 		(*tp->t_linesw->l_start)(tp);
    346 	}
    347 }
    348 
    349 static void
    350 wmcom_start(struct tty *tp)
    351 {
    352 	struct wmcom_softc *sc
    353 		= device_lookup_private(&wmcom_cd, COMUNIT(tp->t_dev));
    354 	bus_space_tag_t iot = sc->sc_iot;
    355 	bus_space_handle_t ioh = sc->sc_ioh;
    356 	int s, n;
    357 	uint8_t intm;
    358 
    359 	if (!device_is_active(sc->sc_dev))
    360 		return;
    361 
    362 	s = spltty();
    363 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    364 		goto out;
    365 	if (!ttypull(tp))
    366 		goto out;
    367 
    368 	/* Grab the first contiguous region of buffer space. */
    369 	{
    370 		u_char *tba;
    371 		int tbc;
    372 
    373 		tba = tp->t_outq.c_cf;
    374 		tbc = ndqb(&tp->t_outq, 0);
    375 
    376 		(void)splserial();
    377 
    378 		sc->sc_tba = tba;
    379 		sc->sc_tbc = tbc;
    380 	}
    381 
    382 	SET(tp->t_state, TS_BUSY);
    383 
    384 	intm = bus_space_read_1(iot, ioh, UARTINTM);
    385 	if (!ISSET(intm, INT_TXINT)) {
    386 		bus_space_write_1(iot, ioh, UARTINTM, intm | INT_TXINT);
    387 
    388 		/* Output the first chunk of the contiguous buffer. */
    389 		n = min(sc->sc_tbc, UART_FIFO_SIZE);
    390 		bus_space_write_multi_1(iot, ioh, UARTDR, sc->sc_tba, n);
    391 		sc->sc_tba += n;
    392 		sc->sc_tbc -= n;
    393 	}
    394 out:
    395 	splx(s);
    396 	return;
    397 }
    398 
    399 static int
    400 wmcom_param(struct tty *tp, struct termios *t)
    401 {
    402 	struct wmcom_softc *sc =
    403 	    device_lookup_private(&wmcom_cd, COMUNIT(tp->t_dev));
    404 	bus_space_tag_t iot = sc->sc_iot;
    405 	bus_space_handle_t ioh = sc->sc_ioh;
    406 	int s;
    407 
    408 	if (!device_is_active(sc->sc_dev))
    409 		return ENXIO;
    410 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    411 		return EINVAL;
    412 
    413 	/*
    414 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    415 	 * is always active.
    416 	 */
    417 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    418 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    419 		SET(t->c_cflag, CLOCAL);
    420 		CLR(t->c_cflag, HUPCL);
    421 	}
    422 
    423 	/*
    424 	 * If there were no changes, don't do anything.  This avoids dropping
    425 	 * input and improves performance when all we did was frob things like
    426 	 * VMIN and VTIME.
    427 	 */
    428 	if (tp->t_ospeed == t->c_ospeed &&
    429 	    tp->t_cflag == t->c_cflag)
    430 		return 0;
    431 
    432 	s = splserial();
    433 	bus_space_write_4(iot, ioh, UARTLCR, wmcom_rate2lcr(t->c_ospeed));
    434 	bus_space_write_1(iot, ioh, UARTFCR, wmcom_cflag2fcr(t->c_cflag));
    435 
    436 	/* And copy to tty. */
    437 	tp->t_ispeed = 0;
    438 	tp->t_ospeed = t->c_ospeed;
    439 	tp->t_cflag = t->c_cflag;
    440 	splx(s);
    441 
    442 	/*
    443 	 * Update the tty layer's idea of the carrier bit.
    444 	 * We tell tty the carrier is always on.
    445 	 */
    446 	(*tp->t_linesw->l_modem)(tp, 1);
    447 
    448 	return 0;
    449 }
    450 
    451 static int
    452 wmcom_hwiflow(struct tty *tp, int block)
    453 {
    454 	/* Nothing */
    455 	return 0;
    456 }
    457 
    458 /* ARGSUSED */
    459 int
    460 wmcomopen(dev_t dev, int flag, int mode, struct lwp *l)
    461 {
    462 	struct wmcom_softc *sc;
    463 	struct tty *tp;
    464 	int error, s, s2;
    465 	uint8_t con;
    466 
    467 	sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
    468 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
    469 		return ENXIO;
    470 	if (!device_is_active(sc->sc_dev))
    471 		return ENXIO;
    472 
    473 #ifdef KGDB
    474 	/*
    475 	 * If this is the kgdb port, no other use is permitted.
    476 	 */
    477 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
    478 		return EBUSY;
    479 #endif
    480 
    481 	tp = sc->sc_tty;
    482 
    483 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    484 		return EBUSY;
    485 
    486 	s = spltty();
    487 
    488 	/*
    489 	 * Do the following iff this is a first open.
    490 	 */
    491 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    492 		struct termios t;
    493 
    494 		tp->t_dev = dev;
    495 
    496 		/* Enable and turn on interrupt */
    497 		con = CON_UARTEN;
    498 		if (ISSET(sc->sc_flags, WMCOM_IRDA))
    499 			con |= CON_IRTXM;
    500 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTCON, con);
    501 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTINTM, INT_RXINT);
    502 
    503 		/*
    504 		 * Initialize the termios status to the defaults.  Add in the
    505 		 * sticky bits from TIOCSFLAGS.
    506 		 */
    507 		t.c_ispeed = 0;
    508 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    509 			t.c_ospeed = wmcom_cnrate;
    510 			t.c_cflag = wmcom_cncflag;
    511 		} else {
    512 			t.c_ospeed = TTYDEF_SPEED;
    513 			t.c_cflag = TTYDEF_CFLAG;
    514 		}
    515 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    516 			SET(t.c_cflag, CLOCAL);
    517 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    518 			SET(t.c_cflag, CRTSCTS);
    519 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    520 			SET(t.c_cflag, MDMBUF);
    521 		/* Make sure wmcom_param() we do something */
    522 		tp->t_ospeed = 0;
    523 		wmcom_param(tp, &t);
    524 		tp->t_iflag = TTYDEF_IFLAG;
    525 		tp->t_oflag = TTYDEF_OFLAG;
    526 		tp->t_lflag = TTYDEF_LFLAG;
    527 		ttychars(tp);
    528 		ttsetwater(tp);
    529 
    530 		s2 = splserial();
    531 
    532 		/* Clear the input ring. */
    533 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    534 		sc->sc_rbavail = WMCOM_RING_SIZE;
    535 		wmcom_iflush(sc);
    536 
    537 		splx(s2);
    538 	}
    539 
    540 	splx(s);
    541 
    542 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    543 	if (error)
    544 		goto bad;
    545 
    546 	error = (*tp->t_linesw->l_open)(dev, tp);
    547 	if (error)
    548 		goto bad;
    549 	return 0;
    550 
    551 bad:
    552 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    553 		/*
    554 		 * We failed to open the device, and nobody else had it opened.
    555 		 * Clean up the state as appropriate.
    556 		 */
    557 		wmcom_shutdown(sc);
    558 
    559 		/* Disable UART */
    560 		if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    561 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTCON, 0);
    562 	}
    563 
    564 	return error;
    565 }
    566 
    567 /* ARGSUSED */
    568 int
    569 wmcomclose(dev_t dev, int flag, int mode, struct lwp *l)
    570 {
    571 	struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
    572 	struct tty *tp = sc->sc_tty;
    573 
    574 	/* XXXX This is for cons.c. */
    575 	if (!ISSET(tp->t_state, TS_ISOPEN))
    576 		return 0;
    577 
    578 	(*tp->t_linesw->l_close)(tp, flag);
    579 	ttyclose(tp);
    580 
    581 	if (!device_is_active(sc->sc_dev))
    582 		return 0;
    583 
    584 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    585 		/*
    586 		 * Although we got a last close, the device may still be in
    587 		 * use; e.g. if this was the dialout node, and there are still
    588 		 * processes waiting for carrier on the non-dialout node.
    589 		 */
    590 		wmcom_shutdown(sc);
    591 
    592 		/* Disable UART */
    593 		if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    594 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTCON, 0);
    595 	}
    596 
    597 	return 0;
    598 }
    599 
    600 int
    601 wmcomread(dev_t dev, struct uio *uio, int flag)
    602 {
    603 	struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
    604 	struct tty *tp = sc->sc_tty;
    605 
    606 	if (!device_is_active(sc->sc_dev))
    607 		return EIO;
    608 
    609 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    610 }
    611 
    612 int
    613 wmcomwrite(dev_t dev, struct uio *uio, int flag)
    614 {
    615 	struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
    616 	struct tty *tp = sc->sc_tty;
    617 
    618 	if (!device_is_active(sc->sc_dev))
    619 		return EIO;
    620 
    621 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    622 }
    623 
    624 int
    625 wmcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    626 {
    627 	struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
    628 	struct tty *tp = sc->sc_tty;
    629 	int error, s;
    630 
    631 	if (!device_is_active(sc->sc_dev))
    632 		return EIO;
    633 
    634 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    635 	if (error != EPASSTHROUGH)
    636 		return error;
    637 
    638 	error = ttioctl(tp, cmd, data, flag, l);
    639 	if (error != EPASSTHROUGH)
    640 		return error;
    641 
    642 	switch (cmd) {
    643 	case TIOCSFLAGS:
    644 		error = kauth_authorize_device_tty(l->l_cred,
    645 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    646 		break;
    647 	default:
    648 		break;
    649 	}
    650 	if (error)
    651 		return error;
    652 
    653 	s = splserial();
    654 	error = 0;
    655 	switch (cmd) {
    656 	case TIOCSBRK:
    657 		wmcom_break(sc, 1);
    658 		break;
    659 
    660 	case TIOCCBRK:
    661 		wmcom_break(sc, 0);
    662 		break;
    663 
    664 	case TIOCGFLAGS:
    665 		*(int *)data = sc->sc_swflags;
    666 		break;
    667 
    668 	case TIOCSFLAGS:
    669 		sc->sc_swflags = *(int *)data;
    670 		break;
    671 
    672 	default:
    673 		error = EPASSTHROUGH;
    674 		break;
    675 	}
    676 	splx(s);
    677 	return error;
    678 }
    679 
    680 int
    681 wmcompoll(dev_t dev, int events, struct lwp *l)
    682 {
    683 	struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
    684 	struct tty *tp = sc->sc_tty;
    685 
    686 	if (!device_is_active(sc->sc_dev))
    687 		return EIO;
    688 
    689 	return (*tp->t_linesw->l_poll)(tp, events, l);
    690 }
    691 
    692 struct tty *
    693 wmcomtty(dev_t dev)
    694 {
    695 	struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
    696 
    697 	return sc->sc_tty;
    698 }
    699 
    700 void
    701 wmcomstop(struct tty *tp, int flag)
    702 {
    703 	int s;
    704 
    705 	s = splserial();
    706 	if (ISSET(tp->t_state, TS_BUSY)) {
    707 		/* Stop transmitting at the next chunk. */
    708 		if (!ISSET(tp->t_state, TS_TTSTOP))
    709 			SET(tp->t_state, TS_FLUSH);
    710 	}
    711 	splx(s);
    712 }
    713 
    714 
    715 static void
    716 wmcom_iflush(struct wmcom_softc *sc)
    717 {
    718 	bus_space_tag_t iot = sc->sc_iot;
    719 	bus_space_handle_t ioh = sc->sc_ioh;
    720 	int timo;
    721 
    722 	timo = 50000;
    723 	while ((bus_space_read_1(iot, ioh, UARTFR) & FR_RXFE) == 0 &&
    724 	    timo--)
    725 		bus_space_read_1(iot, ioh, UARTDR);
    726 	if (timo == 0)
    727 		printf("%s: iflush timeout\n", device_xname(sc->sc_dev));
    728 }
    729 
    730 static void
    731 wmcom_shutdown(struct wmcom_softc *sc)
    732 {
    733 	int s;
    734 
    735 	s = splserial();
    736 
    737 	/* Turn off interrupt */
    738 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTINTM, 0);
    739 
    740 	/* Clear any break condition set with TIOCSBRK. */
    741 	wmcom_break(sc, 0);
    742 
    743 	splx(s);
    744 }
    745 
    746 static void
    747 wmcom_break(struct wmcom_softc *sc, int onoff)
    748 {
    749 	int s;
    750 	uint8_t fcr;
    751 
    752 	s = splserial();
    753 	fcr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, UARTFCR);
    754 	if (onoff)
    755 		SET(fcr, FCR_BREAK);
    756 	else
    757 		CLR(fcr, FCR_BREAK);
    758 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTFCR, fcr);
    759 	splx(s);
    760 }
    761 
    762 static void
    763 wmcom_rxsoft(struct wmcom_softc *sc, struct tty *tp)
    764 {
    765 	bus_space_tag_t iot = sc->sc_iot;
    766 	bus_space_handle_t ioh = sc->sc_ioh;
    767 	int code, s;
    768 	u_int cc, scc;
    769 	uint8_t intm;
    770 	u_char sts, *get;
    771 
    772 	get = sc->sc_rbget;
    773 	scc = cc = WMCOM_RING_SIZE - sc->sc_rbavail;
    774 	while (cc) {
    775 		code = get[0];
    776 		sts = get[1];
    777 		if (ISSET(sts, RSR_FE | RSR_PE | RSR_OE)) {
    778 			if (ISSET(sts, (RSR_FE)))
    779 				SET(code, TTY_FE);
    780 			if (ISSET(sts, RSR_PE))
    781 				SET(code, TTY_PE);
    782 			if (ISSET(sts, RSR_OE))
    783 				;		/* XXXXX: Overrun */
    784 		}
    785 		if ((*tp->t_linesw->l_rint)(code, tp) == -1) {
    786 			/*
    787 			 * The line discipline's buffer is out of space.
    788 			 */
    789 			/*
    790 			 * We're either not using flow control, or the
    791 			 * line discipline didn't tell us to block for
    792 			 * some reason.  Either way, we have no way to
    793 			 * know when there's more space available, so
    794 			 * just drop the rest of the data.
    795 			 */
    796 			get += cc << 1;
    797 			if (get >= sc->sc_rbuf + (WMCOM_RING_SIZE << 1))
    798 				get -= (WMCOM_RING_SIZE << 1);
    799 			cc = 0;
    800 			break;
    801 		}
    802 		get += 2;
    803 		if (get >= sc->sc_rbuf + (WMCOM_RING_SIZE << 1))
    804 			get = sc->sc_rbuf;
    805 		cc--;
    806 	}
    807 
    808 	if (cc != scc) {
    809 		sc->sc_rbget = get;
    810 		s = splserial();
    811 
    812 		cc = sc->sc_rbavail += scc - cc;
    813 		/* Buffers should be ok again, release possible block. */
    814 		if (cc >= 1) {
    815 			intm = bus_space_read_1(iot, ioh, UARTINTM);
    816 			SET(intm, INT_RXINT);
    817 			bus_space_write_1(iot, ioh, UARTINTM, intm);
    818 		}
    819 		splx(s);
    820 	}
    821 }
    822 
    823 static inline uint32_t
    824 wmcom_rate2lcr(int rate)
    825 {
    826 
    827 	return 7372800 / (16 * rate) - 1;
    828 }
    829 
    830 static uint8_t
    831 wmcom_cflag2fcr(tcflag_t cflag)
    832 {
    833 	int8_t fcr = FCR_UFIFOEN;
    834 
    835 	switch (cflag & CSIZE) {
    836 	case CS5: SET(fcr, FCR_WLEN_5); break;
    837 	case CS6: SET(fcr, FCR_WLEN_6); break;
    838 	case CS7: SET(fcr, FCR_WLEN_7); break;
    839 	case CS8: SET(fcr, FCR_WLEN_8); break;
    840 	default:  SET(fcr, FCR_WLEN_8); break;
    841 	}
    842 	if (cflag & CSTOPB)
    843 		SET(fcr, FCR_XSTOP);
    844 	if (cflag & PARENB) {
    845 		SET(fcr, (FCR_PRTEN | FCR_EVENPRT));
    846 		if (cflag & PARODD)
    847 			CLR(fcr, FCR_EVENPRT);
    848 	}
    849 	return fcr;
    850 }
    851 
    852 #define WMCOM_CNREAD_1(offset) \
    853 	(*(volatile uint8_t *)(wmcom_cnaddr + ((offset) << 2)))
    854 #define WMCOM_CNREAD_4(offset) \
    855 	(*(volatile uint32_t *)(wmcom_cnaddr + ((offset) << 2)))
    856 #define WMCOM_CNWRITE_1(offset, val) \
    857 	(*(volatile uint8_t *)(wmcom_cnaddr + ((offset) << 2)) = val)
    858 #define WMCOM_CNWRITE_4(offset, val) \
    859 	(*(volatile uint32_t *)(wmcom_cnaddr + ((offset) << 2)) = val)
    860 
    861 static struct consdev wmcomcons = {
    862 	NULL, NULL, wmcom_cngetc, wmcom_cnputc, wmcom_cnpollc, NULL, NULL, NULL,
    863 	NODEV, CN_NORMAL
    864 };
    865 
    866 int
    867 wmcom_cnattach(vaddr_t addr, int rate, tcflag_t cflag, int irda)
    868 {
    869 
    870 	wmcom_cnaddr = addr;
    871 	wmcom_cnrate = rate;
    872 	wmcom_cncflag = cflag;
    873 	WMCOM_CNWRITE_4(UARTLCR, wmcom_rate2lcr(rate));
    874 	WMCOM_CNWRITE_1(UARTFCR, wmcom_cflag2fcr(cflag));
    875 	if (irda)
    876 		WMCOM_CNWRITE_1(UARTCON, CON_UARTEN | CON_IRTXM);
    877 	else
    878 		WMCOM_CNWRITE_1(UARTCON, CON_UARTEN);
    879 
    880 	cn_tab = &wmcomcons;
    881 	cn_init_magic(&wmcom_cnm_state);
    882 	cn_set_magic("\047\001");	/* default magic is BREAK */
    883 
    884 	return 0;
    885 }
    886 
    887 /* ARGSUSED */
    888 static int
    889 wmcom_cngetc(dev_t dev)
    890 {
    891 	int s = splserial();
    892 	char ch;
    893 
    894 	while (WMCOM_CNREAD_1(UARTFR) & FR_RXFE);
    895 
    896 	ch = WMCOM_CNREAD_4(UARTDR);
    897 
    898 	{
    899 #ifdef DDB
    900 		extern int db_active;
    901 		if (!db_active)
    902 #endif
    903 			cn_check_magic(dev, ch, wmcom_cnm_state);
    904 	}
    905 
    906 	splx(s);
    907 	return ch;
    908 }
    909 
    910 /* ARGSUSED */
    911 static void
    912 wmcom_cnputc(dev_t dev, int c)
    913 {
    914 	int s = splserial();
    915 
    916 	while (WMCOM_CNREAD_1(UARTFR) & FR_TXFF);
    917 
    918 	WMCOM_CNWRITE_1(UARTDR, c);
    919 
    920 	/* Make sure output. */
    921 	while (WMCOM_CNREAD_1(UARTFR) & FR_BUSY);
    922 
    923 	splx(s);
    924 }
    925 
    926 /* ARGSUSED */
    927 static void
    928 wmcom_cnpollc(dev_t dev, int on)
    929 {
    930 	/* Nothing */
    931 }
    932