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