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