Home | History | Annotate | Line # | Download | only in dev
ser.c revision 1.71
      1  1.71  christos /*	$NetBSD: ser.c,v 1.71 2005/12/11 12:16:28 christos Exp $ */
      2  1.23       cgd 
      3   1.1        mw /*
      4   1.1        mw  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
      5   1.1        mw  * All rights reserved.
      6   1.1        mw  *
      7   1.1        mw  * Redistribution and use in source and binary forms, with or without
      8   1.1        mw  * modification, are permitted provided that the following conditions
      9   1.1        mw  * are met:
     10   1.1        mw  * 1. Redistributions of source code must retain the above copyright
     11   1.1        mw  *    notice, this list of conditions and the following disclaimer.
     12   1.1        mw  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1        mw  *    notice, this list of conditions and the following disclaimer in the
     14   1.1        mw  *    documentation and/or other materials provided with the distribution.
     15  1.69       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1        mw  *    may be used to endorse or promote products derived from this software
     17   1.1        mw  *    without specific prior written permission.
     18   1.1        mw  *
     19   1.1        mw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1        mw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1        mw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1        mw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1        mw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1        mw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1        mw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1        mw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1        mw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1        mw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1        mw  * SUCH DAMAGE.
     30   1.1        mw  *
     31   1.4        mw  *	@(#)ser.c	7.12 (Berkeley) 6/27/91
     32  1.15    chopps  */
     33  1.15    chopps /*
     34  1.32        is  * XXX This file needs major cleanup it will never service more than one
     35  1.15    chopps  * XXX unit.
     36   1.1        mw  */
     37  1.57     lukem 
     38  1.58   aymeric #include "opt_amigacons.h"
     39  1.66   aymeric #include "opt_ddb.h"
     40  1.57     lukem #include "opt_kgdb.h"
     41  1.61   aymeric 
     42  1.61   aymeric #include <sys/cdefs.h>
     43  1.71  christos __KERNEL_RCSID(0, "$NetBSD: ser.c,v 1.71 2005/12/11 12:16:28 christos Exp $");
     44   1.1        mw 
     45  1.10    chopps #include <sys/param.h>
     46  1.10    chopps #include <sys/systm.h>
     47  1.10    chopps #include <sys/ioctl.h>
     48  1.15    chopps #include <sys/device.h>
     49  1.10    chopps #include <sys/tty.h>
     50  1.10    chopps #include <sys/proc.h>
     51  1.10    chopps #include <sys/file.h>
     52  1.10    chopps #include <sys/malloc.h>
     53  1.10    chopps #include <sys/uio.h>
     54  1.10    chopps #include <sys/kernel.h>
     55  1.10    chopps #include <sys/syslog.h>
     56  1.13    chopps #include <sys/queue.h>
     57  1.63   gehenna #include <sys/conf.h>
     58  1.15    chopps #include <machine/cpu.h>
     59  1.15    chopps #include <amiga/amiga/device.h>
     60  1.10    chopps #include <amiga/dev/serreg.h>
     61  1.10    chopps #include <amiga/amiga/custom.h>
     62  1.10    chopps #include <amiga/amiga/cia.h>
     63  1.10    chopps #include <amiga/amiga/cc.h>
     64   1.1        mw 
     65  1.14    chopps #include <dev/cons.h>
     66  1.14    chopps 
     67  1.15    chopps #include "ser.h"
     68  1.15    chopps #if NSER > 0
     69  1.14    chopps 
     70  1.59   aymeric void serattach(struct device *, struct device *, void *);
     71  1.59   aymeric int sermatch(struct device *, struct cfdata *, void *);
     72  1.15    chopps 
     73  1.28       jtc struct ser_softc {
     74  1.28       jtc 	struct device dev;
     75  1.28       jtc 	struct tty *ser_tty;
     76  1.28       jtc };
     77  1.28       jtc 
     78  1.67   thorpej CFATTACH_DECL(ser, sizeof(struct ser_softc),
     79  1.67   thorpej     sermatch, serattach, NULL, NULL);
     80  1.29   thorpej 
     81  1.43   thorpej extern struct cfdriver ser_cd;
     82  1.15    chopps 
     83  1.63   gehenna dev_type_open(seropen);
     84  1.63   gehenna dev_type_close(serclose);
     85  1.63   gehenna dev_type_read(serread);
     86  1.63   gehenna dev_type_write(serwrite);
     87  1.63   gehenna dev_type_ioctl(serioctl);
     88  1.63   gehenna dev_type_stop(serstop);
     89  1.63   gehenna dev_type_tty(sertty);
     90  1.63   gehenna dev_type_poll(serpoll);
     91  1.63   gehenna 
     92  1.63   gehenna const struct cdevsw ser_cdevsw = {
     93  1.63   gehenna 	seropen, serclose, serread, serwrite, serioctl,
     94  1.68  jdolecek 	serstop, sertty, serpoll, nommap, ttykqfilter, D_TTY
     95  1.63   gehenna };
     96  1.63   gehenna 
     97  1.26    chopps #ifndef SEROBUF_SIZE
     98  1.15    chopps #define SEROBUF_SIZE 32
     99  1.26    chopps #endif
    100  1.26    chopps #ifndef SERIBUF_SIZE
    101  1.15    chopps #define SERIBUF_SIZE 512
    102  1.26    chopps #endif
    103  1.26    chopps 
    104  1.26    chopps #define splser() spl5()
    105   1.1        mw 
    106  1.59   aymeric void	serstart(struct tty *);
    107  1.59   aymeric void	ser_shutdown(struct ser_softc *);
    108  1.59   aymeric int	serparam(struct tty *, struct termios *);
    109  1.59   aymeric void	serintr(void);
    110  1.59   aymeric int	serhwiflow(struct tty *, int);
    111  1.59   aymeric int	sermctl(dev_t dev, int, int);
    112  1.59   aymeric void	ser_fastint(void);
    113  1.59   aymeric void	sereint(int);
    114  1.59   aymeric static	void ser_putchar(struct tty *, u_short);
    115  1.59   aymeric void	ser_outintr(void);
    116  1.59   aymeric void	sercnprobe(struct consdev *);
    117  1.59   aymeric void	sercninit(struct consdev *);
    118  1.59   aymeric void	serinit(int);
    119  1.59   aymeric int	sercngetc(dev_t dev);
    120  1.59   aymeric void	sercnputc(dev_t, int);
    121  1.59   aymeric void	sercnpollc(dev_t, int);
    122  1.31     veego 
    123   1.1        mw int	nser = NSER;
    124   1.1        mw #ifdef SERCONSOLE
    125  1.58   aymeric int	serconsole = 0;
    126   1.1        mw #else
    127   1.1        mw int	serconsole = -1;
    128   1.1        mw #endif
    129   1.1        mw int	serconsinit;
    130   1.1        mw int	serdefaultrate = TTYDEF_SPEED;
    131  1.14    chopps int	serswflags;
    132  1.14    chopps 
    133  1.44        is struct	vbl_node ser_vbl_node;
    134   1.1        mw struct	tty ser_cons;
    135  1.44        is struct	tty *ser_tty;
    136  1.44        is 
    137  1.44        is static u_short serbuf[SERIBUF_SIZE];
    138  1.44        is static u_short *sbrpt = serbuf;
    139  1.44        is static u_short *sbwpt = serbuf;
    140  1.44        is static u_short sbcnt;
    141  1.44        is static u_short sbovfl;
    142  1.44        is static u_char serdcd;
    143   1.1        mw 
    144  1.59   aymeric /*
    145  1.14    chopps  * Since this UART is not particularly bright (to put it nicely), we'll
    146  1.14    chopps  * have to do parity stuff on our own.	This table contains the 8th bit
    147  1.14    chopps  * in 7bit character mode, for even parity.  If you want odd parity,
    148  1.14    chopps  * flip the bit.  (for generation of the table, see genpar.c)
    149  1.14    chopps  */
    150  1.14    chopps 
    151  1.14    chopps u_char	even_parity[] = {
    152  1.14    chopps 	0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    153  1.14    chopps 	1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
    154  1.14    chopps 	1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
    155  1.14    chopps 	0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    156  1.14    chopps 	1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
    157  1.14    chopps 	0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    158  1.14    chopps 	0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    159  1.14    chopps 	1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
    160   1.1        mw };
    161   1.1        mw 
    162  1.59   aymeric /*
    163  1.14    chopps  * Since we don't get interrupts for changes on the modem control line,
    164  1.14    chopps  * we'll have to fake them by comparing current settings to the settings
    165  1.14    chopps  * we remembered on last invocation.
    166  1.14    chopps  */
    167  1.14    chopps 
    168  1.14    chopps u_char	last_ciab_pra;
    169  1.14    chopps 
    170  1.14    chopps extern struct tty *constty;
    171   1.1        mw 
    172  1.42    mhitch extern int ser_open_speed;	/* current speed of open serial device */
    173  1.42    mhitch 
    174   1.1        mw #ifdef KGDB
    175  1.10    chopps #include <machine/remote-sl.h>
    176   1.1        mw 
    177   1.1        mw extern dev_t kgdb_dev;
    178   1.1        mw extern int kgdb_rate;
    179   1.1        mw extern int kgdb_debug_init;
    180   1.1        mw #endif
    181   1.1        mw 
    182   1.1        mw #ifdef DEBUG
    183   1.1        mw long	fifoin[17];
    184   1.1        mw long	fifoout[17];
    185   1.1        mw long	serintrcount[16];
    186   1.1        mw long	sermintcount[16];
    187   1.1        mw #endif
    188   1.1        mw 
    189  1.59   aymeric void	sermint(register int unit);
    190   1.5        mw 
    191   1.5        mw int
    192  1.59   aymeric sermatch(struct device *pdp, struct cfdata *cfp, void *auxp)
    193  1.15    chopps {
    194  1.50    kleink 	static int ser_matched = 0;
    195  1.52        is 	static int ser_matched_real = 0;
    196  1.29   thorpej 
    197  1.50    kleink 	/* Allow only once instance. */
    198  1.52        is 	if (matchname("ser", (char *)auxp) == 0)
    199  1.15    chopps 		return(0);
    200  1.52        is 
    201  1.52        is 	if (amiga_realconfig) {
    202  1.52        is 		if (ser_matched_real)
    203  1.52        is 			return(0);
    204  1.52        is 		ser_matched_real = 1;
    205  1.53        is 	} else {
    206  1.53        is 		if (serconsole != 0)
    207  1.53        is 			return(0);
    208  1.52        is 
    209  1.53        is 		if (ser_matched != 0)
    210  1.53        is 			return(0);
    211  1.50    kleink 
    212  1.53        is 		ser_matched = 1;
    213  1.53        is 	}
    214  1.15    chopps 	return(1);
    215  1.15    chopps }
    216  1.15    chopps 
    217  1.15    chopps 
    218  1.15    chopps void
    219  1.59   aymeric serattach(struct device *pdp, struct device *dp, void *auxp)
    220   1.1        mw {
    221  1.44        is 	struct ser_softc *sc;
    222  1.44        is 	struct tty *tp;
    223  1.15    chopps 	u_short ir;
    224  1.14    chopps 
    225  1.44        is 	sc = (struct ser_softc *)dp;
    226  1.44        is 
    227  1.14    chopps 	ir = custom.intenar;
    228  1.15    chopps 	if (serconsole == 0)
    229  1.14    chopps 		DELAY(100000);
    230  1.14    chopps 
    231  1.44        is 	ser_vbl_node.function = (void (*) (void *)) sermint;
    232  1.44        is 	add_vbl_function(&ser_vbl_node, SER_VBL_PRIORITY, (void *) 0);
    233   1.1        mw #ifdef KGDB
    234  1.63   gehenna 	if (kgdb_dev == makedev(cdevsw_lookup_major(&ser_cdevsw), 0)) {
    235  1.15    chopps 		if (serconsole == 0)
    236  1.14    chopps 			kgdb_dev = NODEV; /* can't debug over console port */
    237  1.14    chopps 		else {
    238  1.44        is 			(void) serinit(kgdb_rate);
    239  1.14    chopps 			serconsinit = 1;       /* don't re-init in serputc */
    240  1.14    chopps 			if (kgdb_debug_init == 0)
    241  1.38  christos 				printf(" kgdb enabled\n");
    242  1.14    chopps 			else {
    243  1.14    chopps 				/*
    244  1.14    chopps 				 * Print prefix of device name,
    245  1.14    chopps 				 * let kgdb_connect print the rest.
    246  1.14    chopps 				 */
    247  1.38  christos 				printf("ser0: ");
    248  1.14    chopps 				kgdb_connect(1);
    249  1.14    chopps 			}
    250  1.14    chopps 		}
    251  1.14    chopps 	}
    252   1.5        mw #endif
    253  1.14    chopps 	/*
    254  1.14    chopps 	 * Need to reset baud rate, etc. of next print so reset serconsinit.
    255  1.14    chopps 	 */
    256  1.15    chopps 	if (0 == serconsole)
    257  1.14    chopps 		serconsinit = 0;
    258  1.44        is 
    259  1.44        is 	tp = ttymalloc();
    260  1.44        is 	tp->t_oproc = (void (*) (struct tty *)) serstart;
    261  1.44        is 	tp->t_param = serparam;
    262  1.44        is 	tp->t_hwiflow = serhwiflow;
    263  1.44        is 	tty_attach(tp);
    264  1.44        is 	sc->ser_tty = ser_tty = tp;
    265  1.44        is 
    266  1.15    chopps 	if (dp)
    267  1.38  christos 		printf(": input fifo %d output fifo %d\n", SERIBUF_SIZE,
    268  1.15    chopps 		    SEROBUF_SIZE);
    269   1.1        mw }
    270   1.1        mw 
    271  1.14    chopps 
    272   1.1        mw /* ARGSUSED */
    273   1.5        mw int
    274  1.71  christos seropen(dev_t dev, int flag, int mode, struct lwp *l)
    275   1.1        mw {
    276  1.44        is 	struct ser_softc *sc;
    277  1.14    chopps 	struct tty *tp;
    278  1.44        is 	int unit, error, s, s2;
    279  1.14    chopps 
    280  1.14    chopps 	error = 0;
    281  1.14    chopps 	unit = SERUNIT(dev);
    282  1.14    chopps 
    283  1.44        is 	if (unit >= ser_cd.cd_ndevs)
    284  1.44        is 		return (ENXIO);
    285  1.44        is 
    286  1.44        is 	sc = ser_cd.cd_devs[unit];
    287  1.44        is 	if (sc == 0)
    288  1.14    chopps 		return (ENXIO);
    289  1.14    chopps 
    290  1.44        is 	/* XXX com.c: insert KGDB check here */
    291  1.44        is 
    292  1.44        is 	/* XXX ser.c had: s = spltty(); */
    293  1.44        is 
    294  1.44        is 	tp = sc->ser_tty;
    295  1.44        is 
    296  1.44        is 	if ((tp->t_state & TS_ISOPEN) &&
    297  1.44        is 	    (tp->t_state & TS_XCLUDE) &&
    298  1.71  christos 	    suser(l->l_proc->p_ucred, &l->l_proc->p_acflag) != 0)
    299  1.44        is 		return (EBUSY);
    300  1.44        is 
    301  1.14    chopps 	s = spltty();
    302  1.14    chopps 
    303  1.44        is 	/*
    304  1.44        is 	 * If this is a first open...
    305  1.44        is 	 */
    306  1.44        is 
    307  1.44        is 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
    308  1.44        is 		struct termios t;
    309  1.14    chopps 
    310  1.44        is 		tp->t_dev = dev;
    311  1.14    chopps 
    312  1.44        is 		s2 = splser();
    313  1.14    chopps 		/*
    314  1.44        is 		 * XXX here: hw enable,
    315  1.14    chopps 		 */
    316  1.46        is 		last_ciab_pra = ciab.pra;
    317  1.44        is 
    318  1.44        is 		splx(s2);
    319  1.44        is 		t.c_ispeed = 0;
    320  1.44        is 
    321  1.44        is 		/* XXX serconsolerate? */
    322  1.44        is 		t.c_ospeed = TTYDEF_SPEED;
    323  1.44        is 		t.c_cflag = TTYDEF_CFLAG;
    324  1.44        is 
    325  1.14    chopps 		if (serswflags & TIOCFLAG_CLOCAL)
    326  1.44        is 			t.c_cflag |= CLOCAL;
    327  1.14    chopps 		if (serswflags & TIOCFLAG_CRTSCTS)
    328  1.44        is 			t.c_cflag |= CRTSCTS;
    329  1.14    chopps 		if (serswflags & TIOCFLAG_MDMBUF)
    330  1.44        is 			t.c_cflag |= MDMBUF;
    331  1.44        is 
    332  1.44        is 		/* Make sure serparam() will do something. */
    333  1.44        is 		tp->t_ospeed = 0;
    334  1.44        is 		serparam(tp, &t);
    335  1.44        is 		tp->t_iflag = TTYDEF_IFLAG;
    336  1.44        is 		tp->t_oflag = TTYDEF_OFLAG;
    337  1.44        is 		tp->t_lflag = TTYDEF_LFLAG;
    338  1.44        is 		ttychars(tp);
    339  1.14    chopps 		ttsetwater(tp);
    340  1.44        is 
    341  1.44        is 		s2 = splser();
    342  1.45        is 		(void)sermctl(dev, TIOCM_DTR, DMSET);
    343  1.44        is 		/* clear input ring */
    344  1.44        is 		sbrpt = sbwpt = serbuf;
    345  1.44        is 		sbcnt = 0;
    346  1.44        is 		splx(s2);
    347  1.14    chopps 	}
    348  1.14    chopps 
    349  1.44        is 	splx(s);
    350  1.44        is 
    351  1.44        is 	error = ttyopen(tp, DIALOUT(dev), flag & O_NONBLOCK);
    352  1.44        is 	if (error)
    353  1.44        is 		goto bad;
    354  1.44        is 
    355  1.55   aymeric 	error =  tp->t_linesw->l_open(dev, tp);
    356  1.44        is 	if (error)
    357  1.44        is 		goto bad;
    358  1.44        is 
    359  1.44        is 	return (0);
    360  1.14    chopps 
    361  1.44        is bad:
    362  1.44        is 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    363  1.44        is 		ser_shutdown(sc);
    364  1.22    chopps 	}
    365  1.22    chopps 
    366  1.44        is 	return (error);
    367   1.1        mw }
    368  1.14    chopps 
    369   1.1        mw /*ARGSUSED*/
    370   1.5        mw int
    371  1.71  christos serclose(dev_t dev, int flag, int mode, struct lwp *l)
    372  1.14    chopps {
    373  1.44        is 	struct ser_softc *sc;
    374  1.14    chopps 	struct tty *tp;
    375  1.14    chopps 
    376  1.44        is 	sc = ser_cd.cd_devs[0];
    377  1.44        is 	tp = ser_tty;
    378  1.44        is 
    379  1.44        is 	/* XXX This is for cons.c, according to com.c */
    380  1.44        is 	if (!(tp->t_state & TS_ISOPEN))
    381  1.44        is 		return (0);
    382  1.14    chopps 
    383  1.55   aymeric 	tp->t_linesw->l_close(tp, flag);
    384  1.44        is 	ttyclose(tp);
    385  1.44        is 
    386  1.44        is 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    387  1.44        is 		ser_shutdown(sc);
    388  1.44        is 	}
    389  1.44        is 	return (0);
    390  1.44        is }
    391  1.44        is 
    392  1.44        is void
    393  1.59   aymeric ser_shutdown(struct ser_softc *sc)
    394  1.44        is {
    395  1.44        is 	struct tty *tp = sc->ser_tty;
    396  1.44        is 	int s;
    397  1.44        is 
    398  1.44        is 	s = splser();
    399  1.44        is 
    400  1.14    chopps 	custom.adkcon = ADKCONF_UARTBRK;	/* clear break */
    401  1.44        is #if 0 /* XXX fix: #ifdef KGDB */
    402  1.59   aymeric 	/*
    403  1.14    chopps 	 * do not disable interrupts if debugging
    404  1.14    chopps 	 */
    405  1.14    chopps 	if (dev != kgdb_dev)
    406   1.1        mw #endif
    407  1.44        is 		custom.intena = INTF_RBF | INTF_TBE;	/* disable interrupts */
    408  1.14    chopps 	custom.intreq = INTF_RBF | INTF_TBE;		/* clear intr request */
    409  1.14    chopps 
    410  1.14    chopps 	/*
    411  1.41        is 	 * If HUPCL is not set, leave DTR unchanged.
    412  1.14    chopps 	 */
    413  1.44        is 	if (tp->t_cflag & HUPCL) {
    414  1.44        is 		(void)sermctl(tp->t_dev, TIOCM_DTR, DMBIC);
    415  1.44        is 		/*
    416  1.59   aymeric 		 * Idea from dev/ic/com.c:
    417  1.44        is 		 * sleep a bit so that other side will notice, even if we
    418  1.44        is 		 * reopen immediately.
    419  1.44        is 		 */
    420  1.44        is 		(void) tsleep(tp, TTIPRI, ttclos, hz);
    421  1.44        is 	}
    422  1.41        is 
    423  1.14    chopps #if not_yet
    424  1.14    chopps 	if (tp != &ser_cons) {
    425  1.44        is 		remove_vbl_function(&ser_vbl_node);
    426  1.14    chopps 		ttyfree(tp);
    427  1.44        is 		ser_tty = (struct tty *) NULL;
    428  1.14    chopps 	}
    429   1.3        mw #endif
    430  1.42    mhitch 	ser_open_speed = tp->t_ispeed;
    431  1.44        is 	return;
    432   1.1        mw }
    433  1.14    chopps 
    434   1.5        mw int
    435  1.59   aymeric serread(dev_t dev, struct uio *uio, int flag)
    436   1.1        mw {
    437  1.44        is 	/* ARGSUSED */
    438  1.44        is 
    439  1.55   aymeric 	return ser_tty->t_linesw->l_read(ser_tty, uio, flag);
    440  1.14    chopps }
    441   1.1        mw 
    442   1.5        mw int
    443  1.59   aymeric serwrite(dev_t dev, struct uio *uio, int flag)
    444   1.1        mw {
    445  1.44        is 	/* ARGSUSED */
    446  1.14    chopps 
    447  1.55   aymeric 	return ser_tty->t_linesw->l_write(ser_tty, uio, flag);
    448  1.56       scw }
    449  1.56       scw 
    450  1.56       scw int
    451  1.71  christos serpoll(dev_t dev, int events, struct lwp *l)
    452  1.56       scw {
    453  1.56       scw 	/* ARGSUSED */
    454  1.59   aymeric 
    455  1.71  christos 	return ser_tty->t_linesw->l_poll(ser_tty, events, l);
    456   1.1        mw }
    457   1.3        mw 
    458  1.27    chopps struct tty *
    459  1.59   aymeric sertty(dev_t dev)
    460  1.27    chopps {
    461  1.44        is 	/* ARGSUSED */
    462  1.44        is 
    463  1.44        is 	return (ser_tty);
    464  1.27    chopps }
    465   1.3        mw 
    466  1.14    chopps /*
    467  1.14    chopps  * We don't do any processing of data here, so we store the raw code
    468  1.14    chopps  * obtained from the uart register.  In theory, 110kBaud gives you
    469  1.14    chopps  * 11kcps, so 16k buffer should be more than enough, interrupt
    470  1.14    chopps  * latency of 1s should never happen, or something is seriously
    471  1.14    chopps  * wrong..
    472  1.44        is  * buffers moved to above seropen()	-is
    473  1.14    chopps  */
    474  1.14    chopps 
    475  1.14    chopps /*
    476  1.14    chopps  * This is a replacement for the lack of a hardware fifo.  32k should be
    477  1.14    chopps  * enough (there's only one unit anyway, so this is not going to
    478  1.14    chopps  * accumulate).
    479  1.14    chopps  */
    480   1.3        mw void
    481  1.59   aymeric ser_fastint(void)
    482   1.3        mw {
    483  1.59   aymeric 	/*
    484  1.14    chopps 	 * We're at RBE-level, which is higher than VBL-level which is used
    485  1.14    chopps 	 * to periodically transmit contents of this buffer up one layer,
    486  1.59   aymeric 	 * so no spl-raising is necessary.
    487  1.14    chopps 	 */
    488  1.47        is 	u_short code;
    489  1.14    chopps 
    490  1.47        is 	/*
    491  1.47        is 	 * This register contains both data and status bits!
    492  1.47        is 	 */
    493  1.47        is 	code = custom.serdatr;
    494  1.14    chopps 
    495  1.40    mhitch 	/*
    496  1.47        is 	 * Use SERDATF_RBF instead of INTF_RBF; they're equivalent, but
    497  1.47        is 	 * we save one (slow) custom chip access.
    498  1.40    mhitch 	 */
    499  1.47        is 	if ((code & SERDATRF_RBF) == 0)
    500  1.47        is 		return;
    501  1.40    mhitch 
    502  1.59   aymeric 	/*
    503  1.59   aymeric 	 * clear interrupt
    504  1.14    chopps 	 */
    505  1.44        is 	custom.intreq = INTF_RBF;
    506  1.14    chopps 
    507  1.14    chopps 	/*
    508  1.14    chopps 	 * check for buffer overflow.
    509  1.14    chopps 	 */
    510  1.22    chopps 	if (sbcnt == SERIBUF_SIZE) {
    511  1.18    chopps 		++sbovfl;
    512  1.14    chopps 		return;
    513  1.14    chopps 	}
    514  1.14    chopps 	/*
    515  1.14    chopps 	 * store in buffer
    516  1.14    chopps 	 */
    517  1.14    chopps 	*sbwpt++ = code;
    518  1.14    chopps 	if (sbwpt == serbuf + SERIBUF_SIZE)
    519  1.14    chopps 		sbwpt = serbuf;
    520  1.22    chopps 	++sbcnt;
    521  1.26    chopps 	if (sbcnt > SERIBUF_SIZE - 20)
    522  1.22    chopps 		CLRRTS(ciab.pra);	/* drop RTS if buffer almost full */
    523   1.3        mw }
    524   1.3        mw 
    525   1.3        mw 
    526  1.31     veego void
    527  1.59   aymeric serintr(void)
    528   1.1        mw {
    529  1.18    chopps 	int s1, s2, ovfl;
    530  1.44        is 	struct tty *tp = ser_tty;
    531   1.1        mw 
    532  1.14    chopps 	/*
    533  1.14    chopps 	 * Make sure we're not interrupted by another
    534  1.14    chopps 	 * vbl, but allow level5 ints
    535  1.14    chopps 	 */
    536  1.14    chopps 	s1 = spltty();
    537   1.1        mw 
    538  1.14    chopps 	/*
    539  1.14    chopps 	 * pass along any acumulated information
    540  1.14    chopps 	 */
    541  1.22    chopps 	while (sbcnt > 0 && (tp->t_state & TS_TBLOCK) == 0) {
    542  1.59   aymeric 		/*
    543  1.14    chopps 		 * no collision with ser_fastint()
    544  1.14    chopps 		 */
    545  1.44        is 		sereint(*sbrpt++);
    546  1.14    chopps 
    547  1.18    chopps 		ovfl = 0;
    548  1.14    chopps 		/* lock against ser_fastint() */
    549  1.26    chopps 		s2 = splser();
    550  1.22    chopps 		sbcnt--;
    551  1.14    chopps 		if (sbrpt == serbuf + SERIBUF_SIZE)
    552  1.14    chopps 			sbrpt = serbuf;
    553  1.18    chopps 		if (sbovfl != 0) {
    554  1.18    chopps 			ovfl = sbovfl;
    555  1.18    chopps 			sbovfl = 0;
    556  1.18    chopps 		}
    557  1.14    chopps 		splx(s2);
    558  1.18    chopps 		if (ovfl != 0)
    559  1.21    chopps 			log(LOG_WARNING, "ser0: %d ring buffer overflows.\n",
    560  1.21    chopps 			    ovfl);
    561  1.14    chopps 	}
    562  1.33        is 	s2 = splser();
    563  1.22    chopps 	if (sbcnt == 0 && (tp->t_state & TS_TBLOCK) == 0)
    564  1.22    chopps 		SETRTS(ciab.pra);	/* start accepting data again */
    565  1.33        is 	splx(s2);
    566  1.14    chopps 	splx(s1);
    567   1.1        mw }
    568   1.1        mw 
    569  1.31     veego void
    570  1.59   aymeric sereint(int stat)
    571   1.1        mw {
    572  1.66   aymeric 	static int break_in_progress = 0;
    573  1.14    chopps 	struct tty *tp;
    574  1.14    chopps 	u_char ch;
    575  1.14    chopps 	int c;
    576  1.14    chopps 
    577  1.44        is 	tp = ser_tty;
    578  1.49        is 	ch = stat & 0xff;
    579  1.14    chopps 	c = ch;
    580  1.14    chopps 
    581  1.14    chopps 	if ((tp->t_state & TS_ISOPEN) == 0) {
    582   1.1        mw #ifdef KGDB
    583  1.63   gehenna 		int maj;
    584  1.63   gehenna 
    585  1.14    chopps 		/* we don't care about parity errors */
    586  1.63   gehenna 		maj = cdevsw_lookup_major(&ser_cdevsw);
    587  1.63   gehenna 		if (kgdb_dev == makedev(maj, 0) && c == FRAME_END)
    588  1.14    chopps 			kgdb_connect(0);	/* trap into kgdb */
    589   1.1        mw #endif
    590  1.14    chopps 		return;
    591  1.14    chopps 	}
    592  1.14    chopps 
    593  1.14    chopps 	/*
    594  1.14    chopps 	 * Check for break and (if enabled) parity error.
    595  1.14    chopps 	 */
    596  1.66   aymeric 	if ((stat & 0x1ff) == 0) {
    597  1.66   aymeric 		if (break_in_progress)
    598  1.66   aymeric 			return;
    599  1.66   aymeric 
    600  1.66   aymeric 		c = TTY_FE;
    601  1.66   aymeric 		break_in_progress = 1;
    602  1.66   aymeric #ifdef DDB
    603  1.66   aymeric 		if (serconsole == 0) {
    604  1.66   aymeric 			extern int db_active;
    605  1.66   aymeric 
    606  1.66   aymeric 			if (!db_active) {
    607  1.66   aymeric 				console_debugger();
    608  1.66   aymeric 				return;
    609  1.66   aymeric 			}
    610  1.66   aymeric 		}
    611  1.66   aymeric #endif
    612  1.66   aymeric 	} else {
    613  1.66   aymeric 		break_in_progress = 0;
    614  1.66   aymeric 		if ((tp->t_cflag & PARENB) &&
    615  1.66   aymeric 			(((ch >> 7) + even_parity[ch & 0x7f]
    616  1.66   aymeric 				+ !!(tp->t_cflag & PARODD)) & 1))
    617  1.14    chopps 			c |= TTY_PE;
    618  1.66   aymeric 	}
    619  1.14    chopps 
    620  1.14    chopps 	if (stat & SERDATRF_OVRUN)
    621  1.15    chopps 		log(LOG_WARNING, "ser0: silo overflow\n");
    622   1.1        mw 
    623  1.55   aymeric 	tp->t_linesw->l_rint(c, tp);
    624  1.14    chopps }
    625  1.14    chopps 
    626  1.14    chopps /*
    627  1.14    chopps  * This interrupt is periodically invoked in the vertical blank
    628  1.14    chopps  * interrupt.  It's used to keep track of the modem control lines
    629  1.14    chopps  * and (new with the fast_int code) to move accumulated data
    630  1.14    chopps  * up into the tty layer.
    631  1.14    chopps  */
    632   1.3        mw void
    633  1.59   aymeric sermint(int unit)
    634   1.1        mw {
    635  1.14    chopps 	struct tty *tp;
    636  1.14    chopps 	u_char stat, last, istat;
    637  1.14    chopps 
    638  1.44        is 	tp = ser_tty;
    639  1.14    chopps 	if (!tp)
    640  1.14    chopps 		return;
    641  1.14    chopps 
    642  1.44        is 	/*
    643  1.44        is 	if ((tp->t_state & TS_ISOPEN) == 0 || tp->t_wopen == 0) {
    644  1.14    chopps 		sbrpt = sbwpt = serbuf;
    645  1.14    chopps 		return;
    646  1.14    chopps 	}
    647  1.44        is 	*/
    648  1.14    chopps 	/*
    649  1.14    chopps 	 * empty buffer
    650  1.14    chopps 	 */
    651  1.44        is 	serintr();
    652  1.14    chopps 
    653  1.14    chopps 	stat = ciab.pra;
    654  1.14    chopps 	last = last_ciab_pra;
    655  1.14    chopps 	last_ciab_pra = stat;
    656  1.14    chopps 
    657  1.14    chopps 	/*
    658  1.14    chopps 	 * check whether any interesting signal changed state
    659  1.14    chopps 	 */
    660  1.14    chopps 	istat = stat ^ last;
    661   1.1        mw 
    662  1.44        is 	if (istat & serdcd) {
    663  1.55   aymeric 		tp->t_linesw->l_modem(tp, ISDCD(stat));
    664  1.14    chopps 	}
    665  1.44        is 
    666  1.14    chopps 	if ((istat & CIAB_PRA_CTS) && (tp->t_state & TS_ISOPEN) &&
    667  1.14    chopps 	    (tp->t_cflag & CRTSCTS)) {
    668   1.5        mw #if 0
    669  1.14    chopps 		/* the line is up and we want to do rts/cts flow control */
    670  1.14    chopps 		if (ISCTS(stat)) {
    671  1.14    chopps 			tp->t_state &= ~TS_TTSTOP;
    672  1.14    chopps 			ttstart(tp);
    673  1.14    chopps 			/* cause tbe-int if we were stuck there */
    674  1.14    chopps 			custom.intreq = INTF_SETCLR | INTF_TBE;
    675  1.14    chopps 		} else
    676  1.14    chopps 			tp->t_state |= TS_TTSTOP;
    677   1.5        mw #else
    678  1.14    chopps 		/* do this on hardware level, not with tty driver */
    679  1.14    chopps 		if (ISCTS(stat)) {
    680  1.14    chopps 			tp->t_state &= ~TS_TTSTOP;
    681  1.14    chopps 			/* cause TBE interrupt */
    682  1.14    chopps 			custom.intreq = INTF_SETCLR | INTF_TBE;
    683  1.14    chopps 		}
    684  1.14    chopps #endif
    685   1.5        mw 	}
    686   1.1        mw }
    687   1.1        mw 
    688   1.5        mw int
    689  1.71  christos serioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    690   1.1        mw {
    691  1.14    chopps 	register struct tty *tp;
    692  1.14    chopps 	register int error;
    693  1.14    chopps 
    694  1.44        is 	tp = ser_tty;
    695  1.14    chopps 	if (!tp)
    696  1.14    chopps 		return ENXIO;
    697  1.14    chopps 
    698  1.71  christos 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l);
    699  1.62    atatat 	if (error != EPASSTHROUGH)
    700  1.14    chopps 		return(error);
    701  1.14    chopps 
    702  1.71  christos 	error = ttioctl(tp, cmd, data, flag, l);
    703  1.62    atatat 	if (error != EPASSTHROUGH)
    704  1.14    chopps 		return(error);
    705  1.14    chopps 
    706  1.14    chopps 	switch (cmd) {
    707  1.14    chopps 	case TIOCSBRK:
    708  1.14    chopps 		custom.adkcon = ADKCONF_SETCLR | ADKCONF_UARTBRK;
    709  1.14    chopps 		break;
    710  1.14    chopps 
    711  1.14    chopps 	case TIOCCBRK:
    712  1.14    chopps 		custom.adkcon = ADKCONF_UARTBRK;
    713  1.14    chopps 		break;
    714  1.14    chopps 
    715  1.14    chopps 	case TIOCSDTR:
    716  1.44        is 		(void) sermctl(dev, TIOCM_DTR, DMBIS);
    717  1.14    chopps 		break;
    718  1.14    chopps 
    719  1.14    chopps 	case TIOCCDTR:
    720  1.44        is 		(void) sermctl(dev, TIOCM_DTR, DMBIC);
    721  1.14    chopps 		break;
    722  1.14    chopps 
    723  1.14    chopps 	case TIOCMSET:
    724  1.14    chopps 		(void) sermctl(dev, *(int *) data, DMSET);
    725  1.14    chopps 		break;
    726  1.14    chopps 
    727  1.14    chopps 	case TIOCMBIS:
    728  1.14    chopps 		(void) sermctl(dev, *(int *) data, DMBIS);
    729  1.14    chopps 		break;
    730  1.14    chopps 
    731  1.14    chopps 	case TIOCMBIC:
    732  1.14    chopps 		(void) sermctl(dev, *(int *) data, DMBIC);
    733  1.14    chopps 		break;
    734  1.14    chopps 
    735  1.14    chopps 	case TIOCMGET:
    736  1.14    chopps 		*(int *)data = sermctl(dev, 0, DMGET);
    737  1.14    chopps 		break;
    738  1.14    chopps 	case TIOCGFLAGS:
    739  1.44        is 		*(int *)data = serswflags;
    740  1.14    chopps 		break;
    741  1.14    chopps 	case TIOCSFLAGS:
    742  1.71  christos 		error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
    743  1.14    chopps 		if (error != 0)
    744  1.59   aymeric 			return(EPERM);
    745  1.14    chopps 
    746  1.14    chopps 		serswflags = *(int *)data;
    747  1.14    chopps                 serswflags &= /* only allow valid flags */
    748  1.14    chopps                   (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS);
    749  1.14    chopps 		break;
    750  1.14    chopps 	default:
    751  1.62    atatat 		return(EPASSTHROUGH);
    752  1.14    chopps 	}
    753   1.1        mw 
    754  1.14    chopps 	return(0);
    755   1.1        mw }
    756   1.1        mw 
    757   1.5        mw int
    758  1.59   aymeric serparam(struct tty *tp, struct termios *t)
    759   1.1        mw {
    760  1.44        is 	int cflag, ospeed = 0;
    761  1.59   aymeric 
    762  1.32        is 	if (t->c_ospeed > 0) {
    763  1.32        is 		if (t->c_ospeed < 110)
    764  1.32        is 			return(EINVAL);
    765  1.32        is 		ospeed = SERBRD(t->c_ospeed);
    766  1.32        is 	}
    767  1.32        is 
    768  1.32        is 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    769  1.14    chopps 		return(EINVAL);
    770  1.14    chopps 
    771  1.60   aymeric 	if (serswflags & TIOCFLAG_SOFTCAR || serconsole == 0) {
    772  1.44        is 		t->c_cflag = (t->c_cflag & ~HUPCL) | CLOCAL;
    773  1.44        is 	}
    774  1.44        is 
    775  1.44        is 	/* if no changes, dont do anything. com.c explains why. */
    776  1.44        is 	if (tp->t_ospeed == t->c_ospeed &&
    777  1.44        is 	    tp->t_cflag == t->c_cflag)
    778  1.44        is 		return (0);
    779  1.44        is 
    780  1.44        is 	cflag = t->c_cflag;
    781  1.44        is 
    782  1.44        is 	if (cflag & (CLOCAL | MDMBUF))
    783  1.44        is 		serdcd = 0;
    784  1.44        is 	else
    785  1.44        is 		serdcd = CIAB_PRA_CD;
    786  1.44        is 
    787  1.44        is 	/* TODO: support multiple flow control protocols like com.c */
    788  1.44        is 
    789  1.59   aymeric 	/*
    790  1.14    chopps 	 * copy to tty
    791  1.14    chopps 	 */
    792  1.14    chopps 	tp->t_ispeed = t->c_ispeed;
    793  1.14    chopps 	tp->t_ospeed = t->c_ospeed;
    794  1.14    chopps 	tp->t_cflag = cflag;
    795  1.42    mhitch 	ser_open_speed = tp->t_ispeed;
    796  1.14    chopps 
    797  1.14    chopps 	/*
    798  1.14    chopps 	 * enable interrupts
    799  1.14    chopps 	 */
    800  1.14    chopps 	custom.intena = INTF_SETCLR | INTF_RBF | INTF_TBE;
    801  1.14    chopps 	last_ciab_pra = ciab.pra;
    802  1.14    chopps 
    803  1.32        is 	if (t->c_ospeed == 0)
    804  1.14    chopps 		(void)sermctl(tp->t_dev, 0, DMSET);	/* hang up line */
    805  1.14    chopps 	else {
    806  1.59   aymeric 		/*
    807  1.14    chopps 		 * (re)enable DTR
    808  1.14    chopps 		 * and set baud rate. (8 bit mode)
    809  1.14    chopps 		 */
    810  1.45        is 		(void)sermctl(tp->t_dev, TIOCM_DTR, DMSET);
    811  1.14    chopps 		custom.serper = (0 << 15) | ospeed;
    812  1.14    chopps 	}
    813  1.55   aymeric 	(void)tp->t_linesw->l_modem(tp, ISDCD(last_ciab_pra));
    814  1.59   aymeric 
    815  1.14    chopps 	return(0);
    816   1.1        mw }
    817   1.3        mw 
    818  1.59   aymeric int serhwiflow(struct tty *tp, int flag)
    819  1.22    chopps {
    820  1.22    chopps #if 0
    821  1.38  christos 	printf ("serhwiflow %d\n", flag);
    822  1.22    chopps #endif
    823  1.22    chopps         if (flag)
    824  1.22    chopps 		CLRRTS(ciab.pra);
    825  1.22    chopps 	else
    826  1.22    chopps 	        SETRTS(ciab.pra);
    827  1.22    chopps         return 1;
    828  1.22    chopps }
    829   1.3        mw 
    830   1.3        mw static void
    831  1.59   aymeric ser_putchar(struct tty *tp, u_short c)
    832  1.14    chopps {
    833  1.49        is 	if ((tp->t_cflag & CSIZE) == CS7 || (tp->t_cflag & PARENB))
    834  1.49        is 		c &= 0x7f;
    835  1.14    chopps 
    836  1.14    chopps 	/*
    837  1.14    chopps 	 * handle parity if necessary
    838  1.14    chopps 	 */
    839  1.49        is 	if (tp->t_cflag & PARENB) {
    840  1.14    chopps 		if (even_parity[c])
    841  1.49        is 			c |= 0x80;
    842  1.49        is 		if (tp->t_cflag & PARODD)
    843  1.49        is 			c ^= 0x80;
    844  1.14    chopps 	}
    845  1.59   aymeric 	/*
    846  1.14    chopps 	 * add stop bit(s)
    847  1.14    chopps 	 */
    848  1.49        is 	if (tp->t_cflag & CSTOPB)
    849  1.49        is 		c |= 0x300;
    850  1.49        is 	else
    851  1.49        is 		c |= 0x100;
    852  1.14    chopps 
    853  1.14    chopps 	custom.serdat = c;
    854   1.3        mw }
    855   1.3        mw 
    856   1.3        mw 
    857   1.3        mw static u_char ser_outbuf[SEROBUF_SIZE];
    858  1.14    chopps static u_char *sob_ptr = ser_outbuf, *sob_end = ser_outbuf;
    859  1.14    chopps 
    860   1.3        mw void
    861  1.59   aymeric ser_outintr(void)
    862   1.3        mw {
    863  1.44        is 	struct tty *tp;
    864  1.14    chopps 	int s;
    865  1.14    chopps 
    866  1.44        is 	tp = ser_tty;
    867  1.14    chopps 	s = spltty();
    868  1.14    chopps 
    869  1.14    chopps 	if (tp == 0)
    870  1.14    chopps 		goto out;
    871  1.14    chopps 
    872  1.14    chopps 	if ((custom.intreqr & INTF_TBE) == 0)
    873  1.14    chopps 		goto out;
    874  1.14    chopps 
    875  1.14    chopps 	/*
    876  1.14    chopps 	 * clear interrupt
    877  1.14    chopps 	 */
    878  1.14    chopps 	custom.intreq = INTF_TBE;
    879  1.14    chopps 
    880  1.14    chopps 	if (sob_ptr == sob_end) {
    881  1.14    chopps 		tp->t_state &= ~(TS_BUSY | TS_FLUSH);
    882  1.55   aymeric 		if (tp->t_linesw)
    883  1.55   aymeric 			tp->t_linesw->l_start(tp);
    884  1.14    chopps 		else
    885  1.14    chopps 			serstart(tp);
    886  1.14    chopps 		goto out;
    887  1.14    chopps 	}
    888  1.14    chopps 
    889  1.14    chopps 	/*
    890  1.14    chopps 	 * Do hardware flow control here.  if the CTS line goes down, don't
    891  1.14    chopps 	 * transmit anything.  That way, we'll be restarted by the periodic
    892  1.59   aymeric 	 * interrupt when CTS comes back up.
    893  1.14    chopps 	 */
    894  1.14    chopps 	if (ISCTS(ciab.pra))
    895  1.14    chopps 		ser_putchar(tp, *sob_ptr++);
    896  1.22    chopps 	else
    897  1.22    chopps 		CLRCTS(last_ciab_pra);	/* Remember that CTS is off */
    898   1.5        mw out:
    899  1.14    chopps 	splx(s);
    900   1.3        mw }
    901  1.14    chopps 
    902  1.31     veego void
    903  1.59   aymeric serstart(struct tty *tp)
    904   1.1        mw {
    905  1.44        is 	int cc, s, hiwat;
    906  1.44        is #ifdef DIAGNOSTIC
    907  1.44        is 	int unit;
    908  1.44        is #endif
    909  1.59   aymeric 
    910  1.14    chopps 	hiwat = 0;
    911  1.14    chopps 
    912  1.14    chopps 	if ((tp->t_state & TS_ISOPEN) == 0)
    913  1.14    chopps 		return;
    914  1.14    chopps 
    915  1.44        is #ifdef DIAGNOSTIC
    916  1.14    chopps 	unit = SERUNIT(tp->t_dev);
    917  1.44        is 	if (unit)
    918  1.64    provos 		panic("serstart: unit is %d", unit);
    919  1.44        is #endif
    920   1.1        mw 
    921  1.14    chopps 	s = spltty();
    922  1.14    chopps 	if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))
    923  1.14    chopps 		goto out;
    924  1.14    chopps 
    925  1.14    chopps 	cc = tp->t_outq.c_cc;
    926  1.14    chopps 	if (cc <= tp->t_lowat) {
    927  1.14    chopps 		if (tp->t_state & TS_ASLEEP) {
    928  1.14    chopps 			tp->t_state &= ~TS_ASLEEP;
    929  1.14    chopps 			wakeup((caddr_t) & tp->t_outq);
    930  1.14    chopps 		}
    931  1.14    chopps 		selwakeup(&tp->t_wsel);
    932  1.14    chopps 	}
    933  1.14    chopps 	if (cc == 0 || (tp->t_state & TS_BUSY))
    934  1.14    chopps 		goto out;
    935  1.14    chopps 
    936  1.14    chopps 	/*
    937  1.14    chopps 	 * We only do bulk transfers if using CTSRTS flow control, not for
    938  1.14    chopps 	 * (probably sloooow) ixon/ixoff devices.
    939  1.14    chopps 	 */
    940  1.14    chopps 	if ((tp->t_cflag & CRTSCTS) == 0)
    941  1.14    chopps 		cc = 1;
    942  1.14    chopps 
    943  1.14    chopps 	/*
    944  1.14    chopps 	 * Limit the amount of output we do in one burst
    945  1.14    chopps 	 * to prevent hogging the CPU.
    946  1.14    chopps 	 */
    947  1.14    chopps 	if (cc > SEROBUF_SIZE) {
    948  1.14    chopps 		hiwat++;
    949  1.14    chopps 		cc = SEROBUF_SIZE;
    950  1.14    chopps 	}
    951  1.14    chopps 	cc = q_to_b(&tp->t_outq, ser_outbuf, cc);
    952  1.14    chopps 	if (cc > 0) {
    953  1.14    chopps 		tp->t_state |= TS_BUSY;
    954  1.14    chopps 
    955  1.14    chopps 		sob_ptr = ser_outbuf;
    956  1.14    chopps 		sob_end = ser_outbuf + cc;
    957  1.14    chopps 
    958  1.14    chopps 		/*
    959  1.14    chopps 		 * Get first character out, then have TBE-interrupts blow out
    960  1.14    chopps 		 * further characters, until buffer is empty, and TS_BUSY gets
    961  1.59   aymeric 		 * cleared.
    962  1.14    chopps 		 */
    963  1.14    chopps 		ser_putchar(tp, *sob_ptr++);
    964  1.14    chopps 	}
    965  1.14    chopps out:
    966  1.14    chopps 	splx(s);
    967   1.1        mw }
    968  1.14    chopps 
    969   1.1        mw /*
    970   1.1        mw  * Stop output on a line.
    971   1.1        mw  */
    972   1.1        mw /*ARGSUSED*/
    973  1.36   mycroft void
    974  1.59   aymeric serstop(struct tty *tp, int flag)
    975   1.1        mw {
    976  1.14    chopps 	int s;
    977   1.1        mw 
    978  1.14    chopps 	s = spltty();
    979  1.14    chopps 	if (tp->t_state & TS_BUSY) {
    980  1.14    chopps 		if ((tp->t_state & TS_TTSTOP) == 0)
    981  1.14    chopps 			tp->t_state |= TS_FLUSH;
    982  1.14    chopps 	}
    983  1.14    chopps 	splx(s);
    984   1.1        mw }
    985  1.14    chopps 
    986   1.5        mw int
    987  1.59   aymeric sermctl(dev_t dev, int bits, int how)
    988   1.1        mw {
    989  1.44        is 	int s;
    990  1.31     veego 	u_char ub = 0;
    991  1.14    chopps 
    992  1.14    chopps 	/*
    993  1.14    chopps 	 * convert TIOCM* mask into CIA mask
    994  1.14    chopps 	 * which is active low
    995  1.14    chopps 	 */
    996  1.14    chopps 	if (how != DMGET) {
    997  1.14    chopps 		ub = 0;
    998  1.14    chopps 		if (bits & TIOCM_DTR)
    999  1.14    chopps 			ub |= CIAB_PRA_DTR;
   1000  1.14    chopps 		if (bits & TIOCM_RTS)
   1001  1.14    chopps 			ub |= CIAB_PRA_RTS;
   1002  1.14    chopps 		if (bits & TIOCM_CTS)
   1003  1.14    chopps 			ub |= CIAB_PRA_CTS;
   1004  1.14    chopps 		if (bits & TIOCM_CD)
   1005  1.14    chopps 			ub |= CIAB_PRA_CD;
   1006  1.14    chopps 		if (bits & TIOCM_RI)
   1007  1.14    chopps 			ub |= CIAB_PRA_SEL;	/* collision with /dev/par ! */
   1008  1.14    chopps 		if (bits & TIOCM_DSR)
   1009  1.14    chopps 			ub |= CIAB_PRA_DSR;
   1010  1.14    chopps 	}
   1011  1.14    chopps 	s = spltty();
   1012  1.14    chopps 	switch (how) {
   1013  1.14    chopps 	case DMSET:
   1014  1.14    chopps 		/* invert and set */
   1015  1.14    chopps 		ciab.pra = ~ub;
   1016  1.14    chopps 		break;
   1017  1.14    chopps 
   1018  1.14    chopps 	case DMBIC:
   1019  1.14    chopps 		ciab.pra |= ub;
   1020  1.14    chopps 		ub = ~ciab.pra;
   1021  1.14    chopps 		break;
   1022  1.14    chopps 
   1023  1.14    chopps 	case DMBIS:
   1024  1.14    chopps 		ciab.pra &= ~ub;
   1025  1.14    chopps 		ub = ~ciab.pra;
   1026  1.14    chopps 		break;
   1027  1.14    chopps 
   1028  1.14    chopps 	case DMGET:
   1029  1.14    chopps 		ub = ~ciab.pra;
   1030  1.14    chopps 		break;
   1031  1.14    chopps 	}
   1032  1.14    chopps 	(void)splx(s);
   1033  1.14    chopps 
   1034  1.14    chopps 	bits = 0;
   1035  1.14    chopps 	if (ub & CIAB_PRA_DTR)
   1036  1.14    chopps 		bits |= TIOCM_DTR;
   1037  1.14    chopps 	if (ub & CIAB_PRA_RTS)
   1038  1.14    chopps 		bits |= TIOCM_RTS;
   1039  1.14    chopps 	if (ub & CIAB_PRA_CTS)
   1040  1.14    chopps 		bits |= TIOCM_CTS;
   1041  1.14    chopps 	if (ub & CIAB_PRA_CD)
   1042  1.14    chopps 		bits |= TIOCM_CD;
   1043  1.14    chopps 	if (ub & CIAB_PRA_SEL)
   1044  1.14    chopps 		bits |= TIOCM_RI;
   1045  1.14    chopps 	if (ub & CIAB_PRA_DSR)
   1046  1.14    chopps 		bits |= TIOCM_DSR;
   1047  1.14    chopps 
   1048  1.14    chopps 	return(bits);
   1049   1.1        mw }
   1050   1.1        mw 
   1051   1.1        mw /*
   1052   1.1        mw  * Following are all routines needed for SER to act as console
   1053   1.1        mw  */
   1054  1.31     veego void
   1055  1.59   aymeric sercnprobe(struct consdev *cp)
   1056   1.1        mw {
   1057  1.63   gehenna 	int maj, unit;
   1058  1.63   gehenna #ifdef KGDB
   1059  1.63   gehenna 	extern const struct cdevsw ctty_cdevsw;
   1060  1.63   gehenna #endif
   1061  1.59   aymeric 
   1062  1.14    chopps 	/* locate the major number */
   1063  1.63   gehenna 	maj = cdevsw_lookup_major(&ser_cdevsw);
   1064  1.14    chopps 
   1065  1.59   aymeric 
   1066  1.14    chopps 	unit = CONUNIT;			/* XXX: ick */
   1067  1.14    chopps 
   1068  1.14    chopps 	/*
   1069  1.14    chopps 	 * initialize required fields
   1070  1.14    chopps 	 */
   1071  1.63   gehenna 	cp->cn_dev = makedev(maj, unit);
   1072  1.14    chopps 	if (serconsole == unit)
   1073  1.14    chopps 		cp->cn_pri = CN_REMOTE;
   1074  1.59   aymeric 	else
   1075  1.14    chopps 		cp->cn_pri = CN_NORMAL;
   1076   1.1        mw #ifdef KGDB
   1077  1.63   gehenna 	/* XXX */
   1078  1.63   gehenna 	if (cdevsw_lookup(kgdb_dev) == &ctty_cdevsw)
   1079  1.63   gehenna 		kgdb_dev = makedev(maj, minor(kgdb_dev));
   1080   1.1        mw #endif
   1081   1.1        mw }
   1082   1.1        mw 
   1083  1.31     veego void
   1084  1.59   aymeric sercninit(struct consdev *cp)
   1085   1.1        mw {
   1086  1.14    chopps 	int unit;
   1087   1.1        mw 
   1088  1.14    chopps 	unit = SERUNIT(cp->cn_dev);
   1089  1.14    chopps 
   1090  1.44        is 	serinit(serdefaultrate);
   1091  1.14    chopps 	serconsole = unit;
   1092  1.14    chopps 	serconsinit = 1;
   1093   1.1        mw }
   1094   1.1        mw 
   1095  1.31     veego void
   1096  1.59   aymeric serinit(int rate)
   1097   1.1        mw {
   1098  1.14    chopps 	int s;
   1099   1.1        mw 
   1100  1.26    chopps 	s = splser();
   1101  1.14    chopps 	/*
   1102  1.14    chopps 	 * might want to fiddle with the CIA later ???
   1103  1.14    chopps 	 */
   1104  1.32        is 	custom.serper = (rate>=110 ? SERBRD(rate) : 0);
   1105  1.14    chopps 	splx(s);
   1106   1.1        mw }
   1107   1.1        mw 
   1108  1.31     veego int
   1109  1.59   aymeric sercngetc(dev_t dev)
   1110   1.1        mw {
   1111  1.14    chopps 	u_short stat;
   1112  1.14    chopps 	int c, s;
   1113   1.1        mw 
   1114  1.26    chopps 	s = splser();
   1115  1.14    chopps 	/*
   1116  1.14    chopps 	 * poll
   1117  1.14    chopps 	 */
   1118  1.14    chopps 	while (((stat = custom.serdatr & 0xffff) & SERDATRF_RBF) == 0)
   1119  1.14    chopps 		;
   1120  1.66   aymeric 
   1121  1.14    chopps 	c = stat & 0xff;
   1122  1.14    chopps 	/*
   1123  1.14    chopps 	 * clear interrupt
   1124  1.14    chopps 	 */
   1125  1.14    chopps 	custom.intreq = INTF_RBF;
   1126  1.14    chopps 	splx(s);
   1127  1.66   aymeric 
   1128  1.14    chopps 	return(c);
   1129   1.1        mw }
   1130   1.1        mw 
   1131   1.1        mw /*
   1132   1.1        mw  * Console kernel output character routine.
   1133   1.1        mw  */
   1134  1.31     veego void
   1135  1.59   aymeric sercnputc(dev_t dev, int c)
   1136   1.1        mw {
   1137  1.14    chopps 	register int timo;
   1138  1.14    chopps 	int s;
   1139  1.14    chopps 
   1140  1.14    chopps 	s = splhigh();
   1141   1.1        mw 
   1142  1.14    chopps 	if (serconsinit == 0) {
   1143  1.44        is 		(void)serinit(serdefaultrate);
   1144  1.14    chopps 		serconsinit = 1;
   1145  1.14    chopps 	}
   1146  1.14    chopps 
   1147  1.14    chopps 	/*
   1148  1.59   aymeric 	 * wait for any pending transmission to finish
   1149  1.14    chopps 	 */
   1150  1.14    chopps 	timo = 50000;
   1151  1.14    chopps 	while (!(custom.serdatr & SERDATRF_TBE) && --timo);
   1152  1.14    chopps 
   1153  1.14    chopps 	/*
   1154  1.14    chopps 	 * transmit char.
   1155  1.14    chopps 	 */
   1156  1.14    chopps 	custom.serdat = (c & 0xff) | 0x100;
   1157  1.14    chopps 
   1158  1.59   aymeric 	/*
   1159  1.14    chopps 	 * wait for this transmission to complete
   1160  1.14    chopps 	 */
   1161  1.14    chopps 	timo = 1500000;
   1162  1.14    chopps 	while (!(custom.serdatr & SERDATRF_TBE) && --timo)
   1163  1.14    chopps 		;
   1164  1.14    chopps 
   1165  1.14    chopps 	/*
   1166  1.14    chopps 	 * Wait for the device (my vt100..) to process the data, since we
   1167  1.14    chopps 	 * don't do flow-control with cnputc
   1168  1.14    chopps 	 */
   1169  1.14    chopps 	for (timo = 0; timo < 30000; timo++)
   1170  1.14    chopps 		;
   1171  1.14    chopps 
   1172  1.59   aymeric 	/*
   1173  1.51   aymeric 	 * We set TBE so that ser_outintr() is called right after to check
   1174  1.51   aymeric 	 * whether there still are chars to process.
   1175  1.51   aymeric 	 * We used to clear this, but it hung the tty output if the kernel
   1176  1.51   aymeric 	 * output a char while userland did on the same serial port.
   1177  1.14    chopps 	 */
   1178  1.51   aymeric 	custom.intreq = INTF_SETCLR | INTF_TBE;
   1179  1.14    chopps 	splx(s);
   1180  1.25    chopps }
   1181  1.25    chopps 
   1182  1.25    chopps void
   1183  1.59   aymeric sercnpollc(dev_t dev, int on)
   1184  1.25    chopps {
   1185   1.1        mw }
   1186   1.1        mw #endif
   1187