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