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