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