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