Home | History | Annotate | Line # | Download | only in dev
msc.c revision 1.2
      1  1.2  chopps /*	$NetBSD: msc.c,v 1.2 1995/10/07 18:18:26 chopps Exp $ */
      2  1.1  chopps 
      3  1.1  chopps /*
      4  1.1  chopps  * Copyright (c) 1993 Zik.
      5  1.1  chopps  * Copyright (c) 1995 Jukka Marin <jmarin (at) teeri.jmp.fi>.
      6  1.1  chopps  * Copyright (c) 1995 Timo Rossi <trossi (at) jyu.fi>.
      7  1.1  chopps  * Copyright (c) 1995 Rob Healey <rhealey (at) kas.helios.mn.org>.
      8  1.1  chopps  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
      9  1.1  chopps  * All rights reserved.
     10  1.1  chopps  *
     11  1.1  chopps  * Redistribution and use in source and binary forms, with or without
     12  1.1  chopps  * modification, are permitted provided that the following conditions
     13  1.1  chopps  * are met:
     14  1.1  chopps  * 1. Redistributions of source code must retain the above copyright
     15  1.1  chopps  *    notice, this list of conditions and the following disclaimer.
     16  1.1  chopps  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  chopps  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  chopps  *    documentation and/or other materials provided with the distribution.
     19  1.1  chopps  * 3. All advertising materials mentioning features or use of this software
     20  1.1  chopps  *    must display the following acknowledgement:
     21  1.1  chopps  *	This product includes software developed by the University of
     22  1.1  chopps  *	California, Berkeley and its contributors.
     23  1.1  chopps  * 4. Neither the name of the University nor the names of its contributors
     24  1.1  chopps  *    may be used to endorse or promote products derived from this software
     25  1.1  chopps  *    without specific prior written permission.
     26  1.1  chopps  *
     27  1.1  chopps  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  1.1  chopps  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  1.1  chopps  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  1.1  chopps  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  1.1  chopps  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  1.1  chopps  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  1.1  chopps  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  1.1  chopps  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  1.1  chopps  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  1.1  chopps  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  1.1  chopps  * SUCH DAMAGE.
     38  1.1  chopps  *
     39  1.1  chopps  *   - converted from NetBSD Amiga serial driver to A2232 serial driver
     40  1.1  chopps  *     by zik 931207
     41  1.1  chopps  *   - added ttyflags hooks rfh 940419
     42  1.1  chopps  *   - added new style config support rfh 940601
     43  1.1  chopps  *   - added code to halt board during memory load so board doesn't flip
     44  1.1  chopps  *     out. /dev/reload works now. Also created mschwiflow function so BSD can
     45  1.1  chopps  *     attempt to use board RTS flow control now. rfh 950108
     46  1.1  chopps  *   - Integrated work from Jukka Marin <jmarin (at) jmp.fi> and
     47  1.1  chopps  *     Timo Rossi <trossi (at) jyu.fi> The mscmint() code is Jukka's. 950916
     48  1.1  chopps  *     Integrated more bug fixes by Jukka Marin <jmarin (at) jmp.fi> 950918
     49  1.1  chopps  *     Also added Jukka's turbo board code. 950918
     50  1.1  chopps  *   - Reformatted to NetBSD style format.
     51  1.1  chopps  */
     52  1.1  chopps 
     53  1.1  chopps #include "msc.h"
     54  1.1  chopps 
     55  1.1  chopps #if NMSC > 0
     56  1.1  chopps #include <sys/param.h>
     57  1.1  chopps #include <sys/systm.h>
     58  1.1  chopps #include <sys/ioctl.h>
     59  1.1  chopps #include <sys/tty.h>
     60  1.1  chopps #include <sys/proc.h>
     61  1.1  chopps #include <sys/conf.h>
     62  1.1  chopps #include <sys/file.h>
     63  1.1  chopps #include <sys/malloc.h>
     64  1.1  chopps #include <sys/uio.h>
     65  1.1  chopps #include <sys/kernel.h>
     66  1.1  chopps #include <sys/syslog.h>
     67  1.1  chopps #include <sys/device.h>
     68  1.1  chopps 
     69  1.1  chopps #include <amiga/amiga/device.h>
     70  1.1  chopps #include <amiga/dev/zbusvar.h>
     71  1.1  chopps #include <amiga/dev/mscreg.h>
     72  1.1  chopps #include "machine/cpu.h"
     73  1.1  chopps 
     74  1.1  chopps #include <amiga/amiga/custom.h>
     75  1.1  chopps #include <amiga/amiga/cia.h>
     76  1.1  chopps #include <amiga/amiga/cc.h>
     77  1.1  chopps 
     78  1.1  chopps /* 6502 code for A2232 card */
     79  1.1  chopps #include "msc6502.h"
     80  1.1  chopps 
     81  1.1  chopps /*
     82  1.1  chopps  * Note: These are Zik's original comments:
     83  1.1  chopps  * There is a bit of a "gotcha" concerning the numbering
     84  1.1  chopps  * of msc devices and the specification of the number of serial
     85  1.1  chopps  * lines in the kernel.
     86  1.1  chopps  *
     87  1.1  chopps  * Each board has seven lines, but for programming convenience
     88  1.1  chopps  * and compatibility with Amiga UNIX the boards' minor device
     89  1.1  chopps  * numbers are allocated in groups of sixteen:
     90  1.1  chopps  *
     91  1.1  chopps  *                     minor device numbers
     92  1.1  chopps  * First board		0  2  4  6  8 10 12
     93  1.1  chopps  * Second board        16 18 20 22 24 26 28
     94  1.1  chopps  * Third board	       32 34 36 38 40 42 44
     95  1.1  chopps  *
     96  1.1  chopps  * The intermediate minor device numbers are dialin versions
     97  1.1  chopps  * of the same devices. ie. 10 is dialout line 6 and 11 is
     98  1.1  chopps  * the dialin version of line 6.
     99  1.1  chopps  *
    100  1.1  chopps  * On the other hand, I have made the NMSC config option refer
    101  1.1  chopps  * to the total number of a2232 cards, not the maximum
    102  1.1  chopps  * minor device number. So you might have NMSC=3, in which case
    103  1.1  chopps  * you have three boards with minor device numbers from 0 to 45.
    104  1.1  chopps  */
    105  1.1  chopps 
    106  1.1  chopps int	mscparam();
    107  1.1  chopps int	mscstart __P((struct tty *));
    108  1.1  chopps int	mschwiflow __P((struct tty *, int));
    109  1.1  chopps int	mscinitcard __P((struct zbus_args *));
    110  1.1  chopps 
    111  1.1  chopps int	mscdefaultrate = TTYDEF_SPEED;
    112  1.1  chopps 
    113  1.1  chopps struct	mscdevice mscdev[MSCSLOTS];	/* device structs for all lines */
    114  1.1  chopps struct	tty *msc_tty[MSCTTYS];		/* ttys for all lines */
    115  1.1  chopps 
    116  1.1  chopps struct	vbl_node msc_vbl_node[NMSC];	/* vbl interrupt node per board */
    117  1.1  chopps 
    118  1.1  chopps struct speedtab mscspeedtab_normal[] = {
    119  1.1  chopps 	0,	0,
    120  1.1  chopps 	50,	MSCPARAM_B50,
    121  1.1  chopps 	75,	MSCPARAM_B75,
    122  1.1  chopps 	110,	MSCPARAM_B110,
    123  1.1  chopps 	134,	MSCPARAM_B134,
    124  1.1  chopps 	150,	MSCPARAM_B150,
    125  1.1  chopps 	300,	MSCPARAM_B300,
    126  1.1  chopps 	600,	MSCPARAM_B600,
    127  1.1  chopps 	1200,	MSCPARAM_B1200,
    128  1.1  chopps 	1800,	MSCPARAM_B1800,
    129  1.1  chopps 	2400,	MSCPARAM_B2400,
    130  1.1  chopps 	3600,	MSCPARAM_B3600,
    131  1.1  chopps 	4800,	MSCPARAM_B4800,
    132  1.1  chopps 	7200,	MSCPARAM_B7200,
    133  1.1  chopps 	9600,	MSCPARAM_B9600,
    134  1.1  chopps 	19200,	MSCPARAM_B19200,
    135  1.1  chopps 	115200,	MSCPARAM_B115200,
    136  1.1  chopps 	-1,	-1
    137  1.1  chopps };
    138  1.1  chopps 
    139  1.1  chopps struct speedtab mscspeedtab_turbo[] = {
    140  1.1  chopps 	0,	0,
    141  1.1  chopps 	100,	MSCPARAM_B50,
    142  1.1  chopps 	150,	MSCPARAM_B75,
    143  1.1  chopps 	220,	MSCPARAM_B110,
    144  1.1  chopps 	269,	MSCPARAM_B134,
    145  1.1  chopps 	300,	MSCPARAM_B150,
    146  1.1  chopps 	600,	MSCPARAM_B300,
    147  1.1  chopps 	1200,	MSCPARAM_B600,
    148  1.1  chopps 	2400,	MSCPARAM_B1200,
    149  1.1  chopps 	3600,	MSCPARAM_B1800,
    150  1.1  chopps 	4800,	MSCPARAM_B2400,
    151  1.1  chopps 	7200,	MSCPARAM_B3600,
    152  1.1  chopps 	9600,	MSCPARAM_B4800,
    153  1.1  chopps 	14400,	MSCPARAM_B7200,
    154  1.1  chopps 	19200,	MSCPARAM_B9600,
    155  1.1  chopps 	38400,	MSCPARAM_B19200,
    156  1.1  chopps 	230400,	MSCPARAM_B115200,
    157  1.1  chopps 	-1,	-1
    158  1.1  chopps };
    159  1.1  chopps 
    160  1.1  chopps struct   speedtab *mscspeedtab;
    161  1.1  chopps 
    162  1.1  chopps int mscmctl __P((dev_t dev, int bits, int howto));
    163  1.1  chopps void mscmint __P((register void *data));
    164  1.1  chopps 
    165  1.1  chopps int mscmatch __P((struct device *, struct cfdata *, void *));
    166  1.1  chopps void mscattach __P((struct device *, struct device *, void *));
    167  1.1  chopps 
    168  1.1  chopps #define	SWFLAGS(dev)	(msc->openflags | (MSCDIALIN(dev) ? 0 : TIOCFLAG_SOFTCAR))
    169  1.1  chopps #define	DEBUG_MSC	0
    170  1.1  chopps #define	DEBUG_CD	0
    171  1.1  chopps 
    172  1.1  chopps struct cfdriver msccd = {
    173  1.1  chopps 	NULL, "msc", (cfmatch_t) mscmatch, mscattach, DV_TTY,
    174  1.1  chopps 	sizeof(struct device), NULL, 0
    175  1.1  chopps };
    176  1.1  chopps 
    177  1.2  chopps #if DEBUG_MSC
    178  1.2  chopps void
    179  1.2  chopps bugi(msc, string)
    180  1.2  chopps 	struct mscdevice *msc;
    181  1.2  chopps 	char *string;
    182  1.2  chopps {
    183  1.2  chopps 	volatile struct mscstatus *ms;
    184  1.2  chopps 	volatile struct mscmemory *mscmem;
    185  1.2  chopps 
    186  1.2  chopps 	mscmem = msc->board;
    187  1.2  chopps 	ms = &mscmem->Status[msc->port];
    188  1.2  chopps 
    189  1.2  chopps 	printf("msc  %s u%d f%08lx F%08lx\n", string, msc->port, msc->flags,
    190  1.2  chopps 		msc->openflags);
    191  1.2  chopps 	printf("msc  h%d t%d H%d t%d p%02x c%02x CD%02x\n", ms->InHead,
    192  1.2  chopps 		ms->InTail, ms->OutHead, ms->OutTail, ms->Param, ms->Command,
    193  1.2  chopps 		ms->chCD);
    194  1.2  chopps 	printf("msc  a%02x b%02x c%02x\n", ms->Pad_a, ms->Pad_b, ms->Padc);
    195  1.2  chopps 
    196  1.2  chopps 	return
    197  1.2  chopps }
    198  1.2  chopps 
    199  1.2  chopps #endif
    200  1.2  chopps 
    201  1.1  chopps int
    202  1.1  chopps mscmatch(pdp, cdp, auxp)
    203  1.1  chopps 	struct device *pdp;
    204  1.1  chopps 	struct cfdata *cdp;
    205  1.1  chopps 	void *auxp;
    206  1.1  chopps {
    207  1.1  chopps 	struct zbus_args *zap;
    208  1.1  chopps 
    209  1.1  chopps 	zap = auxp;
    210  1.1  chopps 	if (zap->manid == 514 && (zap->prodid == 70 || zap->prodid == 69))
    211  1.1  chopps 		return(1);
    212  1.1  chopps 
    213  1.1  chopps 	return (0);
    214  1.1  chopps }
    215  1.1  chopps 
    216  1.1  chopps void
    217  1.1  chopps mscattach(pdp, dp, auxp)
    218  1.1  chopps 	struct device *pdp, *dp;
    219  1.1  chopps 	void *auxp;
    220  1.1  chopps {
    221  1.1  chopps   volatile struct mscmemory *mscmem;
    222  1.1  chopps   struct mscdevice *msc;
    223  1.1  chopps   struct zbus_args *zap;
    224  1.1  chopps   int unit;
    225  1.1  chopps   int Count;
    226  1.1  chopps 
    227  1.1  chopps   zap = (struct zbus_args *)auxp;
    228  1.1  chopps   unit = dp->dv_unit;
    229  1.1  chopps 
    230  1.1  chopps   if (mscinitcard(zap) != 0) {
    231  1.1  chopps     printf("\nmsc%d: Board initialize failed, bad download code.\n", unit);
    232  1.1  chopps     return;
    233  1.1  chopps   }
    234  1.1  chopps 
    235  1.1  chopps   printf("\nmsc%d: Board successfully initialized.\n", unit);
    236  1.1  chopps 
    237  1.1  chopps   mscmem = (struct mscmemory *) zap->va;
    238  1.1  chopps 
    239  1.1  chopps   if (mscmem->Common.Crystal == MSC_UNKNOWN) {
    240  1.1  chopps 	printf("msc%d: Unable to detect crystal frequency.\n", unit);
    241  1.1  chopps 	return;
    242  1.1  chopps   }
    243  1.1  chopps 
    244  1.1  chopps   if (mscmem->Common.Crystal == MSC_TURBO) {
    245  1.1  chopps 	printf("msc%d: Turbo version detected (%02x%02x:%d)\n", unit,
    246  1.1  chopps 		mscmem->Common.TimerH, mscmem->Common.TimerL,
    247  1.1  chopps 		mscmem->Common.Pad_a);
    248  1.1  chopps 	mscspeedtab = mscspeedtab_turbo;
    249  1.1  chopps   } else {
    250  1.1  chopps 	printf("msc%d: Normal version detected (%02x%02x:%d)\n", unit,
    251  1.1  chopps 		mscmem->Common.TimerH, mscmem->Common.TimerL,
    252  1.1  chopps 		mscmem->Common.Pad_a);
    253  1.1  chopps 	mscspeedtab = mscspeedtab_normal;
    254  1.1  chopps   }
    255  1.1  chopps 
    256  1.1  chopps   /* XXX 8 is a constant */
    257  1.1  chopps   for (Count = 0; Count < 8 && MSCSLOTUL(unit, Count) < MSCSLOTS; Count++) {
    258  1.1  chopps     msc = &mscdev[MSCSLOTUL(unit, Count)];
    259  1.1  chopps     msc->board = mscmem;
    260  1.1  chopps     msc->port = Count;
    261  1.1  chopps     msc->flags = 0;
    262  1.1  chopps     msc->openflags = 0;
    263  1.1  chopps     msc->active = 1;
    264  1.1  chopps     msc->closing = FALSE;
    265  1.1  chopps     msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, Count))] = NULL;
    266  1.1  chopps     msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, Count))+1] = NULL;
    267  1.1  chopps 
    268  1.1  chopps   }
    269  1.1  chopps 
    270  1.1  chopps   /* disable the non-existant eighth port */
    271  1.1  chopps   if (MSCSLOTUL(unit, NUMLINES) < MSCSLOTS)
    272  1.1  chopps     mscdev[MSCSLOTUL(unit, NUMLINES)].active = 0;
    273  1.1  chopps 
    274  1.1  chopps   msc_vbl_node[unit].function = (void (*) (void *)) mscmint;
    275  1.1  chopps   msc_vbl_node[unit].data = (void *) unit;
    276  1.1  chopps 
    277  1.1  chopps   add_vbl_function (&msc_vbl_node[unit], MSC_VBL_PRIORITY, (void *)unit);
    278  1.1  chopps 
    279  1.1  chopps   return;
    280  1.1  chopps }
    281  1.1  chopps 
    282  1.1  chopps /* ARGSUSED */
    283  1.1  chopps int
    284  1.1  chopps mscopen(dev, flag, mode, p)
    285  1.1  chopps 	dev_t dev;
    286  1.1  chopps 	int flag, mode;
    287  1.1  chopps 	struct proc *p;
    288  1.1  chopps {
    289  1.1  chopps   register struct tty *tp;
    290  1.1  chopps   int error = 0;
    291  1.1  chopps   int s;
    292  1.1  chopps   int slot;
    293  1.1  chopps   int ttyn;
    294  1.1  chopps   struct mscdevice *msc;
    295  1.1  chopps   volatile struct mscstatus *ms;
    296  1.1  chopps 
    297  1.1  chopps   /* get the device structure */
    298  1.1  chopps   slot = MSCSLOT(dev);
    299  1.1  chopps   ttyn = MSCTTY(dev);
    300  1.1  chopps 
    301  1.1  chopps   if (slot >= MSCSLOTS)
    302  1.1  chopps     return ENXIO;
    303  1.1  chopps 
    304  1.1  chopps   if (MSCLINE(dev) >= NUMLINES)
    305  1.1  chopps     return ENXIO;
    306  1.1  chopps 
    307  1.1  chopps   msc = &mscdev[slot];
    308  1.1  chopps   ms = &msc->board->Status[msc->port];
    309  1.1  chopps 
    310  1.1  chopps   if (!msc->active)
    311  1.1  chopps     return ENXIO;
    312  1.1  chopps 
    313  1.1  chopps   /*
    314  1.1  chopps    * RFH: WHY here? Put down by while like other serial drivers
    315  1.1  chopps    *      But if we do that it makes things bomb.
    316  1.1  chopps    */
    317  1.1  chopps   s = spltty();
    318  1.1  chopps 
    319  1.1  chopps   if (!msc_tty[ttyn]) {
    320  1.1  chopps 
    321  1.1  chopps       tp = ttymalloc();
    322  1.1  chopps       msc_tty[ttyn] = tp;
    323  1.1  chopps       msc_tty[ttyn+1] = (struct tty *)NULL;
    324  1.1  chopps 
    325  1.2  chopps #if 0
    326  1.1  chopps       /* default values are not optimal for this device, increase buffers. */
    327  1.1  chopps       clfree(&tp->t_rawq);
    328  1.1  chopps       clfree(&tp->t_canq);
    329  1.1  chopps       clfree(&tp->t_outq);
    330  1.1  chopps       clalloc(&tp->t_rawq, 8192, 1);
    331  1.1  chopps       clalloc(&tp->t_canq, 8192, 1);
    332  1.1  chopps       clalloc(&tp->t_outq, 8192, 0);
    333  1.2  chopps #endif
    334  1.1  chopps 
    335  1.1  chopps   }
    336  1.1  chopps   else
    337  1.1  chopps     tp = msc_tty[ttyn];
    338  1.1  chopps 
    339  1.1  chopps   tp->t_oproc = (void (*) (struct tty *)) mscstart;
    340  1.1  chopps   tp->t_param = mscparam;
    341  1.1  chopps   tp->t_dev = dev;
    342  1.1  chopps   tp->t_hwiflow = mschwiflow;
    343  1.1  chopps 
    344  1.1  chopps   /* if port is still closing, just bitbucket remaining characters */
    345  1.1  chopps   if (msc->closing) {
    346  1.1  chopps 
    347  1.1  chopps       ms->OutFlush = TRUE;
    348  1.1  chopps       msc->closing = FALSE;
    349  1.1  chopps   }
    350  1.1  chopps 
    351  1.1  chopps   /* initialize tty */
    352  1.1  chopps   if ((tp->t_state & TS_ISOPEN) == 0) {
    353  1.1  chopps 
    354  1.1  chopps       tp->t_state |= TS_WOPEN;
    355  1.1  chopps       ttychars(tp);
    356  1.1  chopps       if (tp->t_ispeed == 0) {
    357  1.1  chopps 
    358  1.1  chopps 	  tp->t_iflag = TTYDEF_IFLAG;
    359  1.1  chopps 	  tp->t_oflag = TTYDEF_OFLAG;
    360  1.1  chopps 	  tp->t_cflag = TTYDEF_CFLAG;
    361  1.1  chopps 	  tp->t_lflag = TTYDEF_LFLAG;
    362  1.1  chopps 	  tp->t_ispeed = tp->t_ospeed = mscdefaultrate;
    363  1.1  chopps       }
    364  1.1  chopps 
    365  1.1  chopps       /* flags changed to be private to every unit by JM */
    366  1.1  chopps       if (msc->openflags & TIOCFLAG_CLOCAL)
    367  1.1  chopps 		tp->t_cflag |= CLOCAL;
    368  1.1  chopps       if (msc->openflags & TIOCFLAG_CRTSCTS)
    369  1.1  chopps 		tp->t_cflag |= CRTSCTS;
    370  1.1  chopps       if (msc->openflags & TIOCFLAG_MDMBUF)
    371  1.1  chopps 		tp->t_cflag |= MDMBUF;
    372  1.1  chopps 
    373  1.1  chopps       mscparam(tp, &tp->t_termios);
    374  1.1  chopps       ttsetwater(tp);
    375  1.1  chopps 
    376  1.1  chopps       (void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMSET);
    377  1.1  chopps 
    378  1.1  chopps       if ((SWFLAGS(dev) & TIOCFLAG_SOFTCAR) ||
    379  1.1  chopps 	  (mscmctl(dev, 0, DMGET) & TIOCM_CD))
    380  1.1  chopps             tp->t_state |= TS_CARR_ON;
    381  1.1  chopps       else
    382  1.1  chopps             tp->t_state &= ~TS_CARR_ON;
    383  1.1  chopps 
    384  1.1  chopps   }
    385  1.1  chopps   else {
    386  1.1  chopps         if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
    387  1.1  chopps 	    splx(s);
    388  1.1  chopps 	    return (EBUSY);
    389  1.1  chopps        }
    390  1.1  chopps   }
    391  1.1  chopps 
    392  1.1  chopps   /*
    393  1.1  chopps    * if NONBLOCK requested, ignore carrier
    394  1.1  chopps    */
    395  1.1  chopps   if (flag & O_NONBLOCK)
    396  1.1  chopps     goto done;
    397  1.1  chopps 
    398  1.1  chopps   /*
    399  1.1  chopps    * s = spltty();
    400  1.1  chopps    *
    401  1.1  chopps    * This causes hangs when put here, like other TTY drivers do, rather than
    402  1.1  chopps    * above, WHY? RFH
    403  1.1  chopps    *
    404  1.1  chopps    */
    405  1.1  chopps 
    406  1.1  chopps   while ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_cflag & CLOCAL) == 0) {
    407  1.1  chopps 
    408  1.1  chopps       tp->t_state |= TS_WOPEN;
    409  1.1  chopps 
    410  1.2  chopps #if DEBUG_CD
    411  1.2  chopps       printf("msc %ld waiting for CD\n", MSCLINE(dev));
    412  1.2  chopps #endif
    413  1.1  chopps       error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH, ttopen, 0);
    414  1.1  chopps 
    415  1.1  chopps       if (error) {
    416  1.1  chopps                splx(s);
    417  1.1  chopps                return(error);
    418  1.1  chopps       }
    419  1.1  chopps   }
    420  1.1  chopps 
    421  1.2  chopps done:
    422  1.1  chopps #if DEBUG_CD
    423  1.2  chopps   printf("msc %ld waiting for CD\n", MSCLINE(dev));
    424  1.1  chopps #endif
    425  1.1  chopps   /* This is a way to handle lost XON characters */
    426  1.1  chopps   if ((flag & O_TRUNC) && (tp->t_state & TS_TTSTOP)) {
    427  1.1  chopps           tp->t_state &= ~TS_TTSTOP;
    428  1.1  chopps           ttstart (tp);
    429  1.1  chopps   }
    430  1.1  chopps 
    431  1.1  chopps   splx(s);
    432  1.1  chopps 
    433  1.1  chopps   /*
    434  1.1  chopps    * Reset the tty pointer, as there could have been a dialout
    435  1.1  chopps    * use of the tty with a dialin open waiting.
    436  1.1  chopps    */
    437  1.1  chopps   tp->t_dev = dev;
    438  1.1  chopps 
    439  1.1  chopps   return((*linesw[tp->t_line].l_open)(dev, tp));
    440  1.1  chopps 
    441  1.1  chopps }
    442  1.1  chopps 
    443  1.1  chopps int
    444  1.1  chopps mscclose(dev, flag, mode, p)
    445  1.1  chopps 	dev_t dev;
    446  1.1  chopps 	int flag, mode;
    447  1.1  chopps 	struct proc *p;
    448  1.1  chopps {
    449  1.1  chopps   register struct tty *tp;
    450  1.1  chopps   int slot;
    451  1.1  chopps   volatile struct mscstatus *ms;
    452  1.1  chopps   struct mscdevice *msc;
    453  1.1  chopps 
    454  1.1  chopps   /* get the device structure */
    455  1.1  chopps   slot = MSCSLOT(dev);
    456  1.1  chopps 
    457  1.1  chopps   if (slot >= MSCSLOTS)
    458  1.1  chopps     return ENXIO;
    459  1.1  chopps 
    460  1.1  chopps   msc = &mscdev[slot];
    461  1.1  chopps 
    462  1.1  chopps   if (!msc->active)
    463  1.1  chopps     return ENXIO;
    464  1.1  chopps 
    465  1.1  chopps   ms = &msc->board->Status[msc->port];
    466  1.1  chopps 
    467  1.2  chopps #if DEBUG_MSC
    468  1.2  chopps   bugi(msc, "close1");
    469  1.2  chopps #endif
    470  1.2  chopps 
    471  1.1  chopps   tp = msc_tty[MSCTTY(dev)];
    472  1.1  chopps   (*linesw[tp->t_line].l_close)(tp, flag);
    473  1.1  chopps 
    474  1.1  chopps   (void) mscmctl(dev, 0, DMSET);
    475  1.1  chopps 
    476  1.1  chopps   ttyclose(tp);
    477  1.1  chopps 
    478  1.1  chopps   if (msc->flags & TIOCM_DTR)
    479  1.1  chopps     msc->closing = TRUE; /* flush remaining characters before dropping DTR */
    480  1.1  chopps   else
    481  1.1  chopps     ms->OutFlush = TRUE; /* just bitbucket remaining characters */
    482  1.1  chopps 
    483  1.2  chopps #if DEBUG_MSC
    484  1.2  chopps   bugi(msc, "close2");
    485  1.2  chopps #endif
    486  1.2  chopps 
    487  1.1  chopps   return (0);
    488  1.1  chopps 
    489  1.1  chopps }
    490  1.1  chopps 
    491  1.1  chopps int
    492  1.1  chopps mscread(dev, uio, flag)
    493  1.1  chopps 	dev_t dev;
    494  1.1  chopps 	struct uio *uio;
    495  1.1  chopps 	int flag;
    496  1.1  chopps {
    497  1.1  chopps 	register struct tty *tp;
    498  1.1  chopps 
    499  1.1  chopps 	tp = msc_tty[MSCTTY(dev)];
    500  1.1  chopps 
    501  1.1  chopps 	if (! tp)
    502  1.1  chopps 	 return ENXIO;
    503  1.1  chopps 
    504  1.1  chopps 	return((*linesw[tp->t_line].l_read)(tp, uio, flag));
    505  1.1  chopps }
    506  1.1  chopps 
    507  1.1  chopps int
    508  1.1  chopps mscwrite(dev, uio, flag)
    509  1.1  chopps 	dev_t dev;
    510  1.1  chopps 	struct uio *uio;
    511  1.1  chopps 	int flag;
    512  1.1  chopps {
    513  1.1  chopps   register struct tty *tp;
    514  1.1  chopps 
    515  1.1  chopps   tp = msc_tty[MSCTTY(dev)];
    516  1.1  chopps 
    517  1.1  chopps   if (! tp)
    518  1.1  chopps     return ENXIO;
    519  1.1  chopps 
    520  1.1  chopps   return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    521  1.1  chopps }
    522  1.1  chopps 
    523  1.1  chopps /*
    524  1.1  chopps  * This interrupt is periodically invoked in the vertical blank
    525  1.1  chopps  * interrupt. It's used to keep track of the modem control lines
    526  1.1  chopps  * and (new with the fast_int code) to move accumulated data up in
    527  1.1  chopps  * to the tty layer.
    528  1.1  chopps  *
    529  1.1  chopps  * NOTE: MSCCDHACK is an invention of mine for dubious purposes. If you
    530  1.1  chopps  *	 want to activate it add
    531  1.1  chopps  *	 options MSCCDHACK
    532  1.1  chopps  *	 in the kernel conf file. Basically it forces CD->Low transitions
    533  1.1  chopps  *	 to ALWAYS send a signal to the process, even if the device is in
    534  1.1  chopps  *	 clocal mode or an outdial device. RFH
    535  1.1  chopps  */
    536  1.1  chopps void
    537  1.1  chopps mscmint (data)
    538  1.1  chopps 	register void *data;
    539  1.1  chopps {
    540  1.1  chopps   int unit;
    541  1.1  chopps   register struct tty *tp;
    542  1.1  chopps   int slot;
    543  1.1  chopps   int maxslot;
    544  1.1  chopps   struct mscdevice *msc;
    545  1.1  chopps   volatile struct mscstatus *ms;
    546  1.1  chopps   volatile u_char *ibuf, *cbuf;
    547  1.1  chopps   unsigned char newhead; /* was int */
    548  1.1  chopps   unsigned char bufpos;  /* was int */
    549  1.1  chopps   int s;
    550  1.1  chopps 
    551  1.1  chopps   unit = (int) data;
    552  1.1  chopps 
    553  1.1  chopps   /* check each line on this board */
    554  1.1  chopps   maxslot = MSCSLOTUL(unit, NUMLINES);
    555  1.1  chopps   if (maxslot > MSCSLOTS)
    556  1.1  chopps     maxslot = MSCSLOTS;
    557  1.1  chopps 
    558  1.1  chopps   for (slot = MSCSLOTUL(unit, 0); slot < maxslot; slot++)
    559  1.1  chopps     {
    560  1.1  chopps       msc = &mscdev[slot];
    561  1.1  chopps 
    562  1.1  chopps       if (!msc->active)
    563  1.1  chopps         continue;
    564  1.1  chopps 
    565  1.1  chopps       tp = msc_tty[MSCTTYSLOT(slot)];
    566  1.1  chopps       ms = &msc->board->Status[msc->port];
    567  1.1  chopps 
    568  1.1  chopps       newhead = ms->InHead;		/* 65c02 write pointer */
    569  1.1  chopps 
    570  1.1  chopps       /* yoohoo, is the port open? */
    571  1.1  chopps       if (tp && (tp->t_state & (TS_ISOPEN|TS_WOPEN))) {
    572  1.1  chopps 	/* port is open, handle all type of events */
    573  1.1  chopps 
    574  1.1  chopps 	/* set interrupt priority level */
    575  1.1  chopps         s = spltty();
    576  1.1  chopps 
    577  1.1  chopps       /* check for input for this port */
    578  1.1  chopps       if (newhead != (bufpos = ms->InTail))
    579  1.1  chopps       {
    580  1.1  chopps #if DEBUG_MSC
    581  1.1  chopps 	      printf("iop%d\n",slot);
    582  1.1  chopps #endif
    583  1.1  chopps 	      /* buffer for input chars/events */
    584  1.1  chopps 	      ibuf = &msc->board->InBuf[msc->port][0];
    585  1.1  chopps 
    586  1.1  chopps 	      /* data types of bytes in ibuf */
    587  1.1  chopps 	      cbuf = &msc->board->InCtl[msc->port][0];
    588  1.1  chopps 
    589  1.1  chopps 	      /* do for all chars, if room */
    590  1.1  chopps 	      while (bufpos != newhead)
    591  1.1  chopps 	      {
    592  1.1  chopps 		  /* which type of input data? */
    593  1.1  chopps 		  switch (cbuf[bufpos])
    594  1.1  chopps 		  {
    595  1.1  chopps 		      /* input event (CD, BREAK, etc.) */
    596  1.1  chopps 		      case MSCINCTL_EVENT:
    597  1.1  chopps 			switch (ibuf[bufpos++])
    598  1.1  chopps 			{
    599  1.1  chopps 			    /* carrier detect change OFF -> ON */
    600  1.1  chopps 			    case MSCEVENT_CarrierOn:
    601  1.1  chopps #if DEBUG_CD
    602  1.2  chopps 			      printf("msc  CD ON %d\n", msc->port);
    603  1.1  chopps #endif
    604  1.1  chopps 			      msc->flags |= TIOCM_CD;
    605  1.2  chopps 			      if (MSCDIALIN(tp->t_dev))
    606  1.1  chopps 				(*linesw[tp->t_line].l_modem)(tp, 1);
    607  1.1  chopps 			      break;
    608  1.1  chopps 
    609  1.1  chopps 			    /*  carrier detect change ON -> OFF */
    610  1.1  chopps 			    case MSCEVENT_CarrierOff:
    611  1.1  chopps #if DEBUG_CD
    612  1.2  chopps 			      printf("msc  CD OFF %d\n", msc->port);
    613  1.1  chopps #endif
    614  1.1  chopps 			      msc->flags &= ~TIOCM_CD;
    615  1.1  chopps #ifndef MSCCDHACK
    616  1.2  chopps 			      if (MSCDIALIN(tp->t_dev))
    617  1.2  chopps #endif			    /* Note to format police: Don't merge the { below
    618  1.2  chopps 			       in to the line above! */
    619  1.1  chopps 			      {
    620  1.1  chopps 				  if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
    621  1.1  chopps 				  {
    622  1.1  chopps 				      /* clear RTS and DTR, bitbucket output */
    623  1.1  chopps 				      ms->Command = (ms->Command & ~MSCCMD_CMask) | MSCCMD_Close;
    624  1.1  chopps 				      ms->Setup = TRUE;
    625  1.1  chopps 				      msc->flags &= ~(TIOCM_DTR | TIOCM_RTS);
    626  1.1  chopps 				      ms->OutFlush = TRUE;
    627  1.1  chopps 				  }
    628  1.1  chopps 			      }
    629  1.1  chopps 			      break;
    630  1.1  chopps 
    631  1.1  chopps 			    case MSCEVENT_Break:
    632  1.1  chopps #if DEBUG_MSC
    633  1.1  chopps 			      printf("Break received on msc%d\n", slot);
    634  1.1  chopps #endif
    635  1.1  chopps 			      (*linesw[tp->t_line].l_rint)(TTY_FE, tp);
    636  1.1  chopps 			      break;
    637  1.1  chopps 
    638  1.1  chopps 			    default:
    639  1.1  chopps 			      printf("msc: unknown event type %d\n",
    640  1.1  chopps 				      ibuf[(bufpos-1)&0xff]);
    641  1.1  chopps 
    642  1.1  chopps 			} /* event type switch */
    643  1.1  chopps 			break;
    644  1.1  chopps 
    645  1.1  chopps 		      case MSCINCTL_CHAR:
    646  1.2  chopps 			 if (tp->t_state & TS_TBLOCK) {
    647  1.2  chopps 			   if (ms->chCD) {
    648  1.2  chopps 			     /* Carrier detect ON -> OFF */
    649  1.2  chopps #if DEBUG_CD
    650  1.2  chopps 			     printf("msc  CD OFF blocked %d msc->flags %08lx\n",
    651  1.2  chopps 				    msc->port, msc->flags);
    652  1.2  chopps #endif
    653  1.2  chopps 			     msc->flags &= ~TIOCM_CD;
    654  1.2  chopps 
    655  1.2  chopps #ifndef MSCCDHACK
    656  1.2  chopps 			     if (MSCDIALIN(tp->t_dev))
    657  1.2  chopps #endif
    658  1.2  chopps 			     {
    659  1.2  chopps 			       if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
    660  1.2  chopps 				 /* Clear RTS and DTR, bitbucket output */
    661  1.2  chopps 				 ms->Command = (ms->Command & ~MSCCMD_CMask) |
    662  1.2  chopps 					       MSCCMD_Close;
    663  1.2  chopps 				 ms->Setup = TRUE;
    664  1.2  chopps 				 msc->flags &= ~(TIOCM_DTR | TIOCM_RTS);
    665  1.2  chopps 				 ms->OutFlush = TRUE;
    666  1.2  chopps 			       }
    667  1.2  chopps 			     }
    668  1.2  chopps 			   }
    669  1.1  chopps 			   goto NoRoomForYa;
    670  1.2  chopps 			 }
    671  1.1  chopps #if DEBUG_MSC
    672  1.1  chopps 			 printf("'%c' ",ibuf[bufpos]);
    673  1.1  chopps #endif
    674  1.1  chopps 			 (*linesw[tp->t_line].l_rint)((int)ibuf[bufpos++], tp);
    675  1.1  chopps 			break;
    676  1.1  chopps 
    677  1.1  chopps 		      default:
    678  1.1  chopps 			printf("msc: unknown data type %d\n", cbuf[bufpos]);
    679  1.1  chopps 			bufpos++;
    680  1.1  chopps 
    681  1.1  chopps 		   } /* switch on input data type */
    682  1.1  chopps 
    683  1.1  chopps 		} /* while there's something in the buffer */
    684  1.1  chopps NoRoomForYa:
    685  1.1  chopps 	      ms->InTail = bufpos;		/* tell 65C02 what we've read */
    686  1.1  chopps 
    687  1.1  chopps 	    } /* if there was something in the buffer */
    688  1.1  chopps 
    689  1.1  chopps 	  /* we get here only when the port is open */
    690  1.1  chopps 	  /* send output */
    691  1.1  chopps 	   if (tp->t_state & (TS_BUSY|TS_FLUSH))
    692  1.1  chopps 	    {
    693  1.1  chopps 
    694  1.1  chopps 	      bufpos = ms->OutHead - ms->OutTail;
    695  1.1  chopps 
    696  1.1  chopps 	      /* busy and below low water mark? */
    697  1.1  chopps 	      if (tp->t_state & TS_BUSY)
    698  1.1  chopps 	        {
    699  1.1  chopps 	          if (bufpos < IOBUFLOWWATER)
    700  1.1  chopps 		    {
    701  1.1  chopps 		      tp->t_state &= ~TS_BUSY;	/* not busy any more */
    702  1.1  chopps 		      if (tp->t_line)
    703  1.1  chopps 		        (*linesw[tp->t_line].l_start)(tp);
    704  1.1  chopps 		      else
    705  1.1  chopps 		        mscstart(tp);
    706  1.1  chopps 		    }
    707  1.1  chopps 		}
    708  1.1  chopps 
    709  1.1  chopps 	      /* waiting for flush and buffer empty? */
    710  1.1  chopps 	      if (tp->t_state & TS_FLUSH)
    711  1.1  chopps 	        {
    712  1.1  chopps 	          if (bufpos == 0)
    713  1.1  chopps 	            tp->t_state &= ~TS_FLUSH;	/* finished flushing */
    714  1.1  chopps 	        }
    715  1.1  chopps 	    } /* BUSY or FLUSH */
    716  1.1  chopps 
    717  1.1  chopps 	     splx(s);
    718  1.1  chopps 
    719  1.1  chopps       } else { /* End of port open */
    720  1.1  chopps 	/* port is closed, don't pass on the chars from it */
    721  1.1  chopps 
    722  1.1  chopps       /* check for input for this port */
    723  1.1  chopps       if (newhead != (bufpos = ms->InTail))
    724  1.1  chopps       {
    725  1.1  chopps #if DEBUG_MSC
    726  1.1  chopps 	      printf("icp%d\n",slot);
    727  1.1  chopps #endif
    728  1.1  chopps 	      /* buffer for input chars/events */
    729  1.1  chopps 	      ibuf = &msc->board->InBuf[msc->port][0];
    730  1.1  chopps 
    731  1.1  chopps 	      /* data types of bytes in ibuf */
    732  1.1  chopps 	      cbuf = &msc->board->InCtl[msc->port][0];
    733  1.1  chopps 
    734  1.1  chopps 	      /* do for all chars, if room */
    735  1.1  chopps 	      while (bufpos != newhead)
    736  1.1  chopps 	      {
    737  1.1  chopps 		  /* which type of input data? */
    738  1.1  chopps 		  switch (cbuf[bufpos])
    739  1.1  chopps 		  {
    740  1.1  chopps 		      /* input event (CD, BREAK, etc.) */
    741  1.1  chopps 		      case MSCINCTL_EVENT:
    742  1.1  chopps 			switch (ibuf[bufpos++])
    743  1.1  chopps 			{
    744  1.1  chopps 			    /* carrier detect change OFF -> ON */
    745  1.1  chopps 			    case MSCEVENT_CarrierOn:
    746  1.1  chopps #if DEBUG_CD
    747  1.2  chopps 			      printf("msc  CD ON %d (closed)\n", msc->port);
    748  1.1  chopps #endif
    749  1.1  chopps 			      msc->flags |= TIOCM_CD;
    750  1.1  chopps 			      break;
    751  1.1  chopps 
    752  1.1  chopps 			    /*  carrier detect change ON -> OFF */
    753  1.1  chopps 			    case MSCEVENT_CarrierOff:
    754  1.1  chopps #if DEBUG_CD
    755  1.2  chopps 			      printf("msc  CD OFF %d (closed)\n", msc->port);
    756  1.1  chopps #endif
    757  1.1  chopps 			      msc->flags &= ~TIOCM_CD;
    758  1.1  chopps #ifndef MSCCDHACK
    759  1.1  chopps 			      if (tp && MSCDIALIN(tp->t_dev))
    760  1.1  chopps #else
    761  1.1  chopps 			      if (tp )
    762  1.1  chopps #endif
    763  1.1  chopps 			      {
    764  1.1  chopps 				  if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
    765  1.1  chopps 				  {
    766  1.1  chopps 				      /* clear RTS and DTR, bitbucket output */
    767  1.1  chopps 				      ms->Command = (ms->Command & ~MSCCMD_CMask) | MSCCMD_Close;
    768  1.1  chopps 				      ms->Setup = TRUE;
    769  1.1  chopps 				      msc->flags &= ~(TIOCM_DTR | TIOCM_RTS);
    770  1.1  chopps 				      ms->OutFlush = TRUE;
    771  1.1  chopps 				  }
    772  1.1  chopps 			      }
    773  1.1  chopps 			      break;
    774  1.1  chopps 
    775  1.1  chopps 			    default:
    776  1.1  chopps 			      printf("msc: unknown event type %d\n",
    777  1.1  chopps 				     ibuf[(bufpos-1)&0xff]);
    778  1.1  chopps 
    779  1.1  chopps 			} /* event type switch */
    780  1.1  chopps 			break;
    781  1.1  chopps 
    782  1.1  chopps 		      default:
    783  1.2  chopps 			bufpos++;
    784  1.1  chopps 
    785  1.1  chopps 		   } /* switch on input data type */
    786  1.1  chopps 
    787  1.1  chopps 		} /* while there's something in the buffer */
    788  1.1  chopps 
    789  1.1  chopps 	        ms->InTail = bufpos;		/* tell 65C02 what we've read */
    790  1.1  chopps 
    791  1.1  chopps 	    } /* if there was something in the buffer */
    792  1.1  chopps       } /* End of port open/close */
    793  1.1  chopps 
    794  1.1  chopps       /* is this port closing? */
    795  1.1  chopps       if (msc->closing)
    796  1.1  chopps         {
    797  1.1  chopps 	  /* if DTR is off, just bitbucket remaining characters */
    798  1.1  chopps 	  if ( (msc->flags & TIOCM_DTR) == 0)
    799  1.1  chopps 	    {
    800  1.1  chopps 	      ms->OutFlush = TRUE;
    801  1.1  chopps 	      msc->closing = FALSE;
    802  1.1  chopps 	    }
    803  1.1  chopps 	  /* if output has drained, drop DTR */
    804  1.1  chopps           else if (ms->OutHead == ms->OutTail)
    805  1.1  chopps 	    {
    806  1.1  chopps 	      (void) mscmctl(tp->t_dev, 0, DMSET);
    807  1.1  chopps 	      msc->closing = FALSE;
    808  1.1  chopps             }
    809  1.1  chopps         }
    810  1.1  chopps     }  /* For all ports */
    811  1.1  chopps 
    812  1.1  chopps }
    813  1.1  chopps 
    814  1.1  chopps int
    815  1.1  chopps mscioctl(dev, cmd, data, flag, p)
    816  1.1  chopps 	dev_t dev;
    817  1.1  chopps 	int cmd;
    818  1.1  chopps 	caddr_t data;
    819  1.1  chopps 	int flag;
    820  1.1  chopps 	struct proc *p;
    821  1.1  chopps {
    822  1.1  chopps   register struct tty *tp;
    823  1.1  chopps   register int slot;
    824  1.1  chopps   register int error;
    825  1.1  chopps   struct mscdevice *msc;
    826  1.1  chopps   volatile struct mscstatus *ms;
    827  1.1  chopps   int s;
    828  1.1  chopps 
    829  1.1  chopps   /* get the device structure */
    830  1.1  chopps   slot = MSCSLOT(dev);
    831  1.1  chopps 
    832  1.1  chopps   if (slot >= MSCSLOTS)
    833  1.1  chopps     return ENXIO;
    834  1.1  chopps 
    835  1.1  chopps   msc = &mscdev[slot];
    836  1.1  chopps 
    837  1.1  chopps   if (!msc->active)
    838  1.1  chopps     return ENXIO;
    839  1.1  chopps 
    840  1.1  chopps   ms = &msc->board->Status[msc->port];
    841  1.2  chopps   if (!(tp = msc_tty[MSCTTY(dev)]))
    842  1.2  chopps     return ENXIO;
    843  1.1  chopps 
    844  1.1  chopps   error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    845  1.1  chopps 
    846  1.1  chopps   if (error >= 0)
    847  1.1  chopps     return (error);
    848  1.1  chopps 
    849  1.1  chopps   error = ttioctl(tp, cmd, data, flag, p);
    850  1.1  chopps 
    851  1.1  chopps   if (error >= 0)
    852  1.1  chopps     return (error);
    853  1.1  chopps 
    854  1.1  chopps   switch (cmd) {
    855  1.1  chopps 
    856  1.1  chopps       /* send break */
    857  1.1  chopps     case TIOCSBRK:
    858  1.1  chopps       s = spltty();
    859  1.1  chopps       ms->Command = (ms->Command & (~MSCCMD_RTSMask)) | MSCCMD_Break;
    860  1.1  chopps       ms->Setup = TRUE;
    861  1.1  chopps       splx(s);
    862  1.1  chopps       break;
    863  1.1  chopps 
    864  1.1  chopps       /* clear break */
    865  1.1  chopps     case TIOCCBRK:
    866  1.1  chopps       s = spltty();
    867  1.1  chopps       ms->Command = (ms->Command & (~MSCCMD_RTSMask)) | MSCCMD_RTSOn;
    868  1.1  chopps       ms->Setup = TRUE;
    869  1.1  chopps       splx(s);
    870  1.1  chopps       break;
    871  1.1  chopps 
    872  1.1  chopps     case TIOCSDTR:
    873  1.1  chopps       (void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS);
    874  1.1  chopps       break;
    875  1.1  chopps 
    876  1.1  chopps     case TIOCCDTR:
    877  1.1  chopps       if (!MSCDIALIN(dev))	/* don't let dialins drop DTR */
    878  1.1  chopps         (void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC);
    879  1.1  chopps       break;
    880  1.1  chopps 
    881  1.1  chopps     case TIOCMSET:
    882  1.1  chopps       (void) mscmctl(dev, *(int *)data, DMSET);
    883  1.1  chopps       break;
    884  1.1  chopps 
    885  1.1  chopps     case TIOCMBIS:
    886  1.1  chopps       (void) mscmctl(dev, *(int *)data, DMBIS);
    887  1.1  chopps       break;
    888  1.1  chopps 
    889  1.1  chopps     case TIOCMBIC:
    890  1.1  chopps       if (MSCDIALIN(dev))	/* don't let dialins drop DTR */
    891  1.1  chopps         (void) mscmctl(dev, *(int *)data & TIOCM_DTR, DMBIC);
    892  1.1  chopps       else
    893  1.1  chopps         (void) mscmctl(dev, *(int *)data, DMBIC);
    894  1.1  chopps       break;
    895  1.1  chopps 
    896  1.1  chopps     case TIOCMGET:
    897  1.1  chopps       *(int *)data = mscmctl(dev, 0, DMGET);
    898  1.1  chopps       break;
    899  1.1  chopps 
    900  1.1  chopps     case TIOCGFLAGS:
    901  1.1  chopps       *(int *)data = SWFLAGS(dev);
    902  1.1  chopps       break;
    903  1.1  chopps 
    904  1.1  chopps     case TIOCSFLAGS:
    905  1.1  chopps       error = suser(p->p_ucred, &p->p_acflag);
    906  1.1  chopps       if (error != 0)
    907  1.1  chopps               return(EPERM);
    908  1.1  chopps 
    909  1.1  chopps       msc->openflags = *(int *)data;
    910  1.1  chopps 
    911  1.1  chopps       /* only allow valid flags */
    912  1.1  chopps       msc->openflags &= (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS);
    913  1.1  chopps 
    914  1.1  chopps       break;
    915  1.1  chopps 
    916  1.1  chopps     default:
    917  1.1  chopps       return (ENOTTY);
    918  1.1  chopps     }
    919  1.1  chopps 
    920  1.1  chopps   return (0);
    921  1.1  chopps }
    922  1.1  chopps 
    923  1.1  chopps 
    924  1.1  chopps int
    925  1.1  chopps mscparam(tp, t)
    926  1.1  chopps 	register struct tty *tp;
    927  1.1  chopps 	register struct termios *t;
    928  1.1  chopps {
    929  1.1  chopps   register int cfcr, cflag = t->c_cflag;
    930  1.1  chopps   int slot;
    931  1.1  chopps   struct mscdevice *msc;
    932  1.1  chopps   volatile struct mscstatus *ms;
    933  1.1  chopps   int s;
    934  1.1  chopps   int ospeed = ttspeedtab(t->c_ospeed, mscspeedtab);
    935  1.1  chopps 
    936  1.1  chopps   /* get the device structure */
    937  1.1  chopps   slot = MSCSLOT(tp->t_dev);
    938  1.1  chopps 
    939  1.1  chopps   if (slot >= MSCSLOTS)
    940  1.1  chopps     return ENXIO;
    941  1.1  chopps 
    942  1.1  chopps   msc = &mscdev[slot];
    943  1.1  chopps 
    944  1.1  chopps   if (!msc->active)
    945  1.1  chopps     return ENXIO;
    946  1.1  chopps 
    947  1.1  chopps   ms = &msc->board->Status[msc->port];
    948  1.1  chopps 
    949  1.2  chopps #if DEBUG_MSC
    950  1.2  chopps   bugi(msc, "param1");
    951  1.2  chopps #endif
    952  1.1  chopps   /* check requested parameters */
    953  1.1  chopps   if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
    954  1.1  chopps     return (EINVAL);
    955  1.1  chopps 
    956  1.1  chopps   /* and copy to tty */
    957  1.1  chopps   tp->t_ispeed = t->c_ispeed;
    958  1.1  chopps   tp->t_ospeed = t->c_ospeed;
    959  1.1  chopps   tp->t_cflag = cflag;
    960  1.1  chopps 
    961  1.1  chopps   /* hang up if baud is zero */
    962  1.1  chopps   if (t->c_ospeed == 0) {
    963  1.1  chopps 
    964  1.1  chopps       if (!MSCDIALIN(tp->t_dev))  /* don't let dialins drop DTR */
    965  1.1  chopps         (void) mscmctl(tp->t_dev, 0, DMSET);
    966  1.1  chopps   }
    967  1.1  chopps   else {
    968  1.1  chopps 
    969  1.1  chopps       /* set the baud rate */
    970  1.1  chopps       s = spltty();
    971  1.1  chopps       ms->Param = (ms->Param & ~MSCPARAM_BaudMask) | ospeed | MSCPARAM_RcvBaud;
    972  1.1  chopps 
    973  1.1  chopps       /* make sure any previous hangup is undone, ie.  reenable DTR.
    974  1.1  chopps        * also mscmctl will cause the speed to be set
    975  1.1  chopps        */
    976  1.1  chopps       (void) mscmctl (tp->t_dev, TIOCM_DTR | TIOCM_RTS, DMSET);
    977  1.2  chopps 
    978  1.1  chopps       splx(s);
    979  1.1  chopps   }
    980  1.1  chopps 
    981  1.2  chopps #if DEBUG_MSC
    982  1.2  chopps   bugi(msc, "param2");
    983  1.2  chopps #endif
    984  1.1  chopps   return (0);
    985  1.1  chopps 
    986  1.1  chopps }
    987  1.1  chopps 
    988  1.1  chopps 
    989  1.1  chopps /*
    990  1.1  chopps  *	Jukka's code initializes alot of stuff that other drivers don't
    991  1.1  chopps  *	I'm including it here so that this code is a common set of work
    992  1.1  chopps  *	done by both of us. rfh
    993  1.1  chopps  */
    994  1.1  chopps int
    995  1.1  chopps mschwiflow(tp, flag)
    996  1.1  chopps 	struct tty *tp;
    997  1.1  chopps 	int flag;
    998  1.1  chopps {
    999  1.1  chopps 
   1000  1.1  chopps /* Rob's version */
   1001  1.1  chopps #if 1
   1002  1.1  chopps #if DEBUG_MSC
   1003  1.1  chopps 	printf("mschwiflow %d\n", flag);
   1004  1.1  chopps #endif
   1005  1.1  chopps 
   1006  1.1  chopps 	if (flag)
   1007  1.1  chopps 	   mscmctl( tp->t_dev, TIOCM_RTS, DMBIC); /* Clear/Lower RTS */
   1008  1.1  chopps 	else
   1009  1.1  chopps 	   mscmctl( tp->t_dev, TIOCM_RTS, DMBIS); /* Set/Raise RTS */
   1010  1.1  chopps 
   1011  1.1  chopps #endif
   1012  1.1  chopps 
   1013  1.1  chopps /* Jukka's version */
   1014  1.1  chopps #if 0
   1015  1.1  chopps   int slot;
   1016  1.1  chopps   struct mscdevice *msc;
   1017  1.1  chopps   volatile struct mscstatus *ms;
   1018  1.1  chopps   int s;
   1019  1.1  chopps 
   1020  1.1  chopps   /* get the device structure */
   1021  1.1  chopps   slot = MSCSLOT(tp->t_dev);
   1022  1.1  chopps   if (slot >= MSCSLOTS)
   1023  1.1  chopps     return ENXIO;
   1024  1.1  chopps   msc = &mscdev[slot];
   1025  1.1  chopps   if (!msc->active)
   1026  1.1  chopps     return ENXIO;
   1027  1.1  chopps   ms = &msc->board->Status[msc->port];
   1028  1.1  chopps 
   1029  1.2  chopps #if DEBUG_MSC
   1030  1.2  chopps   bugi(msc, "hwiflow");
   1031  1.2  chopps #endif
   1032  1.1  chopps   /* Well, we should really _do_ something here, but the 65c02 code
   1033  1.1  chopps    * manages the RTS signal on its own now, so...  This will probably
   1034  1.1  chopps    * change in the future.
   1035  1.1  chopps    */
   1036  1.1  chopps 
   1037  1.1  chopps #endif
   1038  1.1  chopps 	return 1;
   1039  1.1  chopps 
   1040  1.1  chopps }
   1041  1.1  chopps 
   1042  1.1  chopps int
   1043  1.1  chopps mscstart(tp)
   1044  1.1  chopps 	register struct tty *tp;
   1045  1.1  chopps {
   1046  1.1  chopps   register int cc;
   1047  1.1  chopps   register char *cp;
   1048  1.1  chopps   register int mhead;
   1049  1.1  chopps   int s;
   1050  1.1  chopps   int slot;
   1051  1.1  chopps   struct mscdevice *msc;
   1052  1.1  chopps   volatile struct mscstatus *ms;
   1053  1.1  chopps   volatile char *mob;
   1054  1.1  chopps   int hiwat = 0;
   1055  1.1  chopps   int maxout;
   1056  1.1  chopps 
   1057  1.1  chopps   if (! (tp->t_state & TS_ISOPEN))
   1058  1.1  chopps     return;
   1059  1.1  chopps 
   1060  1.1  chopps   slot = MSCSLOT(tp->t_dev);
   1061  1.1  chopps 
   1062  1.1  chopps #if 0
   1063  1.1  chopps   printf("starting msc%d\n", slot);
   1064  1.1  chopps #endif
   1065  1.1  chopps 
   1066  1.1  chopps   s = spltty();
   1067  1.1  chopps 
   1068  1.1  chopps   /* don't start if explicitly stopped */
   1069  1.1  chopps   if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
   1070  1.1  chopps     goto out;
   1071  1.1  chopps 
   1072  1.1  chopps   /* wake up if below low water */
   1073  1.1  chopps   cc = tp->t_outq.c_cc;
   1074  1.1  chopps 
   1075  1.1  chopps   if (cc <= tp->t_lowat) {
   1076  1.1  chopps       if (tp->t_state & TS_ASLEEP) {
   1077  1.1  chopps 
   1078  1.1  chopps 	  tp->t_state &= ~TS_ASLEEP;
   1079  1.1  chopps 	  wakeup((caddr_t)&tp->t_outq);
   1080  1.1  chopps 	}
   1081  1.1  chopps 
   1082  1.1  chopps       selwakeup(&tp->t_wsel);
   1083  1.1  chopps   }
   1084  1.1  chopps 
   1085  1.1  chopps   /* don't bother if no characters or busy */
   1086  1.2  chopps   if (cc == 0 || (tp->t_state & TS_BUSY))
   1087  1.1  chopps     goto out;
   1088  1.1  chopps 
   1089  1.1  chopps   /*
   1090  1.1  chopps    * Limit the amount of output we do in one burst
   1091  1.1  chopps    */
   1092  1.1  chopps   msc = &mscdev[slot];
   1093  1.1  chopps   ms = &msc->board->Status[msc->port];
   1094  1.1  chopps   mhead = ms->OutHead;
   1095  1.1  chopps   maxout = mhead - ms->OutTail;
   1096  1.1  chopps 
   1097  1.1  chopps   if (maxout < 0)
   1098  1.1  chopps       maxout += IOBUFLEN;
   1099  1.1  chopps 
   1100  1.1  chopps   maxout = IOBUFLEN - 1 - maxout;
   1101  1.1  chopps 
   1102  1.1  chopps   if (cc >= maxout) {
   1103  1.1  chopps       hiwat++;
   1104  1.1  chopps       cc = maxout;
   1105  1.1  chopps   }
   1106  1.1  chopps 
   1107  1.1  chopps   cc = q_to_b (&tp->t_outq, msc->tmpbuf, cc);
   1108  1.1  chopps 
   1109  1.1  chopps   if (cc > 0) {
   1110  1.1  chopps       tp->t_state |= TS_BUSY;
   1111  1.1  chopps 
   1112  1.1  chopps       mob = &msc->board->OutBuf[msc->port][0];
   1113  1.1  chopps       cp = &msc->tmpbuf[0];
   1114  1.1  chopps 
   1115  1.1  chopps       /* enable output */
   1116  1.1  chopps       ms->OutDisable = FALSE;
   1117  1.1  chopps 
   1118  1.1  chopps #if 0
   1119  1.1  chopps       msc->tmpbuf[cc] = 0;
   1120  1.1  chopps       printf("sending '%s'\n", msctmpbuf);
   1121  1.1  chopps #endif
   1122  1.1  chopps 
   1123  1.1  chopps       /* send the first char across to reduce latency */
   1124  1.1  chopps       mob[mhead++] = *cp++;
   1125  1.1  chopps       mhead &= IOBUFLENMASK;
   1126  1.1  chopps       ms->OutHead = mhead;
   1127  1.1  chopps       cc--;
   1128  1.1  chopps 
   1129  1.1  chopps       /* copy the rest of the chars across quickly */
   1130  1.1  chopps       while (cc > 0) {
   1131  1.1  chopps 	  mob[mhead++] = *cp++;
   1132  1.1  chopps 	  mhead &= IOBUFLENMASK;
   1133  1.1  chopps 	  cc--;
   1134  1.1  chopps       }
   1135  1.1  chopps       ms->OutHead = mhead;
   1136  1.1  chopps 
   1137  1.1  chopps       /* leave the device busy if we've filled the buffer */
   1138  1.1  chopps       if (!hiwat)
   1139  1.1  chopps         tp->t_state &= ~TS_BUSY;
   1140  1.1  chopps     }
   1141  1.1  chopps 
   1142  1.1  chopps out:
   1143  1.1  chopps   splx(s);
   1144  1.1  chopps 
   1145  1.1  chopps }
   1146  1.1  chopps 
   1147  1.1  chopps /* XXX */
   1148  1.1  chopps /*
   1149  1.1  chopps  * Stop output on a line.
   1150  1.1  chopps  */
   1151  1.1  chopps /*ARGSUSED*/
   1152  1.1  chopps int
   1153  1.1  chopps mscstop(tp, flag)
   1154  1.1  chopps 	register struct tty *tp;
   1155  1.1  chopps 	int flag;			/* defaulted to int anyway */
   1156  1.1  chopps {
   1157  1.1  chopps 	register int s;
   1158  1.1  chopps 	struct mscdevice *msc;
   1159  1.1  chopps 	volatile struct mscstatus *ms;
   1160  1.1  chopps 
   1161  1.1  chopps 	s = spltty();
   1162  1.1  chopps 	if (tp->t_state & TS_BUSY) {
   1163  1.1  chopps 		if (tp->t_state & TS_TTSTOP == 0) {
   1164  1.1  chopps 			tp->t_state |= TS_FLUSH;
   1165  1.1  chopps #if 0
   1166  1.1  chopps 			msc = &mscdev[MSCSLOT(tp->t_dev)];
   1167  1.1  chopps 			ms = &msc->board->Status[msc->port];
   1168  1.1  chopps 			printf("stopped output on msc%d\n", MSCSLOT(tp->t_dev));
   1169  1.1  chopps 			ms->OutDisable = TRUE;
   1170  1.1  chopps #endif
   1171  1.1  chopps 		}
   1172  1.1  chopps 	}
   1173  1.1  chopps 	splx(s);
   1174  1.1  chopps }
   1175  1.1  chopps 
   1176  1.1  chopps /*
   1177  1.1  chopps  * bits can be: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, TIOCM_CD, TIOCM_RI, TIOCM_DSR
   1178  1.1  chopps  */
   1179  1.1  chopps int
   1180  1.1  chopps mscmctl(dev, bits, how)
   1181  1.1  chopps 	dev_t dev;
   1182  1.1  chopps 	int bits, how;
   1183  1.1  chopps {
   1184  1.1  chopps   struct mscdevice *msc;
   1185  1.1  chopps   volatile struct mscstatus *ms;
   1186  1.1  chopps   int slot;
   1187  1.1  chopps   int s;
   1188  1.1  chopps   u_char newcmd;
   1189  1.1  chopps   int OldFlags;
   1190  1.1  chopps 
   1191  1.1  chopps   /* get the device structure */
   1192  1.1  chopps   slot = MSCSLOT(dev);
   1193  1.1  chopps 
   1194  1.1  chopps   if (slot >= MSCSLOTS)
   1195  1.1  chopps     return ENXIO;
   1196  1.1  chopps 
   1197  1.1  chopps   msc = &mscdev[slot];
   1198  1.1  chopps 
   1199  1.1  chopps   if (!msc->active)
   1200  1.1  chopps     return ENXIO;
   1201  1.1  chopps 
   1202  1.2  chopps #if DEBUG_MSC
   1203  1.2  chopps   bugi(msc, "mctl1");
   1204  1.2  chopps #endif
   1205  1.2  chopps 
   1206  1.2  chopps   s = spltty();		/* Jukka wants spl6() here, WHY?!! RFH */
   1207  1.1  chopps 
   1208  1.1  chopps   if (how != DMGET) {
   1209  1.1  chopps       OldFlags = msc->flags;
   1210  1.1  chopps       bits &= TIOCM_DTR | TIOCM_RTS; /* can only modify DTR and RTS */
   1211  1.1  chopps 
   1212  1.1  chopps       switch (how) {
   1213  1.1  chopps         case DMSET:
   1214  1.1  chopps 	  msc->flags = (bits | (msc->flags & ~(TIOCM_DTR | TIOCM_RTS)));
   1215  1.1  chopps           break;
   1216  1.1  chopps 
   1217  1.1  chopps         case DMBIC:
   1218  1.1  chopps           msc->flags &= ~bits;
   1219  1.1  chopps           break;
   1220  1.1  chopps 
   1221  1.1  chopps         case DMBIS:
   1222  1.1  chopps           msc->flags |= bits;
   1223  1.1  chopps           break;
   1224  1.1  chopps       }
   1225  1.1  chopps 
   1226  1.2  chopps #if DEBUG_MSC
   1227  1.2  chopps     bugi(msc, "mctl2");
   1228  1.2  chopps #endif
   1229  1.2  chopps 
   1230  1.1  chopps       /* modify modem control state */
   1231  1.1  chopps       ms = &msc->board->Status[msc->port];
   1232  1.1  chopps 
   1233  1.1  chopps       if (msc->flags & TIOCM_RTS)	/* was bits & */
   1234  1.1  chopps 	newcmd = MSCCMD_RTSOn;
   1235  1.1  chopps       else			/* this doesn't actually work now */
   1236  1.1  chopps 	newcmd = MSCCMD_RTSOff;
   1237  1.1  chopps 
   1238  1.1  chopps       if (msc->flags & TIOCM_DTR)	/* was bits & */
   1239  1.1  chopps 	newcmd |= MSCCMD_Enable;
   1240  1.1  chopps 
   1241  1.1  chopps       ms->Command = (ms->Command & (~MSCCMD_RTSMask & ~MSCCMD_Enable)) | newcmd;
   1242  1.1  chopps       ms->Setup = TRUE;
   1243  1.1  chopps 
   1244  1.1  chopps       /* if we've dropped DTR, bitbucket any pending output */
   1245  1.1  chopps       if ( (OldFlags & TIOCM_DTR) && ((bits & TIOCM_DTR) == 0))
   1246  1.1  chopps         ms->OutFlush = TRUE;
   1247  1.1  chopps   }
   1248  1.1  chopps 
   1249  1.1  chopps   bits = msc->flags;
   1250  1.1  chopps 
   1251  1.1  chopps   (void) splx(s);
   1252  1.1  chopps 
   1253  1.2  chopps #if DEBUG_MSC
   1254  1.2  chopps     bugi(msc, "mctl3");
   1255  1.2  chopps #endif
   1256  1.2  chopps 
   1257  1.1  chopps   return(bits);
   1258  1.1  chopps 
   1259  1.1  chopps }
   1260  1.1  chopps 
   1261  1.1  chopps struct tty *
   1262  1.1  chopps msctty(dev)
   1263  1.1  chopps 	dev_t dev;
   1264  1.1  chopps {
   1265  1.1  chopps 	return(msc_tty[MSCTTY(dev)]);
   1266  1.1  chopps }
   1267  1.1  chopps 
   1268  1.1  chopps /*
   1269  1.1  chopps  * Load JM's freely redistributable A2232 6502c code. Let turbo detector
   1270  1.1  chopps  * run for a while too.
   1271  1.1  chopps  */
   1272  1.1  chopps 
   1273  1.1  chopps int
   1274  1.1  chopps mscinitcard(zap)
   1275  1.1  chopps 	struct zbus_args *zap;
   1276  1.1  chopps {
   1277  1.1  chopps   int bcount;
   1278  1.1  chopps   short start;
   1279  1.1  chopps   u_char *from;
   1280  1.1  chopps   volatile u_char *to;
   1281  1.1  chopps   volatile struct mscmemory *mlm;
   1282  1.1  chopps 
   1283  1.1  chopps   mlm = (volatile struct mscmemory *)zap->va;
   1284  1.1  chopps   (void)mlm->Enable6502Reset;
   1285  1.1  chopps 
   1286  1.1  chopps   /* copy the code across to the board */
   1287  1.1  chopps   to = (u_char *)mlm;
   1288  1.1  chopps   from = msc6502code; bcount = sizeof(msc6502code) - 2;
   1289  1.1  chopps   start = *(short *)from; from += sizeof(start);
   1290  1.1  chopps   to += start;
   1291  1.1  chopps 
   1292  1.1  chopps #if DEBUG_MSC
   1293  1.1  chopps   printf("\n** copying %ld bytes from %08lx to %08lx (start=%04lx)\n",
   1294  1.1  chopps 	  (unsigned long)bcount, (unsigned long)from, to, start);
   1295  1.1  chopps   printf("First byte to copy is %02lx\n", *from);
   1296  1.1  chopps #endif
   1297  1.1  chopps 
   1298  1.1  chopps   while(bcount--) *to++ = *from++;
   1299  1.1  chopps 
   1300  1.1  chopps   mlm->Common.Crystal = MSC_UNKNOWN;	/* use automatic speed check */
   1301  1.1  chopps 
   1302  1.2  chopps   /* start 6502 running */
   1303  1.1  chopps   (void)mlm->ResetBoard;
   1304  1.1  chopps 
   1305  1.1  chopps   /* wait until speed detector has finished */
   1306  1.1  chopps   for (bcount = 0; bcount < 200; bcount++) {
   1307  1.1  chopps 	delay(10000);
   1308  1.1  chopps 	if (mlm->Common.Crystal) break;
   1309  1.1  chopps   }
   1310  1.1  chopps 
   1311  1.1  chopps   return(0);
   1312  1.1  chopps 
   1313  1.1  chopps }
   1314  1.1  chopps 
   1315  1.1  chopps #endif  /* NMSC > 0 */
   1316