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