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