Home | History | Annotate | Line # | Download | only in dev
ms.c revision 1.6
      1  1.6  mycroft /*	$NetBSD: ms.c,v 1.6 1999/02/03 20:22:28 mycroft Exp $ */
      2  1.1      oki 
      3  1.1      oki /*
      4  1.1      oki  * Copyright (c) 1992, 1993
      5  1.1      oki  *	The Regents of the University of California.  All rights reserved.
      6  1.1      oki  *
      7  1.1      oki  * This software was developed by the Computer Systems Engineering group
      8  1.1      oki  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  1.1      oki  * contributed to Berkeley.
     10  1.1      oki  *
     11  1.1      oki  * All advertising materials mentioning features or use of this software
     12  1.1      oki  * must display the following acknowledgement:
     13  1.1      oki  *	This product includes software developed by the University of
     14  1.1      oki  *	California, Lawrence Berkeley Laboratory.
     15  1.1      oki  *
     16  1.1      oki  * Redistribution and use in source and binary forms, with or without
     17  1.1      oki  * modification, are permitted provided that the following conditions
     18  1.1      oki  * are met:
     19  1.1      oki  * 1. Redistributions of source code must retain the above copyright
     20  1.1      oki  *    notice, this list of conditions and the following disclaimer.
     21  1.1      oki  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1      oki  *    notice, this list of conditions and the following disclaimer in the
     23  1.1      oki  *    documentation and/or other materials provided with the distribution.
     24  1.1      oki  * 3. All advertising materials mentioning features or use of this software
     25  1.1      oki  *    must display the following acknowledgement:
     26  1.1      oki  *	This product includes software developed by the University of
     27  1.1      oki  *	California, Berkeley and its contributors.
     28  1.1      oki  * 4. Neither the name of the University nor the names of its contributors
     29  1.1      oki  *    may be used to endorse or promote products derived from this software
     30  1.1      oki  *    without specific prior written permission.
     31  1.1      oki  *
     32  1.1      oki  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  1.1      oki  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  1.1      oki  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  1.1      oki  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  1.1      oki  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  1.1      oki  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  1.1      oki  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  1.1      oki  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  1.1      oki  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  1.1      oki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  1.1      oki  * SUCH DAMAGE.
     43  1.1      oki  *
     44  1.1      oki  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
     45  1.1      oki  */
     46  1.1      oki 
     47  1.1      oki /*
     48  1.5  minoura  * X68k mouse driver.
     49  1.1      oki  */
     50  1.1      oki 
     51  1.1      oki #include <sys/param.h>
     52  1.1      oki #include <sys/conf.h>
     53  1.1      oki #include <sys/ioctl.h>
     54  1.1      oki #include <sys/kernel.h>
     55  1.1      oki #include <sys/proc.h>
     56  1.1      oki #include <sys/syslog.h>
     57  1.1      oki #include <sys/systm.h>
     58  1.1      oki #include <sys/tty.h>
     59  1.5  minoura #include <sys/device.h>
     60  1.1      oki 
     61  1.5  minoura #include <dev/ic/z8530reg.h>
     62  1.5  minoura #include <machine/z8530var.h>
     63  1.5  minoura 
     64  1.5  minoura #include <arch/x68k/dev/event_var.h>
     65  1.1      oki #include <machine/vuid_event.h>
     66  1.1      oki 
     67  1.5  minoura #include "locators.h"
     68  1.5  minoura 
     69  1.5  minoura /*
     70  1.5  minoura  * How many input characters we can buffer.
     71  1.5  minoura  * The port-specific var.h may override this.
     72  1.5  minoura  * Note: must be a power of two!
     73  1.5  minoura  */
     74  1.5  minoura #define	MS_RX_RING_SIZE	256
     75  1.5  minoura #define MS_RX_RING_MASK (MS_RX_RING_SIZE-1)
     76  1.5  minoura /*
     77  1.5  minoura  * Output buffer.  Only need a few chars.
     78  1.5  minoura  */
     79  1.5  minoura #define	MS_TX_RING_SIZE	16
     80  1.5  minoura #define MS_TX_RING_MASK (MS_TX_RING_SIZE-1)
     81  1.5  minoura /*
     82  1.5  minoura  * Mouse serial line is fixed at 4800 bps.
     83  1.5  minoura  */
     84  1.5  minoura #define MS_BPS 4800
     85  1.1      oki 
     86  1.1      oki /*
     87  1.1      oki  * Mouse state.  A SHARP X1/X680x0 mouse is a fairly simple device,
     88  1.1      oki  * producing three-byte blobs of the form:
     89  1.1      oki  *
     90  1.1      oki  *	b dx dy
     91  1.1      oki  *
     92  1.1      oki  * where b is the button state, encoded as 0x80|(buttons)---there are
     93  1.1      oki  * two buttons (2=left, 1=right)---and dx,dy are X and Y delta values.
     94  1.5  minoura  *
     95  1.5  minoura  * It needs trigger for the transmission.  When zs RTS negated, the mouse
     96  1.5  minoura  * begins the sequence.  RTS assertion has no effect.
     97  1.1      oki  */
     98  1.1      oki struct ms_softc {
     99  1.5  minoura 	struct	device ms_dev;		/* required first: base device */
    100  1.5  minoura 	struct	zs_chanstate *ms_cs;
    101  1.5  minoura 
    102  1.5  minoura 	/* Flags to communicate with ms_softintr() */
    103  1.5  minoura 	volatile int ms_intr_flags;
    104  1.5  minoura #define	INTR_RX_OVERRUN 1
    105  1.5  minoura #define INTR_TX_EMPTY   2
    106  1.5  minoura #define INTR_ST_CHECK   4
    107  1.5  minoura 
    108  1.5  minoura 	/*
    109  1.5  minoura 	 * The receive ring buffer.
    110  1.5  minoura 	 */
    111  1.5  minoura 	u_int	ms_rbget;	/* ring buffer `get' index */
    112  1.5  minoura 	volatile u_int	ms_rbput;	/* ring buffer `put' index */
    113  1.5  minoura 	u_short	ms_rbuf[MS_RX_RING_SIZE]; /* rr1, data pairs */
    114  1.5  minoura 
    115  1.5  minoura 	/*
    116  1.5  minoura 	 * State of input translator
    117  1.5  minoura 	 */
    118  1.1      oki 	short	ms_byteno;		/* input byte number, for decode */
    119  1.1      oki 	char	ms_mb;			/* mouse button state */
    120  1.1      oki 	char	ms_ub;			/* user button state */
    121  1.1      oki 	int	ms_dx;			/* delta-x */
    122  1.1      oki 	int	ms_dy;			/* delta-y */
    123  1.5  minoura 	int	ms_rts;			/* MSCTRL */
    124  1.5  minoura 	int	ms_nodata;
    125  1.5  minoura 
    126  1.5  minoura 	/*
    127  1.5  minoura 	 * State of upper interface.
    128  1.5  minoura 	 */
    129  1.1      oki 	volatile int ms_ready;		/* event queue is ready */
    130  1.1      oki 	struct	evvar ms_events;	/* event queue state */
    131  1.1      oki } ms_softc;
    132  1.1      oki 
    133  1.4      oki cdev_decl(ms);
    134  1.4      oki 
    135  1.5  minoura static int ms_match __P((struct device*, struct cfdata*, void*));
    136  1.5  minoura static void ms_attach __P((struct device*, struct device*, void*));
    137  1.5  minoura static void ms_trigger __P((struct zs_chanstate*, int));
    138  1.5  minoura void ms_modem __P((void));
    139  1.5  minoura 
    140  1.5  minoura struct cfattach ms_ca = {
    141  1.5  minoura 	sizeof(struct ms_softc), ms_match, ms_attach
    142  1.5  minoura };
    143  1.5  minoura 
    144  1.5  minoura extern struct zsops zsops_ms;
    145  1.5  minoura extern struct cfdriver ms_cd;
    146  1.4      oki 
    147  1.1      oki /*
    148  1.5  minoura  * ms_match: how is this zs channel configured?
    149  1.1      oki  */
    150  1.5  minoura int
    151  1.5  minoura ms_match(parent, cf, aux)
    152  1.5  minoura 	struct device *parent;
    153  1.5  minoura 	struct cfdata *cf;
    154  1.5  minoura 	void   *aux;
    155  1.5  minoura {
    156  1.5  minoura 	struct zsc_attach_args *args = aux;
    157  1.5  minoura 	struct zsc_softc *zsc = (void*) parent;
    158  1.5  minoura 
    159  1.5  minoura 	/* Exact match required for the mouse. */
    160  1.5  minoura 	if (cf->cf_loc[ZSCCF_CHANNEL] != args->channel)
    161  1.5  minoura 		return 0;
    162  1.5  minoura 	if (args->channel != 1)
    163  1.5  minoura 		return 0;
    164  1.5  minoura 
    165  1.5  minoura 	return 1;
    166  1.5  minoura }
    167  1.5  minoura 
    168  1.5  minoura void
    169  1.5  minoura ms_attach(parent, self, aux)
    170  1.5  minoura 	struct device *parent, *self;
    171  1.5  minoura 	void   *aux;
    172  1.5  minoura 
    173  1.5  minoura {
    174  1.5  minoura 	struct zsc_softc *zsc = (void *) parent;
    175  1.5  minoura 	struct ms_softc *ms = (void *) self;
    176  1.5  minoura 	struct zsc_attach_args *args = aux;
    177  1.5  minoura 	struct zs_chanstate *cs;
    178  1.5  minoura 	struct cfdata *cf;
    179  1.5  minoura 	int reset, s;
    180  1.5  minoura 
    181  1.5  minoura 	cf = ms->ms_dev.dv_cfdata;
    182  1.5  minoura 	cs = zsc->zsc_cs[1];
    183  1.5  minoura 	cs->cs_private = ms;
    184  1.5  minoura 	cs->cs_ops = &zsops_ms;
    185  1.5  minoura 	ms->ms_cs = cs;
    186  1.5  minoura 
    187  1.5  minoura 	/* Initialize the speed, etc. */
    188  1.5  minoura 	s = splzs();
    189  1.5  minoura 	/* May need reset... */
    190  1.5  minoura 	reset = ZSWR9_B_RESET;
    191  1.5  minoura 	zs_write_reg(cs, 9, reset);
    192  1.5  minoura 	/* We don't care about status or tx interrupts. */
    193  1.5  minoura 	cs->cs_preg[1] = ZSWR1_RIE;
    194  1.5  minoura 	cs->cs_preg[4] = ZSWR4_CLK_X16 | ZSWR4_TWOSB;
    195  1.5  minoura 	(void) zs_set_speed(cs, MS_BPS);
    196  1.5  minoura 	zs_loadchannelregs(cs);
    197  1.5  minoura 	splx(s);
    198  1.5  minoura 
    199  1.5  minoura 	/* Initialize translator. */
    200  1.5  minoura 	ms->ms_ready = 0;
    201  1.5  minoura 
    202  1.5  minoura 	printf ("\n");
    203  1.5  minoura }
    204  1.5  minoura 
    205  1.5  minoura /****************************************************************
    206  1.5  minoura  *  Entry points for /dev/mouse
    207  1.5  minoura  *  (open,close,read,write,...)
    208  1.5  minoura  ****************************************************************/
    209  1.5  minoura 
    210  1.5  minoura int
    211  1.5  minoura msopen(dev, flags, mode, p)
    212  1.5  minoura 	dev_t dev;
    213  1.5  minoura 	int flags, mode;
    214  1.5  minoura 	struct proc *p;
    215  1.5  minoura {
    216  1.5  minoura 	struct ms_softc *ms;
    217  1.5  minoura 	int unit;
    218  1.5  minoura 
    219  1.5  minoura printf ("msopen\n");
    220  1.5  minoura 	unit = minor(dev);
    221  1.5  minoura 	if (unit >= ms_cd.cd_ndevs)
    222  1.5  minoura 		return (ENXIO);
    223  1.5  minoura 	ms = ms_cd.cd_devs[unit];
    224  1.5  minoura 	if (ms == NULL)
    225  1.5  minoura 		return (ENXIO);
    226  1.5  minoura 
    227  1.5  minoura 	/* This is an exclusive open device. */
    228  1.5  minoura 	if (ms->ms_events.ev_io)
    229  1.5  minoura 		return (EBUSY);
    230  1.5  minoura 	ms->ms_events.ev_io = p;
    231  1.5  minoura 	ev_init(&ms->ms_events);	/* may cause sleep */
    232  1.5  minoura 
    233  1.5  minoura 	ms->ms_ready = 1;		/* start accepting events */
    234  1.5  minoura 	ms->ms_rts = 1;
    235  1.5  minoura 	ms_trigger(ms->ms_cs, 1);	/* set MSCTR high (standby) */
    236  1.5  minoura 	ms->ms_byteno = -1;
    237  1.5  minoura 	ms->ms_nodata = 0;
    238  1.5  minoura 	return (0);
    239  1.5  minoura }
    240  1.5  minoura 
    241  1.5  minoura int
    242  1.5  minoura msclose(dev, flags, mode, p)
    243  1.5  minoura 	dev_t dev;
    244  1.5  minoura 	int flags, mode;
    245  1.5  minoura 	struct proc *p;
    246  1.5  minoura {
    247  1.5  minoura 	struct ms_softc *ms;
    248  1.5  minoura 
    249  1.5  minoura 	ms = ms_cd.cd_devs[minor(dev)];
    250  1.5  minoura 	ms->ms_ready = 0;		/* stop accepting events */
    251  1.5  minoura 	ev_fini(&ms->ms_events);
    252  1.5  minoura 
    253  1.5  minoura 	ms->ms_events.ev_io = NULL;
    254  1.5  minoura 	return (0);
    255  1.5  minoura }
    256  1.5  minoura 
    257  1.5  minoura int
    258  1.5  minoura msread(dev, uio, flags)
    259  1.5  minoura 	dev_t dev;
    260  1.5  minoura 	struct uio *uio;
    261  1.5  minoura 	int flags;
    262  1.5  minoura {
    263  1.5  minoura 	struct ms_softc *ms;
    264  1.5  minoura 
    265  1.5  minoura 	ms = ms_cd.cd_devs[minor(dev)];
    266  1.5  minoura 	return (ev_read(&ms->ms_events, uio, flags));
    267  1.5  minoura }
    268  1.5  minoura 
    269  1.5  minoura /* this routine should not exist, but is convenient to write here for now */
    270  1.5  minoura int
    271  1.5  minoura mswrite(dev, uio, flags)
    272  1.5  minoura 	dev_t dev;
    273  1.5  minoura 	struct uio *uio;
    274  1.5  minoura 	int flags;
    275  1.1      oki {
    276  1.1      oki 
    277  1.5  minoura 	return (EOPNOTSUPP);
    278  1.1      oki }
    279  1.1      oki 
    280  1.5  minoura int
    281  1.5  minoura msioctl(dev, cmd, data, flag, p)
    282  1.5  minoura 	dev_t dev;
    283  1.5  minoura 	u_long cmd;
    284  1.5  minoura 	register caddr_t data;
    285  1.5  minoura 	int flag;
    286  1.5  minoura 	struct proc *p;
    287  1.1      oki {
    288  1.5  minoura 	struct ms_softc *ms;
    289  1.5  minoura 
    290  1.5  minoura 	ms = ms_cd.cd_devs[minor(dev)];
    291  1.5  minoura 
    292  1.5  minoura 	switch (cmd) {
    293  1.5  minoura 
    294  1.5  minoura 	case FIONBIO:		/* we will remove this someday (soon???) */
    295  1.5  minoura 		return (0);
    296  1.5  minoura 
    297  1.5  minoura 	case FIOASYNC:
    298  1.5  minoura 		ms->ms_events.ev_async = *(int *)data != 0;
    299  1.5  minoura 		return (0);
    300  1.5  minoura 
    301  1.5  minoura 	case TIOCSPGRP:
    302  1.5  minoura 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
    303  1.5  minoura 			return (EPERM);
    304  1.5  minoura 		return (0);
    305  1.5  minoura 
    306  1.5  minoura 	case VUIDGFORMAT:
    307  1.5  minoura 		/* we only do firm_events */
    308  1.5  minoura 		*(int *)data = VUID_FIRM_EVENT;
    309  1.5  minoura 		return (0);
    310  1.1      oki 
    311  1.5  minoura 	case VUIDSFORMAT:
    312  1.5  minoura 		if (*(int *)data != VUID_FIRM_EVENT)
    313  1.5  minoura 			return (EINVAL);
    314  1.5  minoura 		return (0);
    315  1.1      oki 	}
    316  1.5  minoura 	return (ENOTTY);
    317  1.1      oki }
    318  1.1      oki 
    319  1.5  minoura int
    320  1.5  minoura mspoll(dev, events, p)
    321  1.5  minoura 	dev_t dev;
    322  1.5  minoura 	int events;
    323  1.5  minoura 	struct proc *p;
    324  1.5  minoura {
    325  1.5  minoura 	struct ms_softc *ms;
    326  1.5  minoura 
    327  1.5  minoura 	ms = ms_cd.cd_devs[minor(dev)];
    328  1.5  minoura 	return (ev_poll(&ms->ms_events, events, p));
    329  1.5  minoura }
    330  1.5  minoura 
    331  1.5  minoura 
    332  1.5  minoura /****************************************************************
    333  1.5  minoura  * Middle layer (translator)
    334  1.5  minoura  ****************************************************************/
    335  1.5  minoura 
    336  1.5  minoura static void ms_input __P((struct ms_softc *, int c));
    337  1.5  minoura 
    338  1.5  minoura 
    339  1.5  minoura /*
    340  1.5  minoura  * Called by our ms_softint() routine on input.
    341  1.5  minoura  */
    342  1.5  minoura static void
    343  1.5  minoura ms_input(ms, c)
    344  1.5  minoura 	register struct ms_softc *ms;
    345  1.1      oki 	register int c;
    346  1.1      oki {
    347  1.1      oki 	register struct firm_event *fe;
    348  1.1      oki 	register int mb, ub, d, get, put, any;
    349  1.1      oki 	static const char to_one[] = { 1, 2, 3 };
    350  1.1      oki 	static const int to_id[] = { MS_LEFT, MS_RIGHT, MS_MIDDLE };
    351  1.1      oki 
    352  1.1      oki 	/*
    353  1.1      oki 	 * Discard input if not ready.  Drop sync on parity or framing
    354  1.1      oki 	 * error; gain sync on button byte.
    355  1.1      oki 	 */
    356  1.1      oki 	if (ms->ms_ready == 0)
    357  1.1      oki 		return;
    358  1.1      oki 
    359  1.5  minoura 	ms->ms_nodata = 0;
    360  1.1      oki 	/*
    361  1.1      oki 	 * Run the decode loop, adding to the current information.
    362  1.1      oki 	 * We add, rather than replace, deltas, so that if the event queue
    363  1.1      oki 	 * fills, we accumulate data for when it opens up again.
    364  1.1      oki 	 */
    365  1.1      oki 	switch (ms->ms_byteno) {
    366  1.1      oki 
    367  1.1      oki 	case -1:
    368  1.1      oki 		return;
    369  1.1      oki 
    370  1.1      oki 	case 0:
    371  1.1      oki 		/* buttons */
    372  1.1      oki 		ms->ms_byteno = 1;
    373  1.5  minoura 		ms->ms_mb = c & 0x3;
    374  1.1      oki 		return;
    375  1.1      oki 
    376  1.1      oki 	case 1:
    377  1.1      oki 		/* delta-x */
    378  1.1      oki 		ms->ms_byteno = 2;
    379  1.1      oki 		ms->ms_dx += (char)c;
    380  1.1      oki 		return;
    381  1.1      oki 
    382  1.1      oki 	case 2:
    383  1.1      oki 		/* delta-y */
    384  1.5  minoura 		ms->ms_byteno = -1;
    385  1.1      oki 		ms->ms_dy += (char)c;
    386  1.1      oki 		break;
    387  1.1      oki 
    388  1.1      oki 	default:
    389  1.5  minoura 		panic("ms_input");
    390  1.1      oki 		/* NOTREACHED */
    391  1.1      oki 	}
    392  1.1      oki 
    393  1.1      oki 	/*
    394  1.1      oki 	 * We have at least one event (mouse button, delta-X, or
    395  1.1      oki 	 * delta-Y; possibly all three, and possibly three separate
    396  1.1      oki 	 * button events).  Deliver these events until we are out
    397  1.1      oki 	 * of changes or out of room.  As events get delivered,
    398  1.1      oki 	 * mark them `unchanged'.
    399  1.1      oki 	 */
    400  1.1      oki 	any = 0;
    401  1.1      oki 	get = ms->ms_events.ev_get;
    402  1.1      oki 	put = ms->ms_events.ev_put;
    403  1.1      oki 	fe = &ms->ms_events.ev_q[put];
    404  1.1      oki 
    405  1.1      oki 	/* NEXT prepares to put the next event, backing off if necessary */
    406  1.1      oki #define	NEXT \
    407  1.1      oki 	if ((++put) % EV_QSIZE == get) { \
    408  1.1      oki 		put--; \
    409  1.1      oki 		goto out; \
    410  1.1      oki 	}
    411  1.1      oki 	/* ADVANCE completes the `put' of the event */
    412  1.1      oki #define	ADVANCE \
    413  1.1      oki 	fe++; \
    414  1.1      oki 	if (put >= EV_QSIZE) { \
    415  1.1      oki 		put = 0; \
    416  1.1      oki 		fe = &ms->ms_events.ev_q[0]; \
    417  1.1      oki 	} \
    418  1.1      oki 
    419  1.1      oki 	mb = ms->ms_mb;
    420  1.1      oki 	ub = ms->ms_ub;
    421  1.1      oki 	while ((d = mb ^ ub) != 0) {
    422  1.1      oki 		/*
    423  1.1      oki 		 * Mouse button change.  Convert up to three changes
    424  1.1      oki 		 * to the `first' change, and drop it into the event queue.
    425  1.1      oki 		 */
    426  1.1      oki 		NEXT;
    427  1.1      oki 		d = to_one[d - 1];		/* from 1..7 to {1,2,4} */
    428  1.1      oki 		fe->id = to_id[d - 1];		/* from {1,2,4} to ID */
    429  1.1      oki 		fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
    430  1.1      oki 		fe->time = time;
    431  1.1      oki 		ADVANCE;
    432  1.1      oki 		ub ^= d;
    433  1.5  minoura 		any++;
    434  1.1      oki 	}
    435  1.1      oki 	if (ms->ms_dx) {
    436  1.1      oki 		NEXT;
    437  1.1      oki 		fe->id = LOC_X_DELTA;
    438  1.1      oki 		fe->value = ms->ms_dx;
    439  1.1      oki 		fe->time = time;
    440  1.1      oki 		ADVANCE;
    441  1.1      oki 		ms->ms_dx = 0;
    442  1.5  minoura 		any++;
    443  1.1      oki 	}
    444  1.1      oki 	if (ms->ms_dy) {
    445  1.1      oki 		NEXT;
    446  1.1      oki 		fe->id = LOC_Y_DELTA;
    447  1.5  minoura 		fe->value = -ms->ms_dy;	/* XXX? */
    448  1.1      oki 		fe->time = time;
    449  1.1      oki 		ADVANCE;
    450  1.1      oki 		ms->ms_dy = 0;
    451  1.5  minoura 		any++;
    452  1.1      oki 	}
    453  1.1      oki out:
    454  1.1      oki 	if (any) {
    455  1.1      oki 		ms->ms_ub = ub;
    456  1.1      oki 		ms->ms_events.ev_put = put;
    457  1.1      oki 		EV_WAKEUP(&ms->ms_events);
    458  1.1      oki 	}
    459  1.1      oki }
    460  1.1      oki 
    461  1.5  minoura /****************************************************************
    462  1.5  minoura  * Interface to the lower layer (zscc)
    463  1.5  minoura  ****************************************************************/
    464  1.5  minoura 
    465  1.5  minoura static void ms_rxint __P((struct zs_chanstate *));
    466  1.6  mycroft static void ms_stint __P((struct zs_chanstate *, int));
    467  1.5  minoura static void ms_txint __P((struct zs_chanstate *));
    468  1.5  minoura static void ms_softint __P((struct zs_chanstate *));
    469  1.5  minoura 
    470  1.5  minoura static void
    471  1.5  minoura ms_rxint(cs)
    472  1.5  minoura 	register struct zs_chanstate *cs;
    473  1.1      oki {
    474  1.5  minoura 	register struct ms_softc *ms;
    475  1.5  minoura 	register int put, put_next;
    476  1.5  minoura 	register u_char c, rr1;
    477  1.5  minoura 
    478  1.5  minoura 	ms = cs->cs_private;
    479  1.5  minoura 	put = ms->ms_rbput;
    480  1.5  minoura 
    481  1.5  minoura 	/*
    482  1.5  minoura 	 * First read the status, because reading the received char
    483  1.5  minoura 	 * destroys the status of this char.
    484  1.5  minoura 	 */
    485  1.5  minoura 	rr1 = zs_read_reg(cs, 1);
    486  1.5  minoura 	c = zs_read_data(cs);
    487  1.5  minoura 
    488  1.5  minoura 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    489  1.5  minoura 		/* Clear the receive error. */
    490  1.5  minoura 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    491  1.5  minoura 	}
    492  1.5  minoura 
    493  1.5  minoura 	ms->ms_rbuf[put] = (c << 8) | rr1;
    494  1.5  minoura 	put_next = (put + 1) & MS_RX_RING_MASK;
    495  1.5  minoura 
    496  1.5  minoura 	/* Would overrun if increment makes (put==get). */
    497  1.5  minoura 	if (put_next == ms->ms_rbget) {
    498  1.5  minoura 		ms->ms_intr_flags |= INTR_RX_OVERRUN;
    499  1.5  minoura 	} else {
    500  1.5  minoura 		/* OK, really increment. */
    501  1.5  minoura 		put = put_next;
    502  1.5  minoura 	}
    503  1.5  minoura 
    504  1.5  minoura 	/* Done reading. */
    505  1.5  minoura 	ms->ms_rbput = put;
    506  1.5  minoura 
    507  1.5  minoura 	/* Ask for softint() call. */
    508  1.5  minoura 	cs->cs_softreq = 1;
    509  1.1      oki }
    510  1.5  minoura 
    511  1.5  minoura 
    512  1.5  minoura static void
    513  1.5  minoura ms_txint(cs)
    514  1.5  minoura 	register struct zs_chanstate *cs;
    515  1.1      oki {
    516  1.5  minoura 	register struct ms_softc *ms;
    517  1.1      oki 
    518  1.5  minoura 	ms = cs->cs_private;
    519  1.5  minoura 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    520  1.5  minoura 	ms->ms_intr_flags |= INTR_TX_EMPTY;
    521  1.5  minoura 	/* Ask for softint() call. */
    522  1.5  minoura 	cs->cs_softreq = 1;
    523  1.1      oki }
    524  1.1      oki 
    525  1.5  minoura 
    526  1.5  minoura static void
    527  1.6  mycroft ms_stint(cs, force)
    528  1.5  minoura 	register struct zs_chanstate *cs;
    529  1.6  mycroft 	int force;
    530  1.1      oki {
    531  1.5  minoura 	register struct ms_softc *ms;
    532  1.5  minoura 	register int rr0;
    533  1.1      oki 
    534  1.5  minoura 	ms = cs->cs_private;
    535  1.5  minoura 
    536  1.5  minoura 	rr0 = zs_read_csr(cs);
    537  1.5  minoura 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    538  1.1      oki 
    539  1.5  minoura 	/*
    540  1.5  minoura 	 * We have to accumulate status line changes here.
    541  1.5  minoura 	 * Otherwise, if we get multiple status interrupts
    542  1.5  minoura 	 * before the softint runs, we could fail to notice
    543  1.5  minoura 	 * some status line changes in the softint routine.
    544  1.5  minoura 	 * Fix from Bill Studenmund, October 1996.
    545  1.5  minoura 	 */
    546  1.5  minoura 	cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
    547  1.5  minoura 	cs->cs_rr0 = rr0;
    548  1.5  minoura 	ms->ms_intr_flags |= INTR_ST_CHECK;
    549  1.1      oki 
    550  1.5  minoura 	/* Ask for softint() call. */
    551  1.5  minoura 	cs->cs_softreq = 1;
    552  1.1      oki }
    553  1.1      oki 
    554  1.5  minoura 
    555  1.5  minoura static void
    556  1.5  minoura ms_softint(cs)
    557  1.5  minoura 	struct zs_chanstate *cs;
    558  1.1      oki {
    559  1.5  minoura 	register struct ms_softc *ms;
    560  1.5  minoura 	register int get, c, s;
    561  1.5  minoura 	int intr_flags;
    562  1.5  minoura 	register u_short ring_data;
    563  1.5  minoura 
    564  1.5  minoura 	ms = cs->cs_private;
    565  1.5  minoura 
    566  1.5  minoura 	/* Atomically get and clear flags. */
    567  1.5  minoura 	s = splzs();
    568  1.5  minoura 	intr_flags = ms->ms_intr_flags;
    569  1.5  minoura 	ms->ms_intr_flags = 0;
    570  1.1      oki 
    571  1.5  minoura 	/* Now lower to spltty for the rest. */
    572  1.5  minoura 	(void) spltty();
    573  1.1      oki 
    574  1.5  minoura 	/*
    575  1.5  minoura 	 * Copy data from the receive ring to the event layer.
    576  1.5  minoura 	 */
    577  1.5  minoura 	get = ms->ms_rbget;
    578  1.5  minoura 	while (get != ms->ms_rbput) {
    579  1.5  minoura 		ring_data = ms->ms_rbuf[get];
    580  1.5  minoura 		get = (get + 1) & MS_RX_RING_MASK;
    581  1.5  minoura 
    582  1.5  minoura 		/* low byte of ring_data is rr1 */
    583  1.5  minoura 		c = (ring_data >> 8) & 0xff;
    584  1.5  minoura 
    585  1.5  minoura 		if (ring_data & ZSRR1_DO)
    586  1.5  minoura 			intr_flags |= INTR_RX_OVERRUN;
    587  1.5  minoura 		if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
    588  1.5  minoura 			log(LOG_ERR, "%s: input error (0x%x)\n",
    589  1.5  minoura 				ms->ms_dev.dv_xname, ring_data);
    590  1.5  minoura 			c = -1;	/* signal input error */
    591  1.5  minoura 		}
    592  1.1      oki 
    593  1.5  minoura 		/* Pass this up to the "middle" layer. */
    594  1.5  minoura 		ms_input(ms, c);
    595  1.5  minoura 	}
    596  1.5  minoura 	if (intr_flags & INTR_RX_OVERRUN) {
    597  1.5  minoura 		log(LOG_ERR, "%s: input overrun\n",
    598  1.5  minoura 		    ms->ms_dev.dv_xname);
    599  1.5  minoura 	}
    600  1.5  minoura 	ms->ms_rbget = get;
    601  1.1      oki 
    602  1.5  minoura 	if (intr_flags & INTR_TX_EMPTY) {
    603  1.5  minoura 		/*
    604  1.5  minoura 		 * Transmit done.  (Not expected.)
    605  1.5  minoura 		 */
    606  1.5  minoura 		log(LOG_ERR, "%s: transmit interrupt?\n",
    607  1.5  minoura 		    ms->ms_dev.dv_xname);
    608  1.5  minoura 	}
    609  1.1      oki 
    610  1.5  minoura 	if (intr_flags & INTR_ST_CHECK) {
    611  1.5  minoura 		/*
    612  1.5  minoura 		 * Status line change.  (Not expected.)
    613  1.5  minoura 		 */
    614  1.5  minoura 		log(LOG_ERR, "%s: status interrupt?\n",
    615  1.5  minoura 		    ms->ms_dev.dv_xname);
    616  1.5  minoura 		cs->cs_rr0_delta = 0;
    617  1.1      oki 	}
    618  1.5  minoura 
    619  1.5  minoura 	splx(s);
    620  1.1      oki }
    621  1.1      oki 
    622  1.5  minoura struct zsops zsops_ms = {
    623  1.5  minoura 	ms_rxint,	/* receive char available */
    624  1.5  minoura 	ms_stint,	/* external/status */
    625  1.5  minoura 	ms_txint,	/* xmit buffer empty */
    626  1.5  minoura 	ms_softint,	/* process software interrupt */
    627  1.5  minoura };
    628  1.5  minoura 
    629  1.5  minoura 
    630  1.5  minoura static void
    631  1.5  minoura ms_trigger (cs, onoff)
    632  1.5  minoura 	struct zs_chanstate *cs;
    633  1.5  minoura 	int onoff;
    634  1.1      oki {
    635  1.5  minoura 	/* for front connected one */
    636  1.5  minoura 	if (onoff)
    637  1.5  minoura 		cs->cs_preg[5] |= ZSWR5_RTS;
    638  1.5  minoura 	else
    639  1.5  minoura 		cs->cs_preg[5] &= ~ZSWR5_RTS;
    640  1.5  minoura 	cs->cs_creg[5] = cs->cs_preg[5];
    641  1.5  minoura 	zs_write_reg(cs, 5, cs->cs_preg[5]);
    642  1.5  minoura 
    643  1.5  minoura 	/* for keyborad connected one */
    644  1.5  minoura 	while (!(mfp.tsr & MFP_TSR_BE));
    645  1.5  minoura 	mfp.udr = onoff | 0x40;
    646  1.1      oki }
    647  1.1      oki 
    648  1.5  minoura /*
    649  1.5  minoura  * mouse timer interrupt.
    650  1.5  minoura  * called after system tick interrupt is done.
    651  1.5  minoura  */
    652  1.2      oki void
    653  1.5  minoura ms_modem(void)
    654  1.5  minoura {
    655  1.5  minoura 	struct ms_softc *ms = ms_cd.cd_devs[0];
    656  1.5  minoura 	int s;
    657  1.5  minoura 
    658  1.5  minoura 	if (!ms->ms_ready)
    659  1.5  minoura 		return;
    660  1.5  minoura 	if (ms->ms_nodata++ > 300) { /* XXX */
    661  1.5  minoura 		log(LOG_ERR, "%s: no data for 3 secs. resetting.\n",
    662  1.5  minoura 		    ms->ms_dev.dv_xname);
    663  1.5  minoura 		ms->ms_nodata = 0;
    664  1.5  minoura 		ms->ms_byteno = -1;
    665  1.5  minoura 		ms->ms_rts = 1;
    666  1.5  minoura 		ms_trigger(ms->ms_cs, 1);
    667  1.5  minoura 		return;
    668  1.5  minoura 	}
    669  1.5  minoura 
    670  1.5  minoura 	s = splzs();
    671  1.5  minoura 	if (ms->ms_rts) {
    672  1.5  minoura 		if (ms->ms_byteno == -1) {
    673  1.5  minoura 			/* start next sequence */
    674  1.5  minoura 			ms->ms_rts = 0;
    675  1.5  minoura 			ms_trigger(ms->ms_cs, ms->ms_rts);
    676  1.5  minoura 			ms->ms_byteno = 0;
    677  1.5  minoura 		}
    678  1.5  minoura 	} else {
    679  1.5  minoura 		ms->ms_rts = 1;
    680  1.5  minoura 		ms_trigger(ms->ms_cs, ms->ms_rts);
    681  1.5  minoura 	}
    682  1.5  minoura 	splx(s);
    683  1.5  minoura }
    684