Home | History | Annotate | Line # | Download | only in dec
dz.c revision 1.1
      1  1.1  ad /*	$NetBSD: dz.c,v 1.1 2002/02/25 14:58:08 ad Exp $	*/
      2  1.1  ad /*
      3  1.1  ad  * Copyright (c) 1996  Ken C. Wellsch.  All rights reserved.
      4  1.1  ad  * Copyright (c) 1992, 1993
      5  1.1  ad  *	The Regents of the University of California.  All rights reserved.
      6  1.1  ad  *
      7  1.1  ad  * This code is derived from software contributed to Berkeley by
      8  1.1  ad  * Ralph Campbell and Rick Macklem.
      9  1.1  ad  *
     10  1.1  ad  * Redistribution and use in source and binary forms, with or without
     11  1.1  ad  * modification, are permitted provided that the following conditions
     12  1.1  ad  * are met:
     13  1.1  ad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  ad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  ad  *    documentation and/or other materials provided with the distribution.
     18  1.1  ad  * 3. All advertising materials mentioning features or use of this software
     19  1.1  ad  *    must display the following acknowledgement:
     20  1.1  ad  *	This product includes software developed by the University of
     21  1.1  ad  *	California, Berkeley and its contributors.
     22  1.1  ad  * 4. Neither the name of the University nor the names of its contributors
     23  1.1  ad  *    may be used to endorse or promote products derived from this software
     24  1.1  ad  *    without specific prior written permission.
     25  1.1  ad  *
     26  1.1  ad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1  ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1  ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1  ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1  ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1  ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1  ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1  ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1  ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1  ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1  ad  * SUCH DAMAGE.
     37  1.1  ad  */
     38  1.1  ad 
     39  1.1  ad #include <sys/cdefs.h>
     40  1.1  ad __KERNEL_RCSID(0, "$NetBSD: dz.c,v 1.1 2002/02/25 14:58:08 ad Exp $");
     41  1.1  ad 
     42  1.1  ad #include "opt_ddb.h"
     43  1.1  ad 
     44  1.1  ad #include <sys/param.h>
     45  1.1  ad #include <sys/systm.h>
     46  1.1  ad #include <sys/callout.h>
     47  1.1  ad #include <sys/ioctl.h>
     48  1.1  ad #include <sys/tty.h>
     49  1.1  ad #include <sys/proc.h>
     50  1.1  ad #include <sys/map.h>
     51  1.1  ad #include <sys/buf.h>
     52  1.1  ad #include <sys/conf.h>
     53  1.1  ad #include <sys/file.h>
     54  1.1  ad #include <sys/uio.h>
     55  1.1  ad #include <sys/kernel.h>
     56  1.1  ad #include <sys/syslog.h>
     57  1.1  ad #include <sys/device.h>
     58  1.1  ad 
     59  1.1  ad #include <machine/bus.h>
     60  1.1  ad 
     61  1.1  ad #include <dev/dec/dzreg.h>
     62  1.1  ad #include <dev/dec/dzvar.h>
     63  1.1  ad 
     64  1.1  ad #define	DZ_READ_BYTE(adr) \
     65  1.1  ad 	bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_dr.adr)
     66  1.1  ad #define	DZ_READ_WORD(adr) \
     67  1.1  ad 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, sc->sc_dr.adr)
     68  1.1  ad #define	DZ_WRITE_BYTE(adr, val) \
     69  1.1  ad 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_dr.adr, val)
     70  1.1  ad #define	DZ_WRITE_WORD(adr, val) \
     71  1.1  ad 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, sc->sc_dr.adr, val)
     72  1.1  ad 
     73  1.1  ad #include "ioconf.h"
     74  1.1  ad 
     75  1.1  ad /* A DZ-11 has 8 ports while a DZV/DZQ-11 has only 4. We use 8 by default */
     76  1.1  ad 
     77  1.1  ad #define	NDZLINE 	8
     78  1.1  ad 
     79  1.1  ad #define DZ_C2I(c)	((c)<<3)	/* convert controller # to index */
     80  1.1  ad #define DZ_I2C(c)	((c)>>3)	/* convert minor to controller # */
     81  1.1  ad #define DZ_PORT(u)	((u)&07)	/* extract the port # */
     82  1.1  ad 
     83  1.1  ad /* Flags used to monitor modem bits, make them understood outside driver */
     84  1.1  ad 
     85  1.1  ad #define DML_DTR		TIOCM_DTR
     86  1.1  ad #define DML_DCD		TIOCM_CD
     87  1.1  ad #define DML_RI		TIOCM_RI
     88  1.1  ad #define DML_BRK		0100000		/* no equivalent, we will mask */
     89  1.1  ad 
     90  1.1  ad static struct speedtab dzspeedtab[] =
     91  1.1  ad {
     92  1.1  ad   {       0,	0		},
     93  1.1  ad   {      50,	DZ_LPR_B50	},
     94  1.1  ad   {      75,	DZ_LPR_B75	},
     95  1.1  ad   {     110,	DZ_LPR_B110	},
     96  1.1  ad   {     134,	DZ_LPR_B134	},
     97  1.1  ad   {     150,	DZ_LPR_B150	},
     98  1.1  ad   {     300,	DZ_LPR_B300	},
     99  1.1  ad   {     600,	DZ_LPR_B600	},
    100  1.1  ad   {    1200,	DZ_LPR_B1200	},
    101  1.1  ad   {    1800,	DZ_LPR_B1800	},
    102  1.1  ad   {    2000,	DZ_LPR_B2000	},
    103  1.1  ad   {    2400,	DZ_LPR_B2400	},
    104  1.1  ad   {    3600,	DZ_LPR_B3600	},
    105  1.1  ad   {    4800,	DZ_LPR_B4800	},
    106  1.1  ad   {    7200,	DZ_LPR_B7200	},
    107  1.1  ad   {    9600,	DZ_LPR_B9600	},
    108  1.1  ad   {   19200,	DZ_LPR_B19200	},
    109  1.1  ad   {      -1,	-1		}
    110  1.1  ad };
    111  1.1  ad 
    112  1.1  ad static void	dzstart(struct tty *);
    113  1.1  ad static int	dzparam(struct tty *, struct termios *);
    114  1.1  ad static unsigned	dzmctl(struct dz_softc *, int, int, int);
    115  1.1  ad static void	dzscan(void *);
    116  1.1  ad cdev_decl(dz);
    117  1.1  ad 
    118  1.1  ad /*
    119  1.1  ad  * The DZ series doesn't interrupt on carrier transitions,
    120  1.1  ad  * so we have to use a timer to watch it.
    121  1.1  ad  */
    122  1.1  ad int	dz_timer;	/* true if timer started */
    123  1.1  ad struct callout dzscan_ch;
    124  1.1  ad 
    125  1.1  ad #define DZ_DZ	8		/* Unibus DZ-11 board linecount */
    126  1.1  ad #define DZ_DZV	4		/* Q-bus DZV-11 or DZQ-11 */
    127  1.1  ad 
    128  1.1  ad void
    129  1.1  ad dzattach(struct dz_softc *sc, struct evcnt *parent_evcnt)
    130  1.1  ad {
    131  1.1  ad 	int n;
    132  1.1  ad 
    133  1.1  ad 	sc->sc_rxint = sc->sc_brk = 0;
    134  1.1  ad 
    135  1.1  ad 	sc->sc_dr.dr_tcrw = sc->sc_dr.dr_tcr;
    136  1.1  ad 	DZ_WRITE_WORD(dr_csr, DZ_CSR_MSE | DZ_CSR_RXIE | DZ_CSR_TXIE);
    137  1.1  ad 	DZ_WRITE_BYTE(dr_dtr, 0);
    138  1.1  ad 	DZ_WRITE_BYTE(dr_break, 0);
    139  1.1  ad 
    140  1.1  ad 	/* Initialize our softc structure. Should be done in open? */
    141  1.1  ad 
    142  1.1  ad 	for (n = 0; n < sc->sc_type; n++)
    143  1.1  ad 		sc->sc_dz[n].dz_tty = ttymalloc();
    144  1.1  ad 
    145  1.1  ad 	evcnt_attach_dynamic(&sc->sc_rintrcnt, EVCNT_TYPE_INTR, parent_evcnt,
    146  1.1  ad 		sc->sc_dev.dv_xname, "rintr");
    147  1.1  ad 	evcnt_attach_dynamic(&sc->sc_tintrcnt, EVCNT_TYPE_INTR, parent_evcnt,
    148  1.1  ad 		sc->sc_dev.dv_xname, "tintr");
    149  1.1  ad 
    150  1.1  ad 	/* Alas no interrupt on modem bit changes, so we manually scan */
    151  1.1  ad 
    152  1.1  ad 	if (dz_timer == 0) {
    153  1.1  ad 		dz_timer = 1;
    154  1.1  ad 		callout_init(&dzscan_ch);
    155  1.1  ad 		callout_reset(&dzscan_ch, hz, dzscan, NULL);
    156  1.1  ad 	}
    157  1.1  ad 	printf("\n");
    158  1.1  ad 	return;
    159  1.1  ad }
    160  1.1  ad 
    161  1.1  ad /* Receiver Interrupt */
    162  1.1  ad 
    163  1.1  ad void
    164  1.1  ad dzrint(void *arg)
    165  1.1  ad {
    166  1.1  ad 	struct dz_softc *sc = arg;
    167  1.1  ad 	struct tty *tp;
    168  1.1  ad 	int cc, line;
    169  1.1  ad 	unsigned c;
    170  1.1  ad 	int overrun = 0;
    171  1.1  ad 
    172  1.1  ad 	sc->sc_rxint++;
    173  1.1  ad 
    174  1.1  ad 	while ((c = DZ_READ_WORD(dr_rbuf)) & DZ_RBUF_DATA_VALID) {
    175  1.1  ad 		cc = c & 0xFF;
    176  1.1  ad 		line = DZ_PORT(c>>8);
    177  1.1  ad 		tp = sc->sc_dz[line].dz_tty;
    178  1.1  ad 
    179  1.1  ad 		/* Must be caught early */
    180  1.1  ad 		if (sc->sc_dz[line].dz_catch &&
    181  1.1  ad 		    (*sc->sc_dz[line].dz_catch)(sc->sc_dz[line].dz_private, cc))
    182  1.1  ad 			continue;
    183  1.1  ad 
    184  1.1  ad 		if (!(tp->t_state & TS_ISOPEN)) {
    185  1.1  ad 			wakeup((caddr_t)&tp->t_rawq);
    186  1.1  ad 			continue;
    187  1.1  ad 		}
    188  1.1  ad 
    189  1.1  ad 		if ((c & DZ_RBUF_OVERRUN_ERR) && overrun == 0) {
    190  1.1  ad 			log(LOG_WARNING, "%s: silo overflow, line %d\n",
    191  1.1  ad 			    sc->sc_dev.dv_xname, line);
    192  1.1  ad 			overrun = 1;
    193  1.1  ad 		}
    194  1.1  ad 
    195  1.1  ad 		/* A BREAK key will appear as a NULL with a framing error */
    196  1.1  ad 		if (c & DZ_RBUF_FRAMING_ERR)
    197  1.1  ad 			cc |= TTY_FE;
    198  1.1  ad 		if (c & DZ_RBUF_PARITY_ERR)
    199  1.1  ad 			cc |= TTY_PE;
    200  1.1  ad 
    201  1.1  ad 		(*tp->t_linesw->l_rint)(cc, tp);
    202  1.1  ad 	}
    203  1.1  ad }
    204  1.1  ad 
    205  1.1  ad /* Transmitter Interrupt */
    206  1.1  ad 
    207  1.1  ad void
    208  1.1  ad dzxint(void *arg)
    209  1.1  ad {
    210  1.1  ad 	struct dz_softc *sc = arg;
    211  1.1  ad 	struct tty *tp;
    212  1.1  ad 	struct clist *cl;
    213  1.1  ad 	int line, ch, csr;
    214  1.1  ad 	u_char tcr;
    215  1.1  ad 
    216  1.1  ad 	/*
    217  1.1  ad 	 * Switch to POLLED mode.
    218  1.1  ad 	 *   Some simple measurements indicated that even on
    219  1.1  ad 	 *  one port, by freeing the scanner in the controller
    220  1.1  ad 	 *  by either providing a character or turning off
    221  1.1  ad 	 *  the port when output is complete, the transmitter
    222  1.1  ad 	 *  was ready to accept more output when polled again.
    223  1.1  ad 	 *   With just two ports running the game "worms,"
    224  1.1  ad 	 *  almost every interrupt serviced both transmitters!
    225  1.1  ad 	 *   Each UART is double buffered, so if the scanner
    226  1.1  ad 	 *  is quick enough and timing works out, we can even
    227  1.1  ad 	 *  feed the same port twice.
    228  1.1  ad 	 *
    229  1.1  ad 	 * Ragge 980517:
    230  1.1  ad 	 * Do not need to turn off interrupts, already at interrupt level.
    231  1.1  ad 	 * Remove the pdma stuff; no great need of it right now.
    232  1.1  ad 	 */
    233  1.1  ad 
    234  1.1  ad 	while (((csr = DZ_READ_WORD(dr_csr)) & DZ_CSR_TX_READY) != 0) {
    235  1.1  ad 
    236  1.1  ad 		line = DZ_PORT(csr>>8);
    237  1.1  ad 
    238  1.1  ad 		tp = sc->sc_dz[line].dz_tty;
    239  1.1  ad 		cl = &tp->t_outq;
    240  1.1  ad 		tp->t_state &= ~TS_BUSY;
    241  1.1  ad 
    242  1.1  ad 		/* Just send out a char if we have one */
    243  1.1  ad 		/* As long as we can fill the chip buffer, we just loop here */
    244  1.1  ad 		if (cl->c_cc) {
    245  1.1  ad 			tp->t_state |= TS_BUSY;
    246  1.1  ad 			ch = getc(cl);
    247  1.1  ad 			DZ_WRITE_BYTE(dr_tbuf, ch);
    248  1.1  ad 			continue;
    249  1.1  ad 		}
    250  1.1  ad 		/* Nothing to send; clear the scan bit */
    251  1.1  ad 		/* Clear xmit scanner bit; dzstart may set it again */
    252  1.1  ad 		tcr = DZ_READ_WORD(dr_tcrw);
    253  1.1  ad 		tcr &= 255;
    254  1.1  ad 		tcr &= ~(1 << line);
    255  1.1  ad 		DZ_WRITE_BYTE(dr_tcr, tcr);
    256  1.1  ad 		if (sc->sc_dz[line].dz_catch)
    257  1.1  ad 			continue;
    258  1.1  ad 
    259  1.1  ad 		if (tp->t_state & TS_FLUSH)
    260  1.1  ad 			tp->t_state &= ~TS_FLUSH;
    261  1.1  ad 		else
    262  1.1  ad 			ndflush (&tp->t_outq, cl->c_cc);
    263  1.1  ad 
    264  1.1  ad 		(*tp->t_linesw->l_start)(tp);
    265  1.1  ad 	}
    266  1.1  ad }
    267  1.1  ad 
    268  1.1  ad int
    269  1.1  ad dzopen(dev_t dev, int flag, int mode, struct proc *p)
    270  1.1  ad {
    271  1.1  ad 	struct tty *tp;
    272  1.1  ad 	int unit, line;
    273  1.1  ad 	struct	dz_softc *sc;
    274  1.1  ad 	int s, error = 0;
    275  1.1  ad 
    276  1.1  ad 	unit = DZ_I2C(minor(dev));
    277  1.1  ad 	line = DZ_PORT(minor(dev));
    278  1.1  ad 	if (unit >= dz_cd.cd_ndevs ||  dz_cd.cd_devs[unit] == NULL)
    279  1.1  ad 		return (ENXIO);
    280  1.1  ad 
    281  1.1  ad 	sc = dz_cd.cd_devs[unit];
    282  1.1  ad 
    283  1.1  ad 	if (line >= sc->sc_type)
    284  1.1  ad 		return ENXIO;
    285  1.1  ad 
    286  1.1  ad 	/* if some other device is using the line, it's busy */
    287  1.1  ad 	if (sc->sc_dz[line].dz_catch)
    288  1.1  ad 		return EBUSY;
    289  1.1  ad 
    290  1.1  ad 	tp = sc->sc_dz[line].dz_tty;
    291  1.1  ad 	if (tp == NULL)
    292  1.1  ad 		return (ENODEV);
    293  1.1  ad 	tp->t_oproc   = dzstart;
    294  1.1  ad 	tp->t_param   = dzparam;
    295  1.1  ad 	tp->t_dev = dev;
    296  1.1  ad 	if ((tp->t_state & TS_ISOPEN) == 0) {
    297  1.1  ad 		ttychars(tp);
    298  1.1  ad 		if (tp->t_ispeed == 0) {
    299  1.1  ad 			tp->t_iflag = TTYDEF_IFLAG;
    300  1.1  ad 			tp->t_oflag = TTYDEF_OFLAG;
    301  1.1  ad 			tp->t_cflag = TTYDEF_CFLAG;
    302  1.1  ad 			tp->t_lflag = TTYDEF_LFLAG;
    303  1.1  ad 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    304  1.1  ad 		}
    305  1.1  ad 		(void) dzparam(tp, &tp->t_termios);
    306  1.1  ad 		ttsetwater(tp);
    307  1.1  ad 	} else if ((tp->t_state & TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    308  1.1  ad 		return (EBUSY);
    309  1.1  ad 	/* Use DMBIS and *not* DMSET or else we clobber incoming bits */
    310  1.1  ad 	if (dzmctl(sc, line, DML_DTR, DMBIS) & DML_DCD)
    311  1.1  ad 		tp->t_state |= TS_CARR_ON;
    312  1.1  ad 	s = spltty();
    313  1.1  ad 	while (!(flag & O_NONBLOCK) && !(tp->t_cflag & CLOCAL) &&
    314  1.1  ad 	       !(tp->t_state & TS_CARR_ON)) {
    315  1.1  ad 		tp->t_wopen++;
    316  1.1  ad 		error = ttysleep(tp, (caddr_t)&tp->t_rawq,
    317  1.1  ad 				TTIPRI | PCATCH, ttopen, 0);
    318  1.1  ad 		tp->t_wopen--;
    319  1.1  ad 		if (error)
    320  1.1  ad 			break;
    321  1.1  ad 	}
    322  1.1  ad 	(void) splx(s);
    323  1.1  ad 	if (error)
    324  1.1  ad 		return (error);
    325  1.1  ad 	return ((*tp->t_linesw->l_open)(dev, tp));
    326  1.1  ad }
    327  1.1  ad 
    328  1.1  ad /*ARGSUSED*/
    329  1.1  ad int
    330  1.1  ad dzclose(dev_t dev, int flag, int mode, struct proc *p)
    331  1.1  ad {
    332  1.1  ad 	struct	dz_softc *sc;
    333  1.1  ad 	struct tty *tp;
    334  1.1  ad 	int unit, line;
    335  1.1  ad 
    336  1.1  ad 
    337  1.1  ad 	unit = DZ_I2C(minor(dev));
    338  1.1  ad 	line = DZ_PORT(minor(dev));
    339  1.1  ad 	sc = dz_cd.cd_devs[unit];
    340  1.1  ad 
    341  1.1  ad 	tp = sc->sc_dz[line].dz_tty;
    342  1.1  ad 
    343  1.1  ad 	(*tp->t_linesw->l_close)(tp, flag);
    344  1.1  ad 
    345  1.1  ad 	/* Make sure a BREAK state is not left enabled. */
    346  1.1  ad 	(void) dzmctl(sc, line, DML_BRK, DMBIC);
    347  1.1  ad 
    348  1.1  ad 	/* Do a hangup if so required. */
    349  1.1  ad 	if ((tp->t_cflag & HUPCL) || tp->t_wopen || !(tp->t_state & TS_ISOPEN))
    350  1.1  ad 		(void) dzmctl(sc, line, 0, DMSET);
    351  1.1  ad 
    352  1.1  ad 	return (ttyclose(tp));
    353  1.1  ad }
    354  1.1  ad 
    355  1.1  ad int
    356  1.1  ad dzread(dev_t dev, struct uio *uio, int flag)
    357  1.1  ad {
    358  1.1  ad 	struct tty *tp;
    359  1.1  ad 	struct	dz_softc *sc;
    360  1.1  ad 
    361  1.1  ad 	sc = dz_cd.cd_devs[DZ_I2C(minor(dev))];
    362  1.1  ad 
    363  1.1  ad 	tp = sc->sc_dz[DZ_PORT(minor(dev))].dz_tty;
    364  1.1  ad 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    365  1.1  ad }
    366  1.1  ad 
    367  1.1  ad int
    368  1.1  ad dzwrite(dev_t dev, struct uio *uio, int flag)
    369  1.1  ad {
    370  1.1  ad 	struct tty *tp;
    371  1.1  ad 	struct	dz_softc *sc;
    372  1.1  ad 
    373  1.1  ad 	sc = dz_cd.cd_devs[DZ_I2C(minor(dev))];
    374  1.1  ad 
    375  1.1  ad 	tp = sc->sc_dz[DZ_PORT(minor(dev))].dz_tty;
    376  1.1  ad 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    377  1.1  ad }
    378  1.1  ad 
    379  1.1  ad int
    380  1.1  ad dzpoll(dev, events, p)
    381  1.1  ad 	dev_t dev;
    382  1.1  ad 	int events;
    383  1.1  ad 	struct proc *p;
    384  1.1  ad {
    385  1.1  ad 	struct tty *tp;
    386  1.1  ad 	struct	dz_softc *sc;
    387  1.1  ad 
    388  1.1  ad 	sc = dz_cd.cd_devs[DZ_I2C(minor(dev))];
    389  1.1  ad 
    390  1.1  ad 	tp = sc->sc_dz[DZ_PORT(minor(dev))].dz_tty;
    391  1.1  ad 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    392  1.1  ad }
    393  1.1  ad 
    394  1.1  ad /*ARGSUSED*/
    395  1.1  ad int
    396  1.1  ad dzioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    397  1.1  ad {
    398  1.1  ad 	struct	dz_softc *sc;
    399  1.1  ad 	struct tty *tp;
    400  1.1  ad 	int unit, line;
    401  1.1  ad 	int error;
    402  1.1  ad 
    403  1.1  ad 	unit = DZ_I2C(minor(dev));
    404  1.1  ad 	line = DZ_PORT(minor(dev));
    405  1.1  ad 	sc = dz_cd.cd_devs[unit];
    406  1.1  ad 	tp = sc->sc_dz[line].dz_tty;
    407  1.1  ad 
    408  1.1  ad 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    409  1.1  ad 	if (error >= 0)
    410  1.1  ad 		return (error);
    411  1.1  ad 	error = ttioctl(tp, cmd, data, flag, p);
    412  1.1  ad 	if (error >= 0)
    413  1.1  ad 		return (error);
    414  1.1  ad 
    415  1.1  ad 	switch (cmd) {
    416  1.1  ad 
    417  1.1  ad 	case TIOCSBRK:
    418  1.1  ad 		(void) dzmctl(sc, line, DML_BRK, DMBIS);
    419  1.1  ad 		break;
    420  1.1  ad 
    421  1.1  ad 	case TIOCCBRK:
    422  1.1  ad 		(void) dzmctl(sc, line, DML_BRK, DMBIC);
    423  1.1  ad 		break;
    424  1.1  ad 
    425  1.1  ad 	case TIOCSDTR:
    426  1.1  ad 		(void) dzmctl(sc, line, DML_DTR, DMBIS);
    427  1.1  ad 		break;
    428  1.1  ad 
    429  1.1  ad 	case TIOCCDTR:
    430  1.1  ad 		(void) dzmctl(sc, line, DML_DTR, DMBIC);
    431  1.1  ad 		break;
    432  1.1  ad 
    433  1.1  ad 	case TIOCMSET:
    434  1.1  ad 		(void) dzmctl(sc, line, *(int *)data, DMSET);
    435  1.1  ad 		break;
    436  1.1  ad 
    437  1.1  ad 	case TIOCMBIS:
    438  1.1  ad 		(void) dzmctl(sc, line, *(int *)data, DMBIS);
    439  1.1  ad 		break;
    440  1.1  ad 
    441  1.1  ad 	case TIOCMBIC:
    442  1.1  ad 		(void) dzmctl(sc, line, *(int *)data, DMBIC);
    443  1.1  ad 		break;
    444  1.1  ad 
    445  1.1  ad 	case TIOCMGET:
    446  1.1  ad 		*(int *)data = (dzmctl(sc, line, 0, DMGET) & ~DML_BRK);
    447  1.1  ad 		break;
    448  1.1  ad 
    449  1.1  ad 	default:
    450  1.1  ad 		return (ENOTTY);
    451  1.1  ad 	}
    452  1.1  ad 	return (0);
    453  1.1  ad }
    454  1.1  ad 
    455  1.1  ad struct tty *
    456  1.1  ad dztty(dev_t dev)
    457  1.1  ad {
    458  1.1  ad 	struct	dz_softc *sc = dz_cd.cd_devs[DZ_I2C(minor(dev))];
    459  1.1  ad         struct tty *tp = sc->sc_dz[DZ_PORT(minor(dev))].dz_tty;
    460  1.1  ad 
    461  1.1  ad         return (tp);
    462  1.1  ad }
    463  1.1  ad 
    464  1.1  ad /*ARGSUSED*/
    465  1.1  ad void
    466  1.1  ad dzstop(struct tty *tp, int flag)
    467  1.1  ad {
    468  1.1  ad 	if (tp->t_state & TS_BUSY)
    469  1.1  ad 		if (!(tp->t_state & TS_TTSTOP))
    470  1.1  ad 			tp->t_state |= TS_FLUSH;
    471  1.1  ad }
    472  1.1  ad 
    473  1.1  ad void
    474  1.1  ad dzstart(struct tty *tp)
    475  1.1  ad {
    476  1.1  ad 	struct dz_softc *sc;
    477  1.1  ad 	struct clist *cl;
    478  1.1  ad 	int unit, line, s;
    479  1.1  ad 	char state;
    480  1.1  ad 
    481  1.1  ad 	unit = DZ_I2C(minor(tp->t_dev));
    482  1.1  ad 	line = DZ_PORT(minor(tp->t_dev));
    483  1.1  ad 	sc = dz_cd.cd_devs[unit];
    484  1.1  ad 
    485  1.1  ad 	s = spltty();
    486  1.1  ad 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
    487  1.1  ad 		return;
    488  1.1  ad 	cl = &tp->t_outq;
    489  1.1  ad 	if (cl->c_cc <= tp->t_lowat) {
    490  1.1  ad 		if (tp->t_state & TS_ASLEEP) {
    491  1.1  ad 			tp->t_state &= ~TS_ASLEEP;
    492  1.1  ad 			wakeup((caddr_t)cl);
    493  1.1  ad 		}
    494  1.1  ad 		selwakeup(&tp->t_wsel);
    495  1.1  ad 	}
    496  1.1  ad 	if (cl->c_cc == 0)
    497  1.1  ad 		return;
    498  1.1  ad 
    499  1.1  ad 	tp->t_state |= TS_BUSY;
    500  1.1  ad 
    501  1.1  ad 	state = DZ_READ_WORD(dr_tcrw) & 255;
    502  1.1  ad 	if ((state & (1 << line)) == 0) {
    503  1.1  ad 		DZ_WRITE_BYTE(dr_tcr, state | (1 << line));
    504  1.1  ad 	}
    505  1.1  ad 	dzxint(sc);
    506  1.1  ad 	splx(s);
    507  1.1  ad }
    508  1.1  ad 
    509  1.1  ad static int
    510  1.1  ad dzparam(struct tty *tp, struct termios *t)
    511  1.1  ad {
    512  1.1  ad 	struct	dz_softc *sc;
    513  1.1  ad 	int cflag = t->c_cflag;
    514  1.1  ad 	int unit, line;
    515  1.1  ad 	int ispeed = ttspeedtab(t->c_ispeed, dzspeedtab);
    516  1.1  ad 	int ospeed = ttspeedtab(t->c_ospeed, dzspeedtab);
    517  1.1  ad 	unsigned lpr;
    518  1.1  ad 	int s;
    519  1.1  ad 
    520  1.1  ad 	unit = DZ_I2C(minor(tp->t_dev));
    521  1.1  ad 	line = DZ_PORT(minor(tp->t_dev));
    522  1.1  ad 	sc = dz_cd.cd_devs[unit];
    523  1.1  ad 
    524  1.1  ad 	/* check requested parameters */
    525  1.1  ad         if (ospeed < 0 || ispeed < 0 || ispeed != ospeed)
    526  1.1  ad                 return (EINVAL);
    527  1.1  ad 
    528  1.1  ad         tp->t_ispeed = t->c_ispeed;
    529  1.1  ad         tp->t_ospeed = t->c_ospeed;
    530  1.1  ad         tp->t_cflag = cflag;
    531  1.1  ad 
    532  1.1  ad 	if (ospeed == 0) {
    533  1.1  ad 		(void) dzmctl(sc, line, 0, DMSET);	/* hang up line */
    534  1.1  ad 		return (0);
    535  1.1  ad 	}
    536  1.1  ad 
    537  1.1  ad 	s = spltty();
    538  1.1  ad 
    539  1.1  ad 	lpr = DZ_LPR_RX_ENABLE | ((ispeed&0xF)<<8) | line;
    540  1.1  ad 
    541  1.1  ad 	switch (cflag & CSIZE)
    542  1.1  ad 	{
    543  1.1  ad 	  case CS5:
    544  1.1  ad 		lpr |= DZ_LPR_5_BIT_CHAR;
    545  1.1  ad 		break;
    546  1.1  ad 	  case CS6:
    547  1.1  ad 		lpr |= DZ_LPR_6_BIT_CHAR;
    548  1.1  ad 		break;
    549  1.1  ad 	  case CS7:
    550  1.1  ad 		lpr |= DZ_LPR_7_BIT_CHAR;
    551  1.1  ad 		break;
    552  1.1  ad 	  default:
    553  1.1  ad 		lpr |= DZ_LPR_8_BIT_CHAR;
    554  1.1  ad 		break;
    555  1.1  ad 	}
    556  1.1  ad 	if (cflag & PARENB)
    557  1.1  ad 		lpr |= DZ_LPR_PARENB;
    558  1.1  ad 	if (cflag & PARODD)
    559  1.1  ad 		lpr |= DZ_LPR_OPAR;
    560  1.1  ad 	if (cflag & CSTOPB)
    561  1.1  ad 		lpr |= DZ_LPR_2_STOP;
    562  1.1  ad 
    563  1.1  ad 	DZ_WRITE_WORD(dr_lpr, lpr);
    564  1.1  ad 
    565  1.1  ad 	(void) splx(s);
    566  1.1  ad 	return (0);
    567  1.1  ad }
    568  1.1  ad 
    569  1.1  ad static unsigned
    570  1.1  ad dzmctl(struct dz_softc *sc, int line, int bits, int how)
    571  1.1  ad {
    572  1.1  ad 	unsigned status;
    573  1.1  ad 	unsigned mbits;
    574  1.1  ad 	unsigned bit;
    575  1.1  ad 	int s;
    576  1.1  ad 
    577  1.1  ad 	s = spltty();
    578  1.1  ad 
    579  1.1  ad 	mbits = 0;
    580  1.1  ad 
    581  1.1  ad 	bit = (1 << line);
    582  1.1  ad 
    583  1.1  ad 	/* external signals as seen from the port */
    584  1.1  ad 
    585  1.1  ad 	status = DZ_READ_BYTE(dr_dcd) | sc->sc_dsr;
    586  1.1  ad 
    587  1.1  ad 	if (status & bit)
    588  1.1  ad 		mbits |= DML_DCD;
    589  1.1  ad 
    590  1.1  ad 	status = DZ_READ_BYTE(dr_ring);
    591  1.1  ad 
    592  1.1  ad 	if (status & bit)
    593  1.1  ad 		mbits |= DML_RI;
    594  1.1  ad 
    595  1.1  ad 	/* internal signals/state delivered to port */
    596  1.1  ad 
    597  1.1  ad 	status = DZ_READ_BYTE(dr_dtr);
    598  1.1  ad 
    599  1.1  ad 	if (status & bit)
    600  1.1  ad 		mbits |= DML_DTR;
    601  1.1  ad 
    602  1.1  ad 	if (sc->sc_brk & bit)
    603  1.1  ad 		mbits |= DML_BRK;
    604  1.1  ad 
    605  1.1  ad 	switch (how)
    606  1.1  ad 	{
    607  1.1  ad 	  case DMSET:
    608  1.1  ad 		mbits = bits;
    609  1.1  ad 		break;
    610  1.1  ad 
    611  1.1  ad 	  case DMBIS:
    612  1.1  ad 		mbits |= bits;
    613  1.1  ad 		break;
    614  1.1  ad 
    615  1.1  ad 	  case DMBIC:
    616  1.1  ad 		mbits &= ~bits;
    617  1.1  ad 		break;
    618  1.1  ad 
    619  1.1  ad 	  case DMGET:
    620  1.1  ad 		(void) splx(s);
    621  1.1  ad 		return (mbits);
    622  1.1  ad 	}
    623  1.1  ad 
    624  1.1  ad 	if (mbits & DML_DTR) {
    625  1.1  ad 		DZ_WRITE_BYTE(dr_dtr, DZ_READ_BYTE(dr_dtr) | bit);
    626  1.1  ad 	} else {
    627  1.1  ad 		DZ_WRITE_BYTE(dr_dtr, DZ_READ_BYTE(dr_dtr) & ~bit);
    628  1.1  ad 	}
    629  1.1  ad 
    630  1.1  ad 	if (mbits & DML_BRK) {
    631  1.1  ad 		sc->sc_brk |= bit;
    632  1.1  ad 		DZ_WRITE_BYTE(dr_break, sc->sc_brk);
    633  1.1  ad 	} else {
    634  1.1  ad 		sc->sc_brk &= ~bit;
    635  1.1  ad 		DZ_WRITE_BYTE(dr_break, sc->sc_brk);
    636  1.1  ad 	}
    637  1.1  ad 
    638  1.1  ad 	(void) splx(s);
    639  1.1  ad 	return (mbits);
    640  1.1  ad }
    641  1.1  ad 
    642  1.1  ad /*
    643  1.1  ad  * This is called by timeout() periodically.
    644  1.1  ad  * Check to see if modem status bits have changed.
    645  1.1  ad  */
    646  1.1  ad static void
    647  1.1  ad dzscan(void *arg)
    648  1.1  ad {
    649  1.1  ad 	struct dz_softc *sc;
    650  1.1  ad 	struct tty *tp;
    651  1.1  ad 	int n, bit, port;
    652  1.1  ad 	unsigned csr;
    653  1.1  ad 	int s;
    654  1.1  ad 
    655  1.1  ad 	s = spltty();
    656  1.1  ad 
    657  1.1  ad 	for (n = 0; n < dz_cd.cd_ndevs; n++) {
    658  1.1  ad 
    659  1.1  ad 		if (dz_cd.cd_devs[n] == NULL)
    660  1.1  ad 			continue;
    661  1.1  ad 
    662  1.1  ad 		sc = dz_cd.cd_devs[n];
    663  1.1  ad 
    664  1.1  ad 		for (port = 0; port < sc->sc_type; port++) {
    665  1.1  ad 
    666  1.1  ad 			tp = sc->sc_dz[port].dz_tty;
    667  1.1  ad 			bit = (1 << port);
    668  1.1  ad 
    669  1.1  ad 			if ((DZ_READ_BYTE(dr_dcd) | sc->sc_dsr) & bit) {
    670  1.1  ad 				if (!(tp->t_state & TS_CARR_ON))
    671  1.1  ad 					(*tp->t_linesw->l_modem) (tp, 1);
    672  1.1  ad 			} else if ((tp->t_state & TS_CARR_ON) &&
    673  1.1  ad 			    (*tp->t_linesw->l_modem)(tp, 0) == 0) {
    674  1.1  ad 				DZ_WRITE_BYTE(dr_tcr,
    675  1.1  ad 				    (DZ_READ_WORD(dr_tcrw) & 255) & ~bit);
    676  1.1  ad 			}
    677  1.1  ad 	    	}
    678  1.1  ad 
    679  1.1  ad 		/*
    680  1.1  ad 		 *  If the RX interrupt rate is this high, switch
    681  1.1  ad 		 *  the controller to Silo Alarm - which means don't
    682  1.1  ad 	 	 *  interrupt until the RX silo has 16 characters in
    683  1.1  ad 	 	 *  it (the silo is 64 characters in all).
    684  1.1  ad 		 *  Avoid oscillating SA on and off by not turning
    685  1.1  ad 		 *  if off unless the rate is appropriately low.
    686  1.1  ad 		 */
    687  1.1  ad 
    688  1.1  ad 		csr = DZ_READ_WORD(dr_csr);
    689  1.1  ad 
    690  1.1  ad 		if (sc->sc_rxint > (16*10)) {
    691  1.1  ad 			if ((csr & DZ_CSR_SAE) == 0)
    692  1.1  ad 				DZ_WRITE_WORD(dr_csr, csr | DZ_CSR_SAE);
    693  1.1  ad 	    	} else if ((csr & DZ_CSR_SAE) != 0)
    694  1.1  ad 			if (sc->sc_rxint < 10)
    695  1.1  ad 				DZ_WRITE_WORD(dr_csr, csr & ~(DZ_CSR_SAE));
    696  1.1  ad 
    697  1.1  ad 		sc->sc_rxint = 0;
    698  1.1  ad 	}
    699  1.1  ad 	(void) splx(s);
    700  1.1  ad 	callout_reset(&dzscan_ch, hz, dzscan, NULL);
    701  1.1  ad 	return;
    702  1.1  ad }
    703  1.1  ad 
    704  1.1  ad /*
    705  1.1  ad  * Called after an ubareset. The DZ card is reset, but the only thing
    706  1.1  ad  * that must be done is to start the receiver and transmitter again.
    707  1.1  ad  * No DMA setup to care about.
    708  1.1  ad  */
    709  1.1  ad void
    710  1.1  ad dzreset(struct device *dev)
    711  1.1  ad {
    712  1.1  ad 	struct dz_softc *sc = (void *)dev;
    713  1.1  ad 	struct tty *tp;
    714  1.1  ad 	int i;
    715  1.1  ad 
    716  1.1  ad 	for (i = 0; i < sc->sc_type; i++) {
    717  1.1  ad 		tp = sc->sc_dz[i].dz_tty;
    718  1.1  ad 
    719  1.1  ad 		if (((tp->t_state & TS_ISOPEN) == 0) || (tp->t_wopen == 0))
    720  1.1  ad 			continue;
    721  1.1  ad 
    722  1.1  ad 		dzparam(tp, &tp->t_termios);
    723  1.1  ad 		dzmctl(sc, i, DML_DTR, DMSET);
    724  1.1  ad 		tp->t_state &= ~TS_BUSY;
    725  1.1  ad 		dzstart(tp);	/* Kick off transmitter again */
    726  1.1  ad 	}
    727  1.1  ad }
    728