Home | History | Annotate | Line # | Download | only in tc
      1 /*	$NetBSD: zsms.c,v 1.20 2021/08/07 16:19:16 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratory.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
     41  */
     42 
     43 /*
     44  * VSXXX mice attached with channel A of the 1st SCC
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: zsms.c,v 1.20 2021/08/07 16:19:16 thorpej Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/device.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/syslog.h>
     55 #include <sys/kernel.h>
     56 #include <sys/proc.h>
     57 #include <sys/tty.h>
     58 
     59 #include <dev/ic/z8530reg.h>
     60 #include <machine/z8530var.h>
     61 
     62 #include <dev/dec/lk201.h>
     63 
     64 #include <dev/wscons/wsconsio.h>
     65 #include <dev/wscons/wsmousevar.h>
     66 
     67 #include "locators.h"
     68 
     69 /*
     70  * How many input characters we can buffer.
     71  * The port-specific var.h may override this.
     72  * Note: must be a power of two!
     73  */
     74 #define	ZSMS_RX_RING_SIZE	256
     75 #define ZSMS_RX_RING_MASK (ZSMS_RX_RING_SIZE-1)
     76 /*
     77  * Output buffer.  Only need a few chars.
     78  */
     79 #define	ZSMS_TX_RING_SIZE	16
     80 #define ZSMS_TX_RING_MASK (ZSMS_TX_RING_SIZE-1)
     81 
     82 #define ZSMS_BPS 4800
     83 
     84 struct zsms_softc {		/* driver status information */
     85 	device_t zsms_dev;	/* required first: base device */
     86 	struct	zs_chanstate *zsms_cs;
     87 
     88 	/* Flags to communicate with zsms_softintr() */
     89 	volatile int zsms_intr_flags;
     90 #define	INTR_RX_OVERRUN 1
     91 #define INTR_TX_EMPTY   2
     92 #define INTR_ST_CHECK   4
     93 
     94 	/*
     95 	 * The receive ring buffer.
     96 	 */
     97 	u_int	zsms_rbget;	/* ring buffer `get' index */
     98 	volatile u_int	zsms_rbput;	/* ring buffer `put' index */
     99 	u_short	zsms_rbuf[ZSMS_RX_RING_SIZE]; /* rr1, data pairs */
    100 
    101 	int sc_enabled;		/* input enabled? */
    102 	int sc_selftest;	/* self test in progress */
    103 
    104 	int inputstate;
    105 	u_int buttons;
    106 	int dx, dy;
    107 
    108 	device_t sc_wsmousedev;
    109 };
    110 
    111 static struct zsops zsops_zsms;
    112 
    113 static int  zsms_match(device_t, cfdata_t, void *);
    114 static void zsms_attach(device_t, device_t, void *);
    115 static void zsms_input(void *, int);
    116 
    117 CFATTACH_DECL_NEW(zsms, sizeof(struct zsms_softc),
    118     zsms_match, zsms_attach, NULL, NULL);
    119 
    120 static int  zsms_enable(void *);
    121 static int  zsms_ioctl(void *, u_long, void *, int, struct lwp *);
    122 static void zsms_disable(void *);
    123 
    124 static const struct wsmouse_accessops zsms_accessops = {
    125 	zsms_enable,
    126 	zsms_ioctl,
    127 	zsms_disable,
    128 };
    129 
    130 static int
    131 zsms_match(device_t parent, cfdata_t cf, void *aux)
    132 {
    133 	struct zsc_attach_args *args = aux;
    134 
    135 	/* Exact match is better than wildcard. */
    136 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
    137 		return 2;
    138 
    139 	/* This driver accepts wildcard. */
    140 	if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
    141 		return 1;
    142 
    143 	return 0;
    144 }
    145 
    146 static void
    147 zsms_attach(device_t parent, device_t self, void *aux)
    148 {
    149 	struct zsc_softc *zsc = device_private(parent);
    150 	struct zsms_softc *zsms = device_private(self);
    151 	struct zsc_attach_args *args = aux;
    152 	struct zs_chanstate *cs;
    153 	struct wsmousedev_attach_args a;
    154 	int s;
    155 
    156 	zsms->zsms_dev = self;
    157 	cs = zsc->zsc_cs[args->channel];
    158 	cs->cs_private = zsms;
    159 	cs->cs_ops = &zsops_zsms;
    160 	zsms->zsms_cs = cs;
    161 
    162 	aprint_normal("\n");
    163 
    164 	/* Initialize the speed, etc. */
    165 	s = splzs();
    166 	/* May need reset... */
    167 	zs_write_reg(cs, 9, ZSWR9_A_RESET);
    168 	/* These are OK as set by zscc: WR3, WR5 */
    169 	/* We don't care about status or tx interrupts. */
    170 	cs->cs_preg[1] = ZSWR1_RIE;
    171 	(void) zs_set_speed(cs, ZSMS_BPS);
    172 
    173 	/* mouse wants odd parity */
    174 	cs->cs_preg[4] |= ZSWR4_PARENB;
    175 	/* cs->cs_preg[4] &= ~ZSWR4_EVENP; (no-op) */
    176 
    177 	zs_loadchannelregs(cs);
    178 	splx(s);
    179 
    180 	a.accessops = &zsms_accessops;
    181 	a.accesscookie = zsms;
    182 
    183 	zsms->sc_enabled = 0;
    184 	zsms->sc_selftest = 0;
    185 	zsms->sc_wsmousedev = config_found(self, &a, wsmousedevprint,
    186 	    CFARGS_NONE);
    187 }
    188 
    189 static int
    190 zsms_enable(void *v)
    191 {
    192 	struct zsms_softc *sc = v;
    193 
    194 	if (sc->sc_enabled)
    195 		return EBUSY;
    196 
    197 	sc->sc_selftest = 4; /* wait for 4 byte reply upto 1/2 sec */
    198 	zs_write_data(sc->zsms_cs,  MOUSE_SELF_TEST);
    199 	(void)tsleep(zsms_enable, TTIPRI, "zsmsopen", hz / 2);
    200 	if (sc->sc_selftest != 0) {
    201 		sc->sc_selftest = 0;
    202 		return ENXIO;
    203 	}
    204 	/* XXX DELAY before mode set? */
    205 	zs_write_data(sc->zsms_cs, MOUSE_INCREMENTAL);
    206 	sc->sc_enabled = 1;
    207 	sc->inputstate = 0;
    208 	return 0;
    209 }
    210 
    211 static void
    212 zsms_disable(void *v)
    213 {
    214 	struct zsms_softc *sc = v;
    215 
    216 	sc->sc_enabled = 0;
    217 }
    218 
    219 static int
    220 zsms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    221 {
    222 
    223 	if (cmd == WSMOUSEIO_GTYPE) {
    224 		*(u_int *)data = WSMOUSE_TYPE_VSXXX;
    225 		return 0;
    226 	}
    227 	return EPASSTHROUGH;
    228 }
    229 
    230 static void
    231 zsms_input(void *vsc, int data)
    232 {
    233 	struct zsms_softc *sc = vsc;
    234 
    235 	if (sc->sc_enabled == 0) {
    236 		if (sc->sc_selftest > 0) {
    237 			sc->sc_selftest -= 1;
    238 			if (sc->sc_selftest == 0)
    239 				wakeup(zsms_enable);
    240 		}
    241 		return;
    242 	}
    243 
    244 	if ((data & MOUSE_START_FRAME) != 0)
    245 		sc->inputstate = 1;
    246 	else
    247 		sc->inputstate++;
    248 
    249 	if (sc->inputstate == 1) {
    250 		/* LMR -> RML: wsevents counts 0 for the left-most */
    251 		sc->buttons = data & 02;
    252 		if (data & 01)
    253 			sc->buttons |= 04;
    254 		if (data & 04)
    255 			sc->buttons |= 01;
    256 		sc->dx = data & MOUSE_X_SIGN;
    257 		sc->dy = data & MOUSE_Y_SIGN;
    258 	} else if (sc->inputstate == 2) {
    259 		if (sc->dx == 0)
    260 			sc->dx = -data;
    261 		else
    262 			sc->dx = data;
    263 	} else if (sc->inputstate == 3) {
    264 		sc->inputstate = 0;
    265 		if (sc->dy == 0)
    266 			sc->dy = -data;
    267 		else
    268 			sc->dy = data;
    269 		wsmouse_input(sc->sc_wsmousedev,
    270 				sc->buttons,
    271 		    		sc->dx, sc->dy, 0, 0,
    272 				WSMOUSE_INPUT_DELTA);
    273 	}
    274 
    275 	return;
    276 }
    277 
    278 /****************************************************************
    279  * Interface to the lower layer (zscc)
    280  ****************************************************************/
    281 
    282 static void zsms_rxint(struct zs_chanstate *);
    283 static void zsms_stint(struct zs_chanstate *, int);
    284 static void zsms_txint(struct zs_chanstate *);
    285 static void zsms_softint(struct zs_chanstate *);
    286 
    287 static void
    288 zsms_rxint(struct zs_chanstate *cs)
    289 {
    290 	struct zsms_softc *zsms;
    291 	int put, put_next;
    292 	uint8_t c, rr1;
    293 
    294 	zsms = cs->cs_private;
    295 	put = zsms->zsms_rbput;
    296 
    297 	/*
    298 	 * First read the status, because reading the received char
    299 	 * destroys the status of this char.
    300 	 */
    301 	rr1 = zs_read_reg(cs, 1);
    302 	c = zs_read_data(cs);
    303 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    304 		/* Clear the receive error. */
    305 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    306 	}
    307 
    308 	zsms->zsms_rbuf[put] = (c << 8) | rr1;
    309 	put_next = (put + 1) & ZSMS_RX_RING_MASK;
    310 
    311 	/* Would overrun if increment makes (put==get). */
    312 	if (put_next == zsms->zsms_rbget) {
    313 		zsms->zsms_intr_flags |= INTR_RX_OVERRUN;
    314 	} else {
    315 		/* OK, really increment. */
    316 		put = put_next;
    317 	}
    318 
    319 	/* Done reading. */
    320 	zsms->zsms_rbput = put;
    321 
    322 	/* Ask for softint() call. */
    323 	cs->cs_softreq = 1;
    324 }
    325 
    326 
    327 static void
    328 zsms_txint(struct zs_chanstate *cs)
    329 {
    330 	struct zsms_softc *zsms;
    331 
    332 	zsms = cs->cs_private;
    333 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    334 	zsms->zsms_intr_flags |= INTR_TX_EMPTY;
    335 	/* Ask for softint() call. */
    336 	cs->cs_softreq = 1;
    337 }
    338 
    339 
    340 static void
    341 zsms_stint(struct zs_chanstate *cs, int force)
    342 {
    343 	struct zsms_softc *zsms;
    344 	int rr0;
    345 
    346 	zsms = cs->cs_private;
    347 
    348 	rr0 = zs_read_csr(cs);
    349 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    350 
    351 	/*
    352 	 * We have to accumulate status line changes here.
    353 	 * Otherwise, if we get multiple status interrupts
    354 	 * before the softint runs, we could fail to notice
    355 	 * some status line changes in the softint routine.
    356 	 * Fix from Bill Studenmund, October 1996.
    357 	 */
    358 	cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
    359 	cs->cs_rr0 = rr0;
    360 	zsms->zsms_intr_flags |= INTR_ST_CHECK;
    361 
    362 	/* Ask for softint() call. */
    363 	cs->cs_softreq = 1;
    364 }
    365 
    366 
    367 static void
    368 zsms_softint(struct zs_chanstate *cs)
    369 {
    370 	struct zsms_softc *zsms;
    371 	int get, c, s;
    372 	int intr_flags;
    373 	u_short ring_data;
    374 
    375 	zsms = cs->cs_private;
    376 
    377 	/* Atomically get and clear flags. */
    378 	s = splzs();
    379 	intr_flags = zsms->zsms_intr_flags;
    380 	zsms->zsms_intr_flags = 0;
    381 
    382 	/* Now lower to spltty for the rest. */
    383 	(void) spltty();
    384 
    385 	/*
    386 	 * Copy data from the receive ring to the event layer.
    387 	 */
    388 	get = zsms->zsms_rbget;
    389 	while (get != zsms->zsms_rbput) {
    390 		ring_data = zsms->zsms_rbuf[get];
    391 		get = (get + 1) & ZSMS_RX_RING_MASK;
    392 
    393 		/* low byte of ring_data is rr1 */
    394 		c = (ring_data >> 8) & 0xff;
    395 
    396 		if (ring_data & ZSRR1_DO)
    397 			intr_flags |= INTR_RX_OVERRUN;
    398 		if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
    399 			log(LOG_ERR, "%s: input error (0x%x)\n",
    400 			    device_xname(zsms->zsms_dev), ring_data);
    401 			c = -1;	/* signal input error */
    402 		}
    403 
    404 		/* Pass this up to the "middle" layer. */
    405 		zsms_input(zsms, c);
    406 	}
    407 	if (intr_flags & INTR_RX_OVERRUN) {
    408 		log(LOG_ERR, "%s: input overrun\n",
    409 		    device_xname(zsms->zsms_dev));
    410 	}
    411 	zsms->zsms_rbget = get;
    412 
    413 	if (intr_flags & INTR_TX_EMPTY) {
    414 		/*
    415 		 * Transmit done.  (Not expected.)
    416 		 */
    417 		log(LOG_ERR, "%s: transmit interrupt?\n",
    418 		    device_xname(zsms->zsms_dev));
    419 	}
    420 
    421 	if (intr_flags & INTR_ST_CHECK) {
    422 		/*
    423 		 * Status line change.  (Not expected.)
    424 		 */
    425 		log(LOG_ERR, "%s: status interrupt?\n",
    426 		    device_xname(zsms->zsms_dev));
    427 		cs->cs_rr0_delta = 0;
    428 	}
    429 
    430 	splx(s);
    431 }
    432 
    433 static struct zsops zsops_zsms = {
    434 	zsms_rxint,	/* receive char available */
    435 	zsms_stint,	/* external/status */
    436 	zsms_txint,	/* xmit buffer empty */
    437 	zsms_softint,	/* process software interrupt */
    438 };
    439