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