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