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