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