Home | History | Annotate | Line # | Download | only in dev
ser.c revision 1.52.14.1
      1  1.52.14.1       tls /*	$NetBSD: ser.c,v 1.52.14.1 2014/08/20 00:02:48 tls Exp $	*/
      2        1.1       leo 
      3        1.1       leo /*-
      4        1.1       leo  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5        1.1       leo  * All rights reserved.
      6        1.1       leo  *
      7        1.1       leo  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1       leo  * by Leo Weppelman.
      9        1.1       leo  *
     10        1.1       leo  * Redistribution and use in source and binary forms, with or without
     11        1.1       leo  * modification, are permitted provided that the following conditions
     12        1.1       leo  * are met:
     13        1.1       leo  * 1. Redistributions of source code must retain the above copyright
     14        1.1       leo  *    notice, this list of conditions and the following disclaimer.
     15        1.1       leo  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1       leo  *    notice, this list of conditions and the following disclaimer in the
     17        1.1       leo  *    documentation and/or other materials provided with the distribution.
     18        1.1       leo  *
     19        1.1       leo  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1       leo  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1       leo  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1       leo  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1       leo  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1       leo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1       leo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1       leo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1       leo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1       leo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1       leo  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1       leo  */
     31        1.6       leo /*-
     32        1.6       leo  * Copyright (c) 1993, 1994, 1995, 1996, 1997
     33        1.6       leo  *      Charles M. Hannum.  All rights reserved.
     34        1.6       leo  *
     35        1.6       leo  * Interrupt processing and hardware flow control partly based on code from
     36        1.6       leo  * Onno van der Linden and Gordon Ross.
     37        1.6       leo  *
     38        1.6       leo  * Redistribution and use in source and binary forms, with or without
     39        1.6       leo  * modification, are permitted provided that the following conditions
     40        1.6       leo  * are met:
     41        1.6       leo  * 1. Redistributions of source code must retain the above copyright
     42        1.6       leo  *    notice, this list of conditions and the following disclaimer.
     43        1.6       leo  * 2. Redistributions in binary form must reproduce the above copyright
     44        1.6       leo  *    notice, this list of conditions and the following disclaimer in the
     45        1.6       leo  *    documentation and/or other materials provided with the distribution.
     46        1.6       leo  * 3. All advertising materials mentioning features or use of this software
     47        1.6       leo  *    must display the following acknowledgement:
     48        1.6       leo  *      This product includes software developed by Charles M. Hannum.
     49        1.6       leo  * 4. The name of the author may not be used to endorse or promote products
     50        1.6       leo  *    derived from this software without specific prior written permission.
     51        1.6       leo  *
     52        1.6       leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     53        1.6       leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     54        1.6       leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     55        1.6       leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     56        1.6       leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     57        1.6       leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     58        1.6       leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     59        1.6       leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     60        1.7        pk  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     61        1.6       leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62        1.6       leo  */
     63        1.6       leo 
     64        1.6       leo /*
     65        1.6       leo  * Copyright (c) 1991 The Regents of the University of California.
     66        1.6       leo  * All rights reserved.
     67        1.6       leo  *
     68        1.6       leo  * Redistribution and use in source and binary forms, with or without
     69        1.6       leo  * modification, are permitted provided that the following conditions
     70        1.6       leo  * are met:
     71        1.6       leo  * 1. Redistributions of source code must retain the above copyright
     72        1.6       leo  *    notice, this list of conditions and the following disclaimer.
     73        1.6       leo  * 2. Redistributions in binary form must reproduce the above copyright
     74        1.6       leo  *    notice, this list of conditions and the following disclaimer in the
     75        1.6       leo  *    documentation and/or other materials provided with the distribution.
     76       1.23       agc  * 3. Neither the name of the University nor the names of its contributors
     77        1.6       leo  *    may be used to endorse or promote products derived from this software
     78        1.6       leo  *    without specific prior written permission.
     79        1.6       leo  *
     80        1.6       leo  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     81        1.6       leo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     82        1.6       leo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     83        1.6       leo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     84        1.6       leo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     85        1.6       leo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     86        1.6       leo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     87        1.6       leo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     88        1.6       leo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     89        1.6       leo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     90        1.6       leo  * SUCH DAMAGE.
     91        1.6       leo  *
     92        1.6       leo  *      @(#)com.c       7.5 (Berkeley) 5/16/91
     93        1.6       leo  */
     94       1.22     lukem 
     95       1.22     lukem #include <sys/cdefs.h>
     96  1.52.14.1       tls __KERNEL_RCSID(0, "$NetBSD: ser.c,v 1.52.14.1 2014/08/20 00:02:48 tls Exp $");
     97        1.6       leo 
     98        1.5  jonathan #include "opt_ddb.h"
     99       1.13       leo #include "opt_mbtype.h"
    100       1.17       leo #include "opt_serconsole.h"
    101        1.5  jonathan 
    102        1.1       leo #include <sys/param.h>
    103        1.1       leo #include <sys/systm.h>
    104        1.1       leo #include <sys/ioctl.h>
    105        1.1       leo #include <sys/select.h>
    106        1.1       leo #include <sys/tty.h>
    107        1.1       leo #include <sys/proc.h>
    108        1.1       leo #include <sys/conf.h>
    109        1.1       leo #include <sys/file.h>
    110        1.1       leo #include <sys/uio.h>
    111        1.1       leo #include <sys/kernel.h>
    112        1.1       leo #include <sys/syslog.h>
    113        1.1       leo #include <sys/types.h>
    114        1.1       leo #include <sys/device.h>
    115       1.29      elad #include <sys/kauth.h>
    116        1.1       leo 
    117        1.1       leo #include <m68k/asm_single.h>
    118        1.1       leo 
    119        1.1       leo #include <machine/iomap.h>
    120        1.1       leo #include <machine/mfp.h>
    121       1.44   tsutsui #include <machine/intr.h>
    122       1.13       leo #include <atari/dev/serreg.h>
    123       1.13       leo 
    124       1.47   tsutsui #include "ioconf.h"
    125       1.47   tsutsui 
    126       1.13       leo #if !defined(_MILANHW_)
    127        1.1       leo #include <atari/dev/ym2149reg.h>
    128       1.13       leo #else
    129       1.13       leo 	/* MILAN has no ym2149 */
    130       1.13       leo #define ym2149_dtr(set) {					\
    131       1.13       leo 	if (set)						\
    132       1.13       leo 		single_inst_bset_b(MFP->mf_gpip, 0x08);		\
    133       1.46   tsutsui 	else							\
    134       1.46   tsutsui 		single_inst_bclr_b(MFP->mf_gpip, 0x08);		\
    135       1.13       leo }
    136       1.13       leo 
    137       1.13       leo #define ym2149_rts(set) {					\
    138       1.13       leo 	if (set)						\
    139       1.13       leo 		single_inst_bset_b(MFP->mf_gpip, 0x01);		\
    140       1.46   tsutsui 	else							\
    141       1.46   tsutsui 		single_inst_bclr_b(MFP->mf_gpip, 0x01);		\
    142       1.13       leo }
    143       1.13       leo #endif /* _MILANHW_ */
    144        1.1       leo 
    145        1.1       leo /* #define SER_DEBUG */
    146        1.1       leo 
    147        1.4       leo #define	SERUNIT(x)	(minor(x) & 0x7ffff)
    148        1.4       leo #define	SERDIALOUT(x)	(minor(x) & 0x80000)
    149        1.1       leo 
    150        1.1       leo /* XXX */
    151        1.1       leo #define	CONSBAUD	9600
    152        1.1       leo #define	CONSCFLAG	TTYDEF_CFLAG
    153        1.1       leo /* end XXX */
    154        1.1       leo 
    155        1.1       leo /* Buffer size for character buffer */
    156        1.1       leo #define RXBUFSIZE 2048		/* More than enough..			*/
    157        1.1       leo #define RXBUFMASK (RXBUFSIZE-1)	/* Only iff previous is a power of 2	*/
    158        1.1       leo #define RXHIWAT   (RXBUFSIZE >> 2)
    159        1.1       leo 
    160        1.1       leo struct ser_softc {
    161       1.48   tsutsui 	device_t	 sc_dev;
    162        1.1       leo 	struct tty	*sc_tty;
    163        1.1       leo 
    164       1.10   thorpej 	struct callout sc_diag_ch;
    165       1.10   thorpej 
    166        1.1       leo 	int		 sc_overflows;
    167        1.1       leo 	int		 sc_floods;
    168        1.1       leo 	int		 sc_errors;
    169        1.1       leo 
    170       1.46   tsutsui 	uint8_t		 sc_hwflags;
    171       1.46   tsutsui 	uint8_t		 sc_swflags;
    172        1.1       leo 
    173        1.1       leo 	int		 sc_ospeed;		/* delay + timer-d data	*/
    174       1.46   tsutsui 	uint8_t		 sc_imra;
    175       1.46   tsutsui 	uint8_t		 sc_imrb;
    176       1.46   tsutsui 	uint8_t		 sc_ucr;		/* Uart control		*/
    177       1.46   tsutsui 	uint8_t		 sc_msr;		/* Modem status		*/
    178       1.46   tsutsui 	uint8_t		 sc_tsr;		/* Tranceiver status	*/
    179       1.46   tsutsui 	uint8_t		 sc_rsr;		/* Receiver status	*/
    180       1.46   tsutsui 	uint8_t		 sc_mcr;		/* (Pseudo) Modem ctrl. */
    181       1.46   tsutsui 
    182       1.46   tsutsui 	uint8_t		 sc_msr_delta;
    183       1.46   tsutsui 	uint8_t		 sc_msr_mask;
    184       1.46   tsutsui 	uint8_t		 sc_mcr_active;
    185       1.46   tsutsui 	uint8_t		 sc_mcr_dtr, sc_mcr_rts, sc_msr_cts, sc_msr_dcd;
    186        1.1       leo 
    187        1.1       leo 	int		 sc_r_hiwat;
    188        1.1       leo  	volatile u_int	 sc_rbget;
    189        1.1       leo  	volatile u_int	 sc_rbput;
    190        1.1       leo 	volatile u_int	 sc_rbavail;
    191       1.46   tsutsui  	uint8_t		 sc_rbuf[RXBUFSIZE];
    192       1.46   tsutsui 	uint8_t		 sc_lbuf[RXBUFSIZE];
    193        1.1       leo 
    194       1.46   tsutsui 	volatile uint8_t sc_rx_blocked;
    195       1.46   tsutsui 	volatile uint8_t sc_rx_ready;
    196       1.46   tsutsui 	volatile uint8_t sc_tx_busy;
    197       1.46   tsutsui 	volatile uint8_t sc_tx_done;
    198       1.46   tsutsui 	volatile uint8_t sc_tx_stopped;
    199       1.46   tsutsui 	volatile uint8_t sc_st_check;
    200        1.1       leo 
    201       1.46   tsutsui  	uint8_t		*sc_tba;
    202        1.1       leo  	int		 sc_tbc;
    203        1.1       leo 	int		 sc_heldtbc;
    204        1.1       leo 
    205       1.46   tsutsui 	volatile uint8_t sc_heldchange;
    206       1.43   tsutsui 
    207       1.43   tsutsui 	void		*sc_sicookie;
    208        1.1       leo };
    209        1.1       leo 
    210        1.1       leo /*
    211        1.1       leo  * For sc_hwflags:
    212        1.1       leo  */
    213        1.1       leo #define	SER_HW_CONSOLE	0x01
    214        1.1       leo 
    215       1.51   tsutsui static void	ser_break(struct ser_softc *, int);
    216       1.51   tsutsui static void	ser_hwiflow(struct ser_softc *, int);
    217       1.51   tsutsui static void	ser_iflush(struct ser_softc *);
    218       1.51   tsutsui static void	ser_loadchannelregs(struct ser_softc *);
    219       1.51   tsutsui static void	ser_modem(struct ser_softc *, int);
    220       1.51   tsutsui static void	serdiag(void *);
    221       1.51   tsutsui static int	serhwiflow(struct tty *, int);
    222       1.51   tsutsui static void	serinit(int);
    223       1.51   tsutsui static void	serinitcons(int);
    224       1.51   tsutsui static int	sermintr(void *);
    225       1.51   tsutsui static int	sertrintr(void *);
    226       1.51   tsutsui static int	serparam(struct tty *, struct termios *);
    227       1.51   tsutsui static void	serstart(struct tty *);
    228        1.1       leo 
    229        1.1       leo struct consdev;
    230       1.40       dsl void	sercnprobe(struct consdev *);
    231       1.40       dsl void	sercninit(struct consdev *);
    232       1.40       dsl int	sercngetc(dev_t);
    233       1.40       dsl void	sercnputc(dev_t, int);
    234       1.40       dsl void	sercnpollc(dev_t, int);
    235       1.40       dsl 
    236       1.40       dsl static void sermsrint(struct ser_softc *, struct tty*);
    237       1.40       dsl static void serrxint(struct ser_softc *, struct tty*);
    238       1.40       dsl static void ser_shutdown(struct ser_softc *);
    239       1.40       dsl static int serspeed(long);
    240       1.40       dsl static void sersoft(void *);
    241       1.40       dsl static void sertxint(struct ser_softc *, struct tty*);
    242        1.1       leo 
    243        1.1       leo /*
    244        1.1       leo  * Autoconfig stuff
    245        1.1       leo  */
    246       1.48   tsutsui static int  sermatch(device_t, cfdata_t, void *);
    247       1.48   tsutsui static void serattach(device_t, device_t, void *);
    248        1.1       leo 
    249       1.48   tsutsui CFATTACH_DECL_NEW(ser, sizeof(struct ser_softc),
    250       1.20   thorpej     sermatch, serattach, NULL, NULL);
    251        1.1       leo 
    252       1.18   gehenna dev_type_open(seropen);
    253       1.18   gehenna dev_type_close(serclose);
    254       1.18   gehenna dev_type_read(serread);
    255       1.18   gehenna dev_type_write(serwrite);
    256       1.18   gehenna dev_type_ioctl(serioctl);
    257       1.18   gehenna dev_type_stop(serstop);
    258       1.18   gehenna dev_type_tty(sertty);
    259       1.18   gehenna dev_type_poll(serpoll);
    260       1.18   gehenna 
    261       1.18   gehenna const struct cdevsw ser_cdevsw = {
    262  1.52.14.1       tls 	.d_open = seropen,
    263  1.52.14.1       tls 	.d_close = serclose,
    264  1.52.14.1       tls 	.d_read = serread,
    265  1.52.14.1       tls 	.d_write = serwrite,
    266  1.52.14.1       tls 	.d_ioctl = serioctl,
    267  1.52.14.1       tls 	.d_stop = serstop,
    268  1.52.14.1       tls 	.d_tty = sertty,
    269  1.52.14.1       tls 	.d_poll = serpoll,
    270  1.52.14.1       tls 	.d_mmap = nommap,
    271  1.52.14.1       tls 	.d_kqfilter = ttykqfilter,
    272  1.52.14.1       tls 	.d_discard = nodiscard,
    273  1.52.14.1       tls 	.d_flag = D_TTY
    274       1.18   gehenna };
    275       1.18   gehenna 
    276       1.49   tsutsui #ifndef SERCONSOLE
    277       1.49   tsutsui #define SERCONSOLE	0
    278       1.49   tsutsui #endif
    279       1.49   tsutsui int serconsole = SERCONSOLE;	/* patchable */
    280       1.49   tsutsui 
    281        1.1       leo /*ARGSUSED*/
    282        1.1       leo static	int
    283       1.48   tsutsui sermatch(device_t parent, cfdata_t cf, void *aux)
    284        1.1       leo {
    285       1.46   tsutsui 	static int ser_matched = 0;
    286       1.11       leo 
    287       1.11       leo 	/* Match at most one ser unit */
    288       1.48   tsutsui 	if (strcmp((char *)aux, "ser") || ser_matched)
    289       1.11       leo 		return 0;
    290       1.11       leo 
    291       1.11       leo 	ser_matched = 1;
    292       1.11       leo 	return 1;
    293        1.1       leo }
    294        1.1       leo 
    295        1.1       leo /*ARGSUSED*/
    296        1.1       leo static void
    297       1.48   tsutsui serattach(device_t parent, device_t self, void *aux)
    298        1.1       leo {
    299       1.48   tsutsui 	struct ser_softc *sc = device_private(self);
    300       1.48   tsutsui 
    301       1.48   tsutsui 	sc->sc_dev = self;
    302        1.1       leo 
    303        1.1       leo 	if (intr_establish(1, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
    304       1.48   tsutsui 		aprint_error(": Can't establish interrupt (1)\n");
    305        1.1       leo 	if (intr_establish(2, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
    306       1.48   tsutsui 		aprint_error(": Can't establish interrupt (2)\n");
    307        1.1       leo 	if (intr_establish(14, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
    308       1.48   tsutsui 		aprint_error(": Can't establish interrupt (14)\n");
    309        1.1       leo 	if (intr_establish(9, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
    310       1.48   tsutsui 		aprint_error(": Can't establish interrupt (9)\n");
    311        1.1       leo 	if (intr_establish(10, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
    312       1.48   tsutsui 		aprint_error(": Can't establish interrupt (10)\n");
    313        1.1       leo 	if (intr_establish(11, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
    314       1.48   tsutsui 		aprint_error(": Can't establish interrupt (11)\n");
    315        1.1       leo 	if (intr_establish(12, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
    316       1.48   tsutsui 		aprint_error(": Can't establish interrupt (12)\n");
    317        1.1       leo 
    318       1.43   tsutsui 	sc->sc_sicookie = softint_establish(SOFTINT_SERIAL, sersoft, sc);
    319       1.43   tsutsui 
    320        1.1       leo 	ym2149_rts(1);
    321        1.1       leo 	ym2149_dtr(1);
    322        1.1       leo 
    323        1.1       leo 	/*
    324        1.1       leo 	 * Enable but mask interrupts...
    325        1.1       leo 	 * XXX: Look at edge-sensitivity for DCD/CTS interrupts.
    326        1.1       leo 	 */
    327        1.1       leo 	MFP->mf_ierb |= IB_SCTS|IB_SDCD;
    328        1.1       leo 	MFP->mf_iera |= IA_RRDY|IA_RERR|IA_TRDY|IA_TERR;
    329        1.1       leo 	MFP->mf_imrb &= ~(IB_SCTS|IB_SDCD);
    330        1.1       leo 	MFP->mf_imra &= ~(IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
    331        1.1       leo 
    332       1.34        ad 	callout_init(&sc->sc_diag_ch, 0);
    333       1.10   thorpej 
    334       1.49   tsutsui 	if (serconsole) {
    335       1.49   tsutsui 		/*
    336       1.49   tsutsui 		 * Activate serial console when DCD present...
    337       1.49   tsutsui 		 */
    338       1.49   tsutsui 		if ((MFP->mf_gpip & MCR_DCD) == 0)
    339       1.49   tsutsui 			SET(sc->sc_hwflags, SER_HW_CONSOLE);
    340       1.49   tsutsui 	}
    341        1.1       leo 
    342       1.48   tsutsui 	aprint_normal(": modem1 on 68901 MFP1 USART\n");
    343        1.1       leo 	if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
    344        1.1       leo 		serinit(CONSBAUD);
    345       1.48   tsutsui 		aprint_normal_dev(self, "console\n");
    346        1.1       leo 	}
    347        1.1       leo }
    348        1.1       leo 
    349        1.1       leo #ifdef SER_DEBUG
    350       1.40       dsl void serstatus(struct ser_softc *, char *);
    351        1.1       leo void
    352       1.41       dsl serstatus(struct ser_softc *sc, char *str)
    353        1.1       leo {
    354        1.1       leo 	struct tty *tp = sc->sc_tty;
    355        1.1       leo 
    356        1.1       leo 	printf("%s: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
    357       1.48   tsutsui 	    device_xname(sc->sc_dev), str,
    358        1.1       leo 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
    359        1.1       leo 	    ISSET(sc->sc_msr, MCR_DCD) ? "+" : "-",
    360        1.1       leo 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
    361        1.1       leo 	    ISSET(sc->sc_mcr, MCR_DTR) ? "+" : "-",
    362        1.1       leo 	    sc->sc_tx_stopped ? "+" : "-");
    363        1.1       leo 
    364        1.1       leo 	printf("%s: %s %scrtscts %scts %sts_ttstop  %srts %srx_blocked\n",
    365       1.48   tsutsui 	    device_xname(sc->sc_dev), str,
    366        1.1       leo 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
    367        1.1       leo 	    ISSET(sc->sc_msr, MCR_CTS) ? "+" : "-",
    368        1.1       leo 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
    369        1.1       leo 	    ISSET(sc->sc_mcr, MCR_RTS) ? "+" : "-",
    370        1.1       leo 	    sc->sc_rx_blocked ? "+" : "-");
    371        1.1       leo }
    372        1.1       leo #endif /* SER_DEBUG */
    373        1.1       leo 
    374        1.1       leo int
    375       1.42       dsl seropen(dev_t dev, int flag, int mode, struct lwp *l)
    376        1.1       leo {
    377        1.1       leo 	int unit = SERUNIT(dev);
    378        1.1       leo 	struct ser_softc *sc;
    379        1.1       leo 	struct tty *tp;
    380        1.1       leo 	int s, s2;
    381        1.1       leo 	int error = 0;
    382        1.1       leo 
    383       1.39   tsutsui 	sc = device_lookup_private(&ser_cd, unit);
    384       1.39   tsutsui 	if (sc == NULL)
    385       1.46   tsutsui 		return ENXIO;
    386        1.1       leo 
    387        1.1       leo 	if (!sc->sc_tty) {
    388       1.52     rmind 		tp = sc->sc_tty = tty_alloc();
    389        1.1       leo 		tty_attach(tp);
    390        1.1       leo 	} else
    391        1.1       leo 		tp = sc->sc_tty;
    392        1.1       leo 
    393       1.31      elad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    394       1.46   tsutsui 		return EBUSY;
    395        1.1       leo 
    396        1.1       leo 	s = spltty();
    397        1.1       leo 
    398        1.1       leo 	/*
    399        1.1       leo 	 * Do the following if this is a first open.
    400        1.1       leo 	 */
    401        1.4       leo 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    402       1.46   tsutsui 		struct termios t;
    403        1.1       leo 
    404       1.46   tsutsui 		/* Turn on interrupts. */
    405       1.46   tsutsui 		sc->sc_imra = IA_RRDY|IA_RERR|IA_TRDY|IA_TERR;
    406       1.46   tsutsui 		sc->sc_imrb = IB_SCTS|IB_SDCD;
    407       1.46   tsutsui 		single_inst_bset_b(MFP->mf_imra, sc->sc_imra);
    408       1.46   tsutsui 		single_inst_bset_b(MFP->mf_imrb, sc->sc_imrb);
    409       1.46   tsutsui 
    410       1.46   tsutsui 		/* Fetch the current modem control status, needed later. */
    411       1.46   tsutsui 		sc->sc_msr = ~MFP->mf_gpip & (IO_SDCD|IO_SCTS|IO_SRI);
    412       1.46   tsutsui 
    413       1.46   tsutsui 		/* Add some entry points needed by the tty layer. */
    414       1.46   tsutsui 		tp->t_oproc = serstart;
    415       1.46   tsutsui 		tp->t_param = serparam;
    416       1.46   tsutsui 		tp->t_hwiflow = serhwiflow;
    417       1.46   tsutsui 		tp->t_dev = dev;
    418       1.46   tsutsui 
    419       1.46   tsutsui 		/*
    420       1.46   tsutsui 		 * Initialize the termios status to the defaults.  Add in the
    421       1.46   tsutsui 		 * sticky bits from TIOCSFLAGS.
    422       1.46   tsutsui 		 */
    423       1.46   tsutsui 		t.c_ispeed = 0;
    424       1.46   tsutsui 		if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
    425       1.46   tsutsui 			t.c_ospeed = CONSBAUD;
    426       1.46   tsutsui 			t.c_cflag  = CONSCFLAG;
    427       1.46   tsutsui 		} else {
    428       1.46   tsutsui 			t.c_ospeed = TTYDEF_SPEED;
    429       1.46   tsutsui 			t.c_cflag = TTYDEF_CFLAG;
    430       1.46   tsutsui 		}
    431       1.46   tsutsui 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    432       1.46   tsutsui 			SET(t.c_cflag, CLOCAL);
    433       1.46   tsutsui 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    434       1.46   tsutsui 			SET(t.c_cflag, CRTSCTS);
    435       1.46   tsutsui 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    436       1.46   tsutsui 			SET(t.c_cflag, MDMBUF);
    437       1.46   tsutsui 		tp->t_iflag = TTYDEF_IFLAG;
    438       1.46   tsutsui 		tp->t_oflag = TTYDEF_OFLAG;
    439       1.46   tsutsui 		tp->t_lflag = TTYDEF_LFLAG;
    440       1.46   tsutsui 		ttychars(tp);
    441       1.46   tsutsui 		(void)serparam(tp, &t);
    442       1.46   tsutsui 		ttsetwater(tp);
    443       1.46   tsutsui 
    444       1.46   tsutsui 		s2 = splhigh();
    445       1.46   tsutsui 
    446       1.46   tsutsui 		/*
    447       1.46   tsutsui 		 * Turn on DTR.  We must always do this, even if carrier is not
    448       1.46   tsutsui 		 * present, because otherwise we'd have to use TIOCSDTR
    449       1.46   tsutsui 		 * immediately after setting CLOCAL.  We will drop DTR only on
    450       1.46   tsutsui 		 * the next high-low transition of DCD, or by explicit request.
    451       1.46   tsutsui 		 */
    452       1.46   tsutsui 		ser_modem(sc, 1);
    453       1.46   tsutsui 
    454       1.46   tsutsui 		/* Clear the input ring, and unblock. */
    455       1.46   tsutsui 		sc->sc_rbput = sc->sc_rbget = 0;
    456       1.46   tsutsui 		sc->sc_rbavail = RXBUFSIZE;
    457       1.46   tsutsui 		ser_iflush(sc);
    458       1.46   tsutsui 		sc->sc_rx_blocked = 0;
    459       1.46   tsutsui 		ser_hwiflow(sc, 0);
    460        1.1       leo 
    461        1.1       leo #ifdef SER_DEBUG
    462       1.46   tsutsui 		serstatus(sc, "seropen  ");
    463        1.1       leo #endif
    464        1.1       leo 
    465       1.46   tsutsui 		splx(s2);
    466        1.1       leo 	}
    467        1.1       leo 
    468        1.4       leo 	splx(s);
    469        1.4       leo 
    470        1.4       leo 	error = ttyopen(tp, SERDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    471        1.4       leo 	if (error)
    472        1.4       leo 		goto bad;
    473        1.4       leo 
    474       1.12       eeh 	error = (*tp->t_linesw->l_open)(dev, tp);
    475        1.4       leo         if (error)
    476        1.4       leo 		goto bad;
    477        1.4       leo 
    478       1.46   tsutsui 	return 0;
    479        1.4       leo 
    480        1.4       leo bad:
    481        1.4       leo 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    482        1.4       leo 		/*
    483        1.4       leo 		 * We failed to open the device, and nobody else had it opened.
    484        1.4       leo 		 * Clean up the state as appropriate.
    485        1.4       leo 		 */
    486        1.4       leo 		ser_shutdown(sc);
    487        1.4       leo 	}
    488        1.1       leo 
    489       1.46   tsutsui 	return error;
    490        1.1       leo }
    491        1.1       leo 
    492        1.1       leo int
    493       1.42       dsl serclose(dev_t dev, int flag, int mode, struct lwp *l)
    494        1.1       leo {
    495        1.1       leo 	int unit = SERUNIT(dev);
    496       1.39   tsutsui 	struct ser_softc *sc = device_lookup_private(&ser_cd, unit);
    497        1.1       leo 	struct tty *tp = sc->sc_tty;
    498        1.1       leo 
    499        1.1       leo 	/* XXX This is for cons.c. */
    500        1.1       leo 	if (!ISSET(tp->t_state, TS_ISOPEN))
    501       1.46   tsutsui 		return 0;
    502        1.1       leo 
    503       1.12       eeh 	(*tp->t_linesw->l_close)(tp, flag);
    504        1.1       leo 	ttyclose(tp);
    505        1.1       leo 
    506        1.4       leo 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    507        1.4       leo 		/*
    508        1.4       leo 		 * Although we got a last close, the device may still be in
    509        1.4       leo 		 * use; e.g. if this was the dialout node, and there are still
    510        1.4       leo 		 * processes waiting for carrier on the non-dialout node.
    511        1.4       leo 		 */
    512        1.4       leo 		ser_shutdown(sc);
    513        1.1       leo 	}
    514        1.1       leo 
    515       1.46   tsutsui 	return 0;
    516        1.1       leo }
    517        1.1       leo 
    518        1.1       leo int
    519       1.41       dsl serread(dev_t dev, struct uio *uio, int flag)
    520        1.1       leo {
    521       1.39   tsutsui 	struct ser_softc *sc = device_lookup_private(&ser_cd, SERUNIT(dev));
    522        1.1       leo 	struct tty *tp = sc->sc_tty;
    523        1.1       leo 
    524       1.46   tsutsui 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    525        1.1       leo }
    526        1.1       leo 
    527        1.1       leo int
    528       1.41       dsl serwrite(dev_t dev, struct uio *uio, int flag)
    529        1.1       leo {
    530       1.39   tsutsui 	struct ser_softc *sc = device_lookup_private(&ser_cd, SERUNIT(dev));
    531        1.1       leo 	struct tty *tp = sc->sc_tty;
    532        1.1       leo 
    533       1.46   tsutsui 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    534       1.14       scw }
    535       1.14       scw 
    536       1.14       scw int
    537       1.41       dsl serpoll(dev_t dev, int events, struct lwp *l)
    538       1.14       scw {
    539       1.39   tsutsui 	struct ser_softc *sc = device_lookup_private(&ser_cd, SERUNIT(dev));
    540       1.14       scw 	struct tty *tp = sc->sc_tty;
    541       1.14       scw 
    542       1.46   tsutsui 	return (*tp->t_linesw->l_poll)(tp, events, l);
    543        1.1       leo }
    544        1.1       leo 
    545        1.1       leo struct tty *
    546       1.41       dsl sertty(dev_t dev)
    547        1.1       leo {
    548       1.39   tsutsui 	struct ser_softc *sc = device_lookup_private(&ser_cd, SERUNIT(dev));
    549        1.1       leo 	struct tty *tp = sc->sc_tty;
    550        1.1       leo 
    551       1.46   tsutsui 	return tp;
    552        1.1       leo }
    553        1.1       leo 
    554        1.1       leo int
    555       1.41       dsl serioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    556        1.1       leo {
    557        1.1       leo 	int unit = SERUNIT(dev);
    558       1.39   tsutsui 	struct ser_softc *sc = device_lookup_private(&ser_cd, unit);
    559        1.1       leo 	struct tty *tp = sc->sc_tty;
    560        1.1       leo 	int error;
    561        1.1       leo 
    562       1.27  christos 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    563       1.15    atatat 	if (error != EPASSTHROUGH)
    564       1.46   tsutsui 		return error;
    565        1.1       leo 
    566       1.27  christos 	error = ttioctl(tp, cmd, data, flag, l);
    567       1.15    atatat 	if (error != EPASSTHROUGH)
    568       1.46   tsutsui 		return error;
    569        1.1       leo 
    570        1.1       leo 	switch (cmd) {
    571        1.1       leo 	case TIOCSBRK:
    572        1.1       leo 		ser_break(sc, 1);
    573        1.1       leo 		break;
    574        1.1       leo 
    575        1.1       leo 	case TIOCCBRK:
    576        1.1       leo 		ser_break(sc, 0);
    577        1.1       leo 		break;
    578        1.1       leo 
    579        1.1       leo 	case TIOCSDTR:
    580        1.1       leo 		ser_modem(sc, 1);
    581        1.1       leo 		break;
    582        1.1       leo 
    583        1.1       leo 	case TIOCCDTR:
    584        1.1       leo 		ser_modem(sc, 0);
    585        1.1       leo 		break;
    586        1.1       leo 
    587        1.1       leo 	case TIOCGFLAGS:
    588        1.1       leo 		*(int *)data = sc->sc_swflags;
    589        1.1       leo 		break;
    590        1.1       leo 
    591        1.1       leo 	case TIOCSFLAGS:
    592       1.32      elad 		error = kauth_authorize_device_tty(l->l_cred,
    593       1.32      elad 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    594        1.1       leo 		if (error)
    595       1.46   tsutsui 			return error;
    596        1.1       leo 		sc->sc_swflags = *(int *)data;
    597        1.1       leo 		break;
    598        1.1       leo 
    599        1.1       leo 	case TIOCMSET:
    600        1.1       leo 	case TIOCMBIS:
    601        1.1       leo 	case TIOCMBIC:
    602        1.1       leo 	case TIOCMGET:
    603        1.1       leo 	default:
    604       1.46   tsutsui 		return EPASSTHROUGH;
    605        1.1       leo 	}
    606        1.1       leo 
    607        1.1       leo #ifdef SER_DEBUG
    608       1.46   tsutsui 	serstatus(sc, "serioctl ");
    609        1.1       leo #endif
    610        1.1       leo 
    611       1.46   tsutsui 	return 0;
    612        1.1       leo }
    613        1.1       leo 
    614        1.1       leo void
    615       1.41       dsl ser_break(struct ser_softc *sc, int onoff)
    616        1.1       leo {
    617        1.1       leo 	int s;
    618        1.1       leo 
    619       1.37        ad 	s = splhigh();
    620        1.1       leo 	if (onoff)
    621        1.1       leo 		SET(sc->sc_tsr, TSR_SBREAK);
    622        1.1       leo 	else
    623        1.1       leo 		CLR(sc->sc_tsr, TSR_SBREAK);
    624        1.1       leo 
    625        1.1       leo 	if (!sc->sc_heldchange) {
    626        1.1       leo 		if (sc->sc_tx_busy) {
    627        1.1       leo 			sc->sc_heldtbc = sc->sc_tbc;
    628        1.1       leo 			sc->sc_tbc = 0;
    629        1.1       leo 			sc->sc_heldchange = 1;
    630        1.1       leo 		} else
    631        1.1       leo 			ser_loadchannelregs(sc);
    632        1.1       leo 	}
    633        1.1       leo 	splx(s);
    634        1.1       leo }
    635        1.1       leo 
    636        1.1       leo void
    637       1.41       dsl ser_modem(struct ser_softc *sc, int onoff)
    638        1.1       leo {
    639        1.1       leo 	int s;
    640        1.1       leo 
    641       1.37        ad 	s = splhigh();
    642        1.1       leo 	if (onoff)
    643        1.1       leo 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
    644        1.1       leo 	else
    645        1.1       leo 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
    646        1.1       leo 
    647        1.1       leo 	if (!sc->sc_heldchange) {
    648        1.1       leo 		if (sc->sc_tx_busy) {
    649        1.1       leo 			sc->sc_heldtbc = sc->sc_tbc;
    650        1.1       leo 			sc->sc_tbc = 0;
    651        1.1       leo 			sc->sc_heldchange = 1;
    652        1.1       leo 		} else
    653        1.1       leo 			ser_loadchannelregs(sc);
    654        1.1       leo 	}
    655        1.1       leo 	splx(s);
    656        1.1       leo }
    657        1.1       leo 
    658        1.1       leo int
    659       1.41       dsl serparam(struct tty *tp, struct termios *t)
    660        1.1       leo {
    661       1.39   tsutsui 	struct ser_softc *sc =
    662       1.39   tsutsui 	    device_lookup_private(&ser_cd, SERUNIT(tp->t_dev));
    663        1.1       leo 	int ospeed = serspeed(t->c_ospeed);
    664       1.46   tsutsui 	uint8_t ucr;
    665        1.1       leo 	int s;
    666        1.1       leo 
    667       1.39   tsutsui 
    668        1.1       leo 	/* check requested parameters */
    669        1.1       leo 	if (ospeed < 0)
    670       1.46   tsutsui 		return EINVAL;
    671        1.1       leo 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    672       1.46   tsutsui 		return EINVAL;
    673        1.1       leo 
    674        1.1       leo 	sc->sc_rsr = RSR_ENAB;
    675        1.1       leo 	sc->sc_tsr = TSR_ENAB;
    676        1.1       leo 
    677        1.1       leo 	ucr = UCR_CLKDIV;
    678        1.1       leo 
    679        1.1       leo 	switch (ISSET(t->c_cflag, CSIZE)) {
    680        1.1       leo 	case CS5:
    681        1.1       leo 		SET(ucr, UCR_5BITS);
    682        1.1       leo 		break;
    683        1.1       leo 	case CS6:
    684        1.1       leo 		SET(ucr, UCR_6BITS);
    685        1.1       leo 		break;
    686        1.1       leo 	case CS7:
    687        1.1       leo 		SET(ucr, UCR_7BITS);
    688        1.1       leo 		break;
    689        1.1       leo 	case CS8:
    690        1.1       leo 		SET(ucr, UCR_8BITS);
    691        1.1       leo 		break;
    692        1.1       leo 	}
    693        1.1       leo 	if (ISSET(t->c_cflag, PARENB)) {
    694        1.1       leo 		SET(ucr, UCR_PENAB);
    695        1.1       leo 		if (!ISSET(t->c_cflag, PARODD))
    696        1.1       leo 			SET(ucr, UCR_PEVEN);
    697        1.1       leo 	}
    698        1.1       leo 	if (ISSET(t->c_cflag, CSTOPB))
    699        1.1       leo 		SET(ucr, UCR_STOPB2);
    700        1.1       leo 	else
    701        1.1       leo 		SET(ucr, UCR_STOPB1);
    702        1.1       leo 
    703       1.37        ad 	s = splhigh();
    704        1.1       leo 
    705        1.1       leo 	sc->sc_ucr = ucr;
    706        1.1       leo 
    707        1.1       leo 	/*
    708        1.1       leo 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    709        1.1       leo 	 * is always active.
    710        1.1       leo 	 */
    711        1.1       leo 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    712        1.1       leo 	    ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
    713        1.1       leo 		SET(t->c_cflag, CLOCAL);
    714        1.1       leo 		CLR(t->c_cflag, HUPCL);
    715        1.1       leo 	}
    716        1.1       leo 
    717        1.1       leo 	/*
    718        1.1       leo 	 * If we're not in a mode that assumes a connection is present, then
    719        1.1       leo 	 * ignore carrier changes.
    720        1.1       leo 	 */
    721        1.1       leo 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
    722        1.1       leo 		sc->sc_msr_dcd = 0;
    723        1.1       leo 	else
    724        1.1       leo 		sc->sc_msr_dcd = MCR_DCD;
    725        1.1       leo 	/*
    726        1.1       leo 	 * Set the flow control pins depending on the current flow control
    727        1.1       leo 	 * mode.
    728        1.1       leo 	 */
    729        1.1       leo 	if (ISSET(t->c_cflag, CRTSCTS)) {
    730        1.1       leo 		sc->sc_mcr_dtr = MCR_DTR;
    731        1.1       leo 		sc->sc_mcr_rts = MCR_RTS;
    732        1.1       leo 		sc->sc_msr_cts = MCR_CTS;
    733        1.1       leo 		sc->sc_r_hiwat = RXHIWAT;
    734        1.1       leo 	} else if (ISSET(t->c_cflag, MDMBUF)) {
    735        1.1       leo 		/*
    736        1.1       leo 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
    737        1.1       leo 		 * carrier detection.
    738        1.1       leo 		 */
    739        1.1       leo 		sc->sc_mcr_dtr = 0;
    740        1.1       leo 		sc->sc_mcr_rts = MCR_DTR;
    741        1.1       leo 		sc->sc_msr_cts = MCR_DCD;
    742        1.1       leo 		sc->sc_r_hiwat = RXHIWAT;
    743        1.1       leo 	} else {
    744        1.1       leo 		/*
    745        1.1       leo 		 * If no flow control, then always set RTS.  This will make
    746        1.1       leo 		 * the other side happy if it mistakenly thinks we're doing
    747        1.1       leo 		 * RTS/CTS flow control.
    748        1.1       leo 		 */
    749        1.1       leo 		sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
    750        1.1       leo 		sc->sc_mcr_rts = 0;
    751        1.1       leo 		sc->sc_msr_cts = 0;
    752        1.1       leo 		sc->sc_r_hiwat = 0;
    753        1.1       leo 		if (ISSET(sc->sc_mcr, MCR_DTR))
    754        1.1       leo 			SET(sc->sc_mcr, MCR_RTS);
    755        1.1       leo 		else
    756        1.1       leo 			CLR(sc->sc_mcr, MCR_RTS);
    757        1.1       leo 	}
    758        1.1       leo 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
    759        1.1       leo 
    760        1.1       leo #if 0
    761        1.1       leo 	if (ospeed == 0)
    762        1.1       leo 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
    763        1.1       leo 	else
    764        1.1       leo 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
    765        1.1       leo #endif
    766        1.1       leo 
    767        1.1       leo 	sc->sc_ospeed = ospeed;
    768        1.1       leo 
    769        1.1       leo 	/* and copy to tty */
    770        1.1       leo 	tp->t_ispeed = 0;
    771        1.1       leo 	tp->t_ospeed = t->c_ospeed;
    772        1.1       leo 	tp->t_cflag = t->c_cflag;
    773        1.1       leo 
    774        1.1       leo 	if (!sc->sc_heldchange) {
    775        1.1       leo 		if (sc->sc_tx_busy) {
    776        1.1       leo 			sc->sc_heldtbc = sc->sc_tbc;
    777        1.1       leo 			sc->sc_tbc = 0;
    778        1.1       leo 			sc->sc_heldchange = 1;
    779        1.1       leo 		} else
    780        1.1       leo 			ser_loadchannelregs(sc);
    781        1.1       leo 	}
    782        1.1       leo 
    783        1.1       leo 	splx(s);
    784        1.1       leo 
    785        1.1       leo 	/*
    786        1.1       leo 	 * Update the tty layer's idea of the carrier bit, in case we changed
    787        1.1       leo 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that if we
    788        1.1       leo 	 * lose carrier while carrier detection is on.
    789        1.1       leo 	 */
    790       1.46   tsutsui 	(void)(*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MCR_DCD));
    791        1.1       leo 
    792        1.1       leo #ifdef SER_DEBUG
    793        1.1       leo 	serstatus(sc, "serparam ");
    794        1.1       leo #endif
    795        1.1       leo 
    796        1.1       leo 	/* XXXXX FIX ME */
    797        1.1       leo 	/* Block or unblock as needed. */
    798        1.1       leo 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    799        1.1       leo 		if (sc->sc_rx_blocked) {
    800        1.1       leo 			sc->sc_rx_blocked = 0;
    801        1.1       leo 			ser_hwiflow(sc, 0);
    802        1.1       leo 		}
    803        1.1       leo 		if (sc->sc_tx_stopped) {
    804        1.1       leo 			sc->sc_tx_stopped = 0;
    805        1.1       leo 			serstart(tp);
    806        1.1       leo 		}
    807        1.1       leo 	} else {
    808        1.1       leo #if 0
    809        1.1       leo 		sermsrint(sc, tp);
    810        1.1       leo #endif
    811        1.1       leo 	}
    812        1.1       leo 
    813       1.46   tsutsui 	return 0;
    814        1.1       leo }
    815        1.1       leo 
    816        1.1       leo void
    817       1.41       dsl ser_iflush(struct ser_softc *sc)
    818        1.1       leo {
    819       1.46   tsutsui 	uint8_t tmp;
    820        1.1       leo 
    821        1.1       leo 	/* flush any pending I/O */
    822        1.1       leo 	while (ISSET(MFP->mf_rsr, RSR_CIP|RSR_BFULL))
    823        1.1       leo 		tmp = MFP->mf_udr;
    824  1.52.14.1       tls 	__USE(tmp);
    825        1.1       leo }
    826        1.1       leo 
    827        1.1       leo void
    828       1.41       dsl ser_loadchannelregs(struct ser_softc *sc)
    829        1.1       leo {
    830       1.46   tsutsui 
    831        1.1       leo 	/* XXXXX necessary? */
    832        1.1       leo 	ser_iflush(sc);
    833        1.1       leo 
    834        1.1       leo 	/*
    835        1.1       leo 	 * No interrupts please...
    836        1.1       leo 	 */
    837       1.46   tsutsui 	if ((MFP->mf_imra & (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR)) != sc->sc_imra) {
    838       1.46   tsutsui 		printf("loadchannelregs: mf_imra: %x sc_imra: %x\n",
    839       1.46   tsutsui 		    (u_int)MFP->mf_imra, (u_int)sc->sc_imra);
    840       1.46   tsutsui 	}
    841       1.46   tsutsui 	if ((MFP->mf_imrb & (IB_SCTS|IB_SDCD)) != sc->sc_imrb) {
    842       1.46   tsutsui 		printf("loadchannelregs: mf_imrb: %x sc_imrb: %x\n",
    843       1.46   tsutsui 		     (u_int)MFP->mf_imrb, (u_int)sc->sc_imrb);
    844       1.46   tsutsui 	}
    845        1.1       leo 	single_inst_bclr_b(MFP->mf_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
    846        1.1       leo 	single_inst_bclr_b(MFP->mf_imrb, IB_SCTS|IB_SDCD);
    847        1.1       leo 
    848        1.1       leo 	MFP->mf_ucr = sc->sc_ucr;
    849        1.1       leo 	MFP->mf_rsr = sc->sc_rsr;
    850        1.1       leo 	MFP->mf_tsr = sc->sc_tsr;
    851        1.1       leo 
    852        1.1       leo 	single_inst_bclr_b(MFP->mf_tcdcr, 0x07);
    853        1.1       leo 	MFP->mf_tddr  = sc->sc_ospeed;
    854        1.1       leo 	single_inst_bset_b(MFP->mf_tcdcr, (sc->sc_ospeed >> 8) & 0x0f);
    855        1.1       leo 
    856        1.1       leo 	sc->sc_mcr_active = sc->sc_mcr;
    857        1.2       leo 
    858        1.2       leo 	if (machineid & ATARI_HADES) {
    859        1.2       leo 		/* PCB fault, wires exchanged..... */
    860        1.2       leo 		ym2149_rts(!(sc->sc_mcr_active & MCR_DTR));
    861        1.2       leo 		ym2149_dtr(!(sc->sc_mcr_active & MCR_RTS));
    862       1.46   tsutsui 	} else {
    863        1.2       leo 		ym2149_rts(!(sc->sc_mcr_active & MCR_RTS));
    864        1.2       leo 		ym2149_dtr(!(sc->sc_mcr_active & MCR_DTR));
    865        1.2       leo 	}
    866        1.1       leo 
    867        1.1       leo 	single_inst_bset_b(MFP->mf_imra, sc->sc_imra);
    868        1.1       leo 	single_inst_bset_b(MFP->mf_imrb, sc->sc_imrb);
    869        1.1       leo }
    870        1.1       leo 
    871        1.1       leo int
    872       1.41       dsl serhwiflow(struct tty *tp, int block)
    873        1.1       leo {
    874       1.39   tsutsui 	struct ser_softc *sc =
    875       1.39   tsutsui 	    device_lookup_private(&ser_cd, SERUNIT(tp->t_dev));
    876        1.1       leo 	int s;
    877        1.1       leo 
    878        1.1       leo 	if (sc->sc_mcr_rts == 0)
    879       1.46   tsutsui 		return 0;
    880        1.1       leo 
    881       1.37        ad 	s = splhigh();
    882        1.1       leo 	if (block) {
    883        1.1       leo 		/*
    884        1.1       leo 		 * The tty layer is asking us to block input.
    885        1.1       leo 		 * If we already did it, just return TRUE.
    886        1.1       leo 		 */
    887        1.1       leo 		if (sc->sc_rx_blocked)
    888        1.1       leo 			goto out;
    889        1.1       leo 		sc->sc_rx_blocked = 1;
    890        1.1       leo 	} else {
    891        1.1       leo 		/*
    892        1.1       leo 		 * The tty layer is asking us to resume input.
    893        1.1       leo 		 * The input ring is always empty by now.
    894        1.1       leo 		 */
    895        1.1       leo 		sc->sc_rx_blocked = 0;
    896        1.1       leo 	}
    897        1.1       leo 	ser_hwiflow(sc, block);
    898        1.1       leo out:
    899        1.1       leo 	splx(s);
    900       1.46   tsutsui 	return 1;
    901        1.1       leo }
    902        1.1       leo 
    903        1.1       leo /*
    904        1.1       leo  * (un)block input via hw flowcontrol
    905        1.1       leo  */
    906        1.1       leo void
    907       1.41       dsl ser_hwiflow(struct ser_softc *sc, int block)
    908        1.1       leo {
    909       1.46   tsutsui 
    910        1.1       leo 	if (sc->sc_mcr_rts == 0)
    911        1.1       leo 		return;
    912        1.1       leo 
    913        1.1       leo 	if (block) {
    914        1.1       leo 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
    915        1.1       leo 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
    916        1.1       leo 	} else {
    917        1.1       leo 		SET(sc->sc_mcr, sc->sc_mcr_rts);
    918        1.1       leo 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
    919        1.1       leo 	}
    920        1.2       leo 	if (machineid & ATARI_HADES) {
    921        1.2       leo 		/* PCB fault, wires exchanged..... */
    922        1.2       leo 		ym2149_dtr(sc->sc_mcr_active & MCR_RTS);
    923        1.2       leo 	}
    924        1.2       leo 	else {
    925        1.2       leo 		ym2149_rts(sc->sc_mcr_active & MCR_RTS);
    926        1.2       leo 	}
    927        1.1       leo }
    928        1.1       leo 
    929        1.1       leo void
    930       1.41       dsl serstart(struct tty *tp)
    931        1.1       leo {
    932       1.39   tsutsui 	struct ser_softc *sc =
    933       1.39   tsutsui 	    device_lookup_private(&ser_cd, SERUNIT(tp->t_dev));
    934        1.1       leo 	int s;
    935        1.1       leo 
    936        1.1       leo 	s = spltty();
    937        1.1       leo 	if (ISSET(tp->t_state, TS_BUSY))
    938        1.1       leo 		goto out;
    939        1.1       leo 	if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP))
    940        1.1       leo 		goto stopped;
    941        1.1       leo 
    942        1.1       leo 	if (sc->sc_tx_stopped)
    943        1.1       leo 		goto stopped;
    944       1.36        ad 	if (!ttypull(tp))
    945       1.36        ad 		goto stopped;
    946        1.1       leo 
    947        1.1       leo 	/* Grab the first contiguous region of buffer space. */
    948        1.1       leo 	{
    949       1.46   tsutsui 		uint8_t *tba;
    950        1.1       leo 		int tbc;
    951        1.1       leo 
    952        1.1       leo 		tba = tp->t_outq.c_cf;
    953        1.1       leo 		tbc = ndqb(&tp->t_outq, 0);
    954        1.1       leo 
    955       1.37        ad 		(void)splhigh();
    956        1.1       leo 
    957        1.1       leo 		sc->sc_tba = tba;
    958        1.1       leo 		sc->sc_tbc = tbc;
    959        1.1       leo 	}
    960        1.1       leo 
    961        1.1       leo 	SET(tp->t_state, TS_BUSY);
    962        1.1       leo 	sc->sc_tx_busy = 1;
    963        1.1       leo 
    964        1.1       leo 	/* Enable transmit completion interrupts if necessary. */
    965        1.1       leo 	if (!ISSET(sc->sc_imra, IA_TRDY)) {
    966        1.1       leo 		SET(sc->sc_imra, IA_TRDY|IA_TERR);
    967        1.1       leo 		single_inst_bset_b(MFP->mf_imra, IA_TRDY|IA_TERR);
    968        1.1       leo 	}
    969        1.1       leo 
    970        1.1       leo 	/* Output the first char */
    971        1.1       leo 	MFP->mf_udr = *sc->sc_tba;
    972       1.24    simonb 	sc->sc_tbc--;
    973       1.24    simonb 	sc->sc_tba++;
    974        1.1       leo 
    975        1.1       leo 	splx(s);
    976        1.1       leo 	return;
    977        1.1       leo 
    978        1.1       leo stopped:
    979        1.1       leo 	/* Disable transmit completion interrupts if necessary. */
    980        1.1       leo 	if (ISSET(sc->sc_imra, IA_TRDY)) {
    981        1.1       leo 		CLR(sc->sc_imra, IA_TRDY|IA_TERR);
    982        1.1       leo 		single_inst_bclr_b(MFP->mf_imra, IA_TRDY|IA_TERR);
    983        1.1       leo 	}
    984        1.1       leo out:
    985        1.1       leo 	splx(s);
    986        1.1       leo 	return;
    987        1.1       leo }
    988        1.1       leo 
    989        1.1       leo /*
    990        1.1       leo  * Stop output on a line.
    991        1.1       leo  */
    992        1.1       leo void
    993       1.41       dsl serstop(struct tty *tp, int flag)
    994        1.1       leo {
    995       1.39   tsutsui 	struct ser_softc *sc =
    996       1.39   tsutsui 	    device_lookup_private(&ser_cd, SERUNIT(tp->t_dev));
    997        1.1       leo 	int s;
    998        1.1       leo 
    999       1.37        ad 	s = splhigh();
   1000        1.1       leo 	if (ISSET(tp->t_state, TS_BUSY)) {
   1001        1.1       leo 		/* Stop transmitting at the next chunk. */
   1002        1.1       leo 		sc->sc_tbc = 0;
   1003        1.1       leo 		sc->sc_heldtbc = 0;
   1004        1.1       leo 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1005        1.1       leo 			SET(tp->t_state, TS_FLUSH);
   1006        1.1       leo 	}
   1007        1.1       leo 	splx(s);
   1008        1.1       leo }
   1009        1.1       leo 
   1010        1.1       leo void
   1011       1.41       dsl serdiag(void *arg)
   1012        1.1       leo {
   1013        1.1       leo 	struct ser_softc *sc = arg;
   1014        1.1       leo 	int overflows, floods;
   1015        1.1       leo 	int s;
   1016        1.1       leo 
   1017       1.37        ad 	s = splhigh();
   1018        1.1       leo 	overflows = sc->sc_overflows;
   1019        1.1       leo 	sc->sc_overflows = 0;
   1020        1.1       leo 	floods = sc->sc_floods;
   1021        1.1       leo 	sc->sc_floods = 0;
   1022        1.1       leo 	sc->sc_errors = 0;
   1023        1.1       leo 	splx(s);
   1024        1.1       leo 
   1025        1.1       leo 	log(LOG_WARNING,
   1026        1.1       leo 	    "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1027       1.48   tsutsui 	    device_xname(sc->sc_dev),
   1028        1.1       leo 	    overflows, overflows == 1 ? "" : "s",
   1029        1.1       leo 	    floods, floods == 1 ? "" : "s");
   1030        1.1       leo }
   1031        1.1       leo 
   1032       1.46   tsutsui static void
   1033       1.46   tsutsui ser_shutdown(struct ser_softc *sc)
   1034        1.4       leo {
   1035       1.46   tsutsui 	int s;
   1036        1.4       leo 	struct tty *tp = sc->sc_tty;
   1037        1.4       leo 
   1038        1.4       leo 
   1039       1.37        ad 	s = splhigh();
   1040        1.4       leo 
   1041        1.4       leo 	/* If we were asserting flow control, then deassert it. */
   1042        1.4       leo 	sc->sc_rx_blocked = 1;
   1043        1.4       leo 	ser_hwiflow(sc, 1);
   1044        1.4       leo 
   1045        1.4       leo 	/* Clear any break condition set with TIOCSBRK. */
   1046        1.4       leo 	ser_break(sc, 0);
   1047        1.4       leo 
   1048        1.4       leo 	/*
   1049        1.4       leo 	 * Hang up if necessary.  Wait a bit, so the other side has time to
   1050        1.4       leo 	 * notice even if we immediately open the port again.
   1051        1.4       leo 	 */
   1052        1.4       leo 	if (ISSET(tp->t_cflag, HUPCL)) {
   1053        1.4       leo 		ser_modem(sc, 0);
   1054       1.46   tsutsui 		(void)tsleep(sc, TTIPRI, ttclos, hz);
   1055        1.4       leo 	}
   1056        1.4       leo 
   1057        1.4       leo 	/* Turn off interrupts. */
   1058        1.4       leo 	CLR(sc->sc_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
   1059        1.4       leo 	CLR(sc->sc_imrb, IB_SCTS|IB_SDCD);
   1060        1.4       leo 	single_inst_bclr_b(MFP->mf_imrb, IB_SCTS|IB_SDCD);
   1061        1.4       leo 	single_inst_bclr_b(MFP->mf_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
   1062        1.4       leo 	splx(s);
   1063        1.4       leo }
   1064        1.4       leo 
   1065        1.1       leo static void
   1066       1.41       dsl serrxint(struct ser_softc *sc, struct tty *tp)
   1067        1.1       leo {
   1068       1.46   tsutsui 	u_int get, cc, scc;
   1069       1.46   tsutsui 	int code;
   1070       1.46   tsutsui 	uint8_t rsr;
   1071       1.46   tsutsui 	int s;
   1072       1.50   tsutsui 	static const int lsrmap[8] = {
   1073        1.1       leo 		0,      TTY_PE,
   1074        1.1       leo 		TTY_FE, TTY_PE|TTY_FE,
   1075        1.1       leo 		TTY_FE, TTY_PE|TTY_FE,
   1076        1.1       leo 		TTY_FE, TTY_PE|TTY_FE
   1077        1.1       leo 	};
   1078        1.1       leo 
   1079        1.1       leo 	get = sc->sc_rbget;
   1080        1.1       leo 	scc = cc = RXBUFSIZE - sc->sc_rbavail;
   1081        1.1       leo 
   1082        1.1       leo 	if (cc == RXBUFSIZE) {
   1083        1.1       leo 		sc->sc_floods++;
   1084        1.1       leo 		if (sc->sc_errors++ == 0)
   1085       1.10   thorpej 			callout_reset(&sc->sc_diag_ch, 60 * hz, serdiag, sc);
   1086        1.1       leo 	}
   1087        1.1       leo 
   1088        1.1       leo 	while (cc--) {
   1089        1.1       leo 		rsr = sc->sc_lbuf[get];
   1090        1.1       leo 		if (ISSET(rsr, RSR_BREAK)) {
   1091        1.1       leo #ifdef DDB
   1092        1.1       leo 			if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE))
   1093        1.1       leo 				Debugger();
   1094        1.1       leo #endif
   1095       1.46   tsutsui 		} else if (ISSET(rsr, RSR_OERR)) {
   1096        1.1       leo 			sc->sc_overflows++;
   1097        1.1       leo 			if (sc->sc_errors++ == 0)
   1098       1.10   thorpej 				callout_reset(&sc->sc_diag_ch, 60 * hz,
   1099       1.10   thorpej 				    serdiag, sc);
   1100        1.1       leo 		}
   1101        1.1       leo 		code = sc->sc_rbuf[get] |
   1102        1.1       leo 		    lsrmap[(rsr & (RSR_BREAK|RSR_FERR|RSR_PERR)) >> 3];
   1103       1.12       eeh 		(*tp->t_linesw->l_rint)(code, tp);
   1104        1.1       leo 		get = (get + 1) & RXBUFMASK;
   1105        1.1       leo 	}
   1106        1.1       leo 
   1107        1.1       leo 	sc->sc_rbget = get;
   1108       1.37        ad 	s = splhigh();
   1109        1.1       leo 	sc->sc_rbavail += scc;
   1110        1.1       leo 	/*
   1111        1.1       leo 	 * Buffers should be ok again, release possible block, but only if the
   1112        1.1       leo 	 * tty layer isn't blocking too.
   1113        1.1       leo 	 */
   1114        1.1       leo 	if (sc->sc_rx_blocked && !ISSET(tp->t_state, TS_TBLOCK)) {
   1115        1.1       leo 		sc->sc_rx_blocked = 0;
   1116        1.1       leo 		ser_hwiflow(sc, 0);
   1117        1.1       leo 	}
   1118        1.1       leo 	splx(s);
   1119        1.1       leo }
   1120        1.1       leo 
   1121        1.1       leo static void
   1122       1.41       dsl sertxint(struct ser_softc *sc, struct tty *tp)
   1123        1.1       leo {
   1124        1.1       leo 
   1125        1.1       leo 	CLR(tp->t_state, TS_BUSY);
   1126        1.1       leo 	if (ISSET(tp->t_state, TS_FLUSH))
   1127        1.1       leo 		CLR(tp->t_state, TS_FLUSH);
   1128        1.1       leo 	else
   1129        1.1       leo 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1130       1.12       eeh 	(*tp->t_linesw->l_start)(tp);
   1131        1.1       leo }
   1132        1.1       leo 
   1133        1.1       leo static void
   1134       1.41       dsl sermsrint(struct ser_softc *sc, struct tty *tp)
   1135        1.1       leo {
   1136       1.46   tsutsui 	uint8_t msr, delta;
   1137        1.1       leo 	int s;
   1138        1.1       leo 
   1139       1.37        ad 	s = splhigh();
   1140        1.1       leo 	msr = sc->sc_msr;
   1141        1.1       leo 	delta = sc->sc_msr_delta;
   1142        1.1       leo 	sc->sc_msr_delta = 0;
   1143        1.1       leo 	splx(s);
   1144        1.1       leo 
   1145        1.1       leo 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1146        1.1       leo 		/*
   1147        1.1       leo 		 * Inform the tty layer that carrier detect changed.
   1148        1.1       leo 		 */
   1149       1.46   tsutsui 		(void)(*tp->t_linesw->l_modem)(tp, ISSET(msr, MCR_DCD));
   1150        1.1       leo 	}
   1151        1.1       leo 
   1152        1.1       leo 	if (ISSET(delta, sc->sc_msr_cts)) {
   1153        1.1       leo 		/* Block or unblock output according to flow control. */
   1154        1.1       leo 		if (ISSET(msr, sc->sc_msr_cts)) {
   1155        1.1       leo 			sc->sc_tx_stopped = 0;
   1156       1.12       eeh 			(*tp->t_linesw->l_start)(tp);
   1157        1.1       leo 		} else {
   1158        1.1       leo 			sc->sc_tx_stopped = 1;
   1159        1.1       leo 			serstop(tp, 0);
   1160        1.1       leo 		}
   1161        1.1       leo 	}
   1162        1.1       leo 
   1163        1.1       leo #ifdef SER_DEBUG
   1164        1.1       leo 	serstatus(sc, "sermsrint");
   1165        1.1       leo #endif
   1166        1.1       leo }
   1167        1.1       leo 
   1168        1.1       leo void
   1169       1.41       dsl sersoft(void *arg)
   1170        1.1       leo {
   1171        1.1       leo 	struct ser_softc *sc = arg;
   1172       1.46   tsutsui 	struct tty *tp;
   1173        1.1       leo 
   1174        1.1       leo 	tp = sc->sc_tty;
   1175        1.4       leo 	if (tp == NULL)
   1176        1.4       leo 		return;
   1177        1.4       leo 
   1178        1.4       leo 	if (!ISSET(tp->t_state, TS_ISOPEN) && (tp->t_wopen == 0))
   1179        1.1       leo 		return;
   1180        1.1       leo 
   1181        1.1       leo 	if (sc->sc_rx_ready) {
   1182        1.1       leo 		sc->sc_rx_ready = 0;
   1183        1.1       leo 		serrxint(sc, tp);
   1184        1.1       leo 	}
   1185        1.1       leo 
   1186        1.1       leo 	if (sc->sc_st_check) {
   1187        1.1       leo 		sc->sc_st_check = 0;
   1188        1.1       leo 		sermsrint(sc, tp);
   1189        1.1       leo 	}
   1190        1.1       leo 
   1191        1.1       leo 	if (sc->sc_tx_done) {
   1192        1.1       leo 		sc->sc_tx_done = 0;
   1193        1.1       leo 		sertxint(sc, tp);
   1194        1.1       leo 	}
   1195        1.1       leo }
   1196        1.1       leo 
   1197        1.1       leo int
   1198       1.41       dsl sermintr(void *arg)
   1199        1.1       leo {
   1200        1.1       leo 	struct ser_softc *sc = arg;
   1201       1.46   tsutsui 	uint8_t msr, delta;
   1202        1.1       leo 
   1203        1.1       leo 	msr = ~MFP->mf_gpip;
   1204        1.1       leo 	delta = msr ^ sc->sc_msr;
   1205        1.1       leo 	sc->sc_msr  = sc->sc_msr & ~(MCR_CTS|MCR_DCD|MCR_RI);
   1206        1.1       leo 	sc->sc_msr |= msr & (MCR_CTS|MCR_DCD|MCR_RI);
   1207        1.1       leo 
   1208        1.1       leo 	if (ISSET(delta, sc->sc_msr_mask)) {
   1209        1.1       leo 		sc->sc_msr_delta |= delta;
   1210        1.1       leo 
   1211        1.1       leo 		/*
   1212        1.1       leo 		 * Stop output immediately if we lose the output
   1213        1.1       leo 		 * flow control signal or carrier detect.
   1214        1.1       leo 		 */
   1215        1.1       leo 		if (ISSET(~msr, sc->sc_msr_mask)) {
   1216        1.1       leo 			sc->sc_tbc = 0;
   1217        1.1       leo 			sc->sc_heldtbc = 0;
   1218        1.1       leo #ifdef SER_DEBUG
   1219        1.1       leo 			serstatus(sc, "sermintr  ");
   1220        1.1       leo #endif
   1221        1.1       leo 		}
   1222        1.1       leo 
   1223        1.1       leo 		sc->sc_st_check = 1;
   1224        1.1       leo 	}
   1225       1.43   tsutsui 	softint_schedule(sc->sc_sicookie);
   1226        1.1       leo 	return 1;
   1227        1.1       leo }
   1228        1.1       leo 
   1229        1.1       leo int
   1230       1.41       dsl sertrintr(void *arg)
   1231        1.1       leo {
   1232        1.1       leo 	struct ser_softc *sc = arg;
   1233       1.46   tsutsui 	u_int put, cc;
   1234       1.46   tsutsui 	uint8_t rsr, tsr;
   1235        1.1       leo 
   1236        1.1       leo 	put = sc->sc_rbput;
   1237        1.1       leo 	cc = sc->sc_rbavail;
   1238        1.1       leo 
   1239        1.1       leo 	rsr = MFP->mf_rsr;
   1240        1.1       leo 	if (ISSET(rsr, RSR_BFULL|RSR_BREAK)) {
   1241        1.1       leo 		for (; ISSET(rsr, RSR_BFULL|RSR_BREAK) && cc > 0; cc--) {
   1242        1.1       leo 			sc->sc_rbuf[put] = MFP->mf_udr;
   1243        1.1       leo 			sc->sc_lbuf[put] = rsr;
   1244        1.1       leo 			put = (put + 1) & RXBUFMASK;
   1245        1.1       leo 			if ((rsr & RSR_BREAK) && (MFP->mf_rsr & RSR_BREAK))
   1246        1.1       leo 				rsr = 0;
   1247       1.46   tsutsui 			else
   1248       1.46   tsutsui 				rsr = MFP->mf_rsr;
   1249        1.1       leo 		}
   1250        1.1       leo 		/*
   1251        1.1       leo 		 * Current string of incoming characters ended because
   1252        1.1       leo 		 * no more data was available. Schedule a receive event
   1253        1.1       leo 		 * if any data was received. Drop any characters that
   1254        1.1       leo 		 * we couldn't handle.
   1255        1.1       leo 		 */
   1256        1.1       leo 		sc->sc_rbput    = put;
   1257        1.1       leo 		sc->sc_rbavail  = cc;
   1258        1.1       leo 		sc->sc_rx_ready = 1;
   1259        1.1       leo 		/*
   1260        1.1       leo 		 * See if we are in danger of overflowing a buffer. If
   1261        1.1       leo 		 * so, use hardware flow control to ease the pressure.
   1262        1.1       leo 		 */
   1263        1.1       leo 		if (sc->sc_rx_blocked == 0 &&
   1264        1.1       leo 		    cc < sc->sc_r_hiwat) {
   1265        1.1       leo 			sc->sc_rx_blocked = 1;
   1266        1.1       leo 			ser_hwiflow(sc, 1);
   1267        1.1       leo 		}
   1268        1.1       leo 		/*
   1269        1.1       leo 		 * If we're out of space, throw away any further input.
   1270        1.1       leo 		 */
   1271        1.1       leo 		if (!cc) {
   1272        1.1       leo 			while (ISSET(rsr, RSR_BFULL|RSR_BREAK)) {
   1273        1.1       leo 				rsr = MFP->mf_udr;
   1274        1.1       leo 				rsr = MFP->mf_rsr;
   1275        1.1       leo 			}
   1276        1.1       leo 		}
   1277        1.1       leo 	}
   1278        1.1       leo 
   1279        1.1       leo 	/*
   1280        1.1       leo 	 * Done handling any receive interrupts. See if data can be
   1281        1.1       leo 	 * transmitted as well. Schedule tx done event if no data left
   1282        1.1       leo 	 * and tty was marked busy.
   1283        1.1       leo 	 */
   1284        1.1       leo 	tsr = MFP->mf_tsr;
   1285        1.1       leo 	if (ISSET(tsr, TSR_BE)) {
   1286        1.1       leo 		/*
   1287        1.1       leo 		 * If we've delayed a parameter change, do it now, and restart
   1288        1.1       leo 		 * output.
   1289        1.1       leo 		 */
   1290        1.1       leo 		if (sc->sc_heldchange) {
   1291        1.1       leo 			ser_loadchannelregs(sc);
   1292        1.1       leo 			sc->sc_heldchange = 0;
   1293        1.1       leo 			sc->sc_tbc = sc->sc_heldtbc;
   1294        1.1       leo 			sc->sc_heldtbc = 0;
   1295        1.1       leo 		}
   1296        1.1       leo 		/* Output the next character, if any. */
   1297        1.1       leo 		if (sc->sc_tbc > 0) {
   1298        1.1       leo 			MFP->mf_udr = *sc->sc_tba;
   1299       1.24    simonb 			sc->sc_tbc--;
   1300       1.24    simonb 			sc->sc_tba++;
   1301        1.1       leo 		} else if (sc->sc_tx_busy) {
   1302        1.1       leo 			sc->sc_tx_busy = 0;
   1303        1.1       leo 			sc->sc_tx_done = 1;
   1304        1.1       leo 		}
   1305        1.1       leo 	}
   1306       1.43   tsutsui 	softint_schedule(sc->sc_sicookie);
   1307        1.1       leo 	return 1;
   1308        1.1       leo }
   1309        1.1       leo 
   1310        1.1       leo static int
   1311       1.41       dsl serspeed(long speed)
   1312        1.1       leo {
   1313        1.1       leo #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
   1314        1.1       leo 
   1315        1.1       leo 	int div, x, err;
   1316        1.1       leo 
   1317        1.1       leo 	if (speed <= 0)
   1318       1.46   tsutsui 		return -1;
   1319        1.1       leo 
   1320        1.1       leo 	for (div = 4; div <= 64; div *= 4) {
   1321        1.1       leo 		x = divrnd((SER_FREQ / div), speed);
   1322        1.1       leo 
   1323        1.1       leo 		/*
   1324        1.1       leo 		 * The value must fit in the timer-d dataregister. If
   1325        1.1       leo 		 * not, try another delay-mode.
   1326        1.1       leo 		 */
   1327       1.46   tsutsui 		if ((x / 2) > 255)
   1328        1.1       leo 			continue;
   1329        1.1       leo 
   1330        1.1       leo 		/*
   1331        1.1       leo 		 * Baudrate to high for the interface or cannot be made
   1332        1.1       leo 		 * within tolerance.
   1333        1.1       leo 		 */
   1334        1.1       leo 		if (x <= 0)
   1335       1.46   tsutsui 			return -1;
   1336        1.1       leo 
   1337        1.1       leo 		err = divrnd((SER_FREQ / div) * 1000, speed * x) - 1000;
   1338        1.1       leo 		if (err < 0)
   1339        1.1       leo 			err = -err;
   1340        1.1       leo 		if (err > SER_TOLERANCE)
   1341        1.1       leo 			continue;
   1342        1.1       leo 
   1343        1.1       leo 		/*
   1344        1.1       leo 		 * Translate 'div' to delay-code
   1345        1.1       leo 		 */
   1346        1.1       leo 		if (div == 4)
   1347        1.1       leo 			div = 1;
   1348        1.1       leo 		else if (div == 16)
   1349        1.1       leo 			div = 3;
   1350        1.1       leo 		else if (div == 64)
   1351        1.1       leo 			div = 5;
   1352        1.1       leo 
   1353       1.46   tsutsui 		return (x / 2) | (div << 8);
   1354        1.1       leo 	}
   1355       1.46   tsutsui 	return -1;
   1356        1.1       leo 
   1357       1.16   thorpej #undef	divrnd
   1358        1.1       leo }
   1359        1.1       leo 
   1360        1.1       leo /*
   1361        1.1       leo  * Following are all routines needed for SER to act as console
   1362        1.1       leo  */
   1363        1.1       leo #include <dev/cons.h>
   1364        1.1       leo 
   1365        1.1       leo void
   1366       1.41       dsl sercnprobe(struct consdev *cp)
   1367        1.1       leo {
   1368       1.46   tsutsui 
   1369        1.1       leo 	/*
   1370        1.1       leo 	 * Activate serial console when DCD present...
   1371        1.1       leo 	 */
   1372        1.1       leo 	if (MFP->mf_gpip & MCR_DCD) {
   1373        1.1       leo 		cp->cn_pri = CN_DEAD;
   1374        1.1       leo 		return;
   1375        1.1       leo 	}
   1376        1.1       leo 
   1377        1.1       leo 	/* initialize required fields */
   1378       1.18   gehenna 	/* XXX: LWP What unit? */
   1379       1.18   gehenna 	cp->cn_dev = makedev(cdevsw_lookup_major(&ser_cdevsw), 0);
   1380       1.49   tsutsui 	if (serconsole)
   1381       1.49   tsutsui 		cp->cn_pri = CN_REMOTE;	/* Force a serial port console */
   1382       1.49   tsutsui 	else
   1383       1.49   tsutsui 		cp->cn_pri = CN_NORMAL;
   1384        1.1       leo }
   1385        1.1       leo 
   1386        1.1       leo void
   1387       1.41       dsl sercninit(struct consdev *cp)
   1388        1.1       leo {
   1389       1.46   tsutsui 
   1390        1.1       leo 	serinitcons(CONSBAUD);
   1391        1.1       leo }
   1392        1.1       leo 
   1393        1.1       leo /*
   1394        1.1       leo  * Initialize UART to known state.
   1395        1.1       leo  */
   1396        1.1       leo void
   1397       1.41       dsl serinit(int baud)
   1398        1.1       leo {
   1399        1.1       leo 	int ospeed = serspeed(baud);
   1400        1.1       leo 
   1401        1.1       leo 	MFP->mf_ucr = UCR_CLKDIV|UCR_8BITS|UCR_STOPB1;
   1402        1.1       leo 	MFP->mf_rsr = RSR_ENAB;
   1403        1.1       leo 	MFP->mf_tsr = TSR_ENAB;
   1404        1.1       leo 
   1405        1.1       leo 	single_inst_bclr_b(MFP->mf_tcdcr, 0x07);
   1406        1.1       leo 	MFP->mf_tddr  = ospeed;
   1407        1.1       leo 	single_inst_bset_b(MFP->mf_tcdcr, (ospeed >> 8) & 0x0f);
   1408        1.1       leo }
   1409        1.1       leo 
   1410        1.1       leo /*
   1411        1.1       leo  * Set UART for console use. Do normal init, then enable interrupts.
   1412        1.1       leo  */
   1413        1.1       leo void
   1414       1.41       dsl serinitcons(int baud)
   1415        1.1       leo {
   1416       1.46   tsutsui 
   1417        1.1       leo 	serinit(baud);
   1418        1.1       leo 
   1419        1.1       leo 	/* Set rts/dtr */
   1420        1.1       leo 	ym2149_rts(0);
   1421        1.1       leo 	ym2149_dtr(0);
   1422        1.1       leo 
   1423        1.1       leo 	single_inst_bset_b(MFP->mf_imra, (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR));
   1424        1.1       leo }
   1425        1.1       leo 
   1426        1.1       leo int
   1427       1.41       dsl sercngetc(dev_t dev)
   1428        1.1       leo {
   1429       1.46   tsutsui 	uint8_t stat, c;
   1430       1.46   tsutsui 	int s;
   1431        1.1       leo 
   1432       1.37        ad 	s = splhigh();
   1433        1.1       leo 	while (!ISSET(stat = MFP->mf_rsr, RSR_BFULL)) {
   1434        1.1       leo 		if (!ISSET(stat, RSR_ENAB)) /* XXX */
   1435        1.1       leo 			MFP->mf_rsr |= RSR_ENAB;
   1436        1.1       leo 		if (stat & (RSR_FERR|RSR_PERR|RSR_OERR))
   1437        1.1       leo 			c = MFP->mf_udr;
   1438        1.1       leo 	}
   1439        1.1       leo 	c = MFP->mf_udr;
   1440        1.1       leo 	splx(s);
   1441        1.1       leo 	return c;
   1442        1.1       leo }
   1443        1.1       leo 
   1444       1.46   tsutsui #if 1
   1445        1.1       leo u_int s_imra;
   1446        1.1       leo u_int s_stat1, s_stat2, s_stat3;
   1447       1.46   tsutsui #endif
   1448        1.1       leo void
   1449       1.41       dsl sercnputc(dev_t dev, int c)
   1450        1.1       leo {
   1451       1.46   tsutsui 	int timo;
   1452       1.46   tsutsui 	uint8_t stat, imra;
   1453        1.1       leo 
   1454        1.1       leo 	/* Mask serial interrupts */
   1455        1.1       leo 	imra  = MFP->mf_imra & (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
   1456        1.1       leo 	single_inst_bclr_b(MFP->mf_imra, imra);
   1457       1.46   tsutsui #if 1
   1458       1.46   tsutsui 	s_imra = imra;
   1459       1.46   tsutsui #endif
   1460        1.1       leo 
   1461        1.1       leo 	/* wait for any pending transmission to finish */
   1462        1.1       leo 	timo = 50000;
   1463       1.46   tsutsui #if 1
   1464       1.46   tsutsui 	s_stat1 = MFP->mf_tsr;
   1465       1.46   tsutsui #endif
   1466        1.1       leo 	while (!ISSET(stat = MFP->mf_tsr, TSR_BE) && --timo)
   1467        1.1       leo 		;
   1468        1.1       leo 	MFP->mf_udr = c;
   1469        1.1       leo 	/* wait for this transmission to complete */
   1470        1.1       leo 	timo = 1500000;
   1471       1.46   tsutsui #if 1
   1472       1.46   tsutsui 	s_stat2 = MFP->mf_tsr;
   1473       1.46   tsutsui #endif
   1474        1.1       leo 	while (!ISSET(stat = MFP->mf_tsr, TSR_BE) && --timo)
   1475        1.1       leo 		;
   1476        1.1       leo 
   1477       1.46   tsutsui #if 1
   1478       1.46   tsutsui 	s_stat3 = MFP->mf_tsr;
   1479       1.46   tsutsui #endif
   1480        1.1       leo 	/* Clear pending serial interrupts and re-enable */
   1481       1.46   tsutsui 	MFP->mf_ipra = (uint8_t)~imra;
   1482        1.1       leo 	single_inst_bset_b(MFP->mf_imra, imra);
   1483        1.1       leo }
   1484        1.1       leo 
   1485        1.1       leo void
   1486       1.41       dsl sercnpollc(dev_t dev, int on)
   1487        1.1       leo {
   1488        1.1       leo 
   1489        1.1       leo }
   1490