1 /* $NetBSD: zsms.c,v 1.16 2007/03/04 06:02:47 christos 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.16 2007/03/04 06:02:47 christos 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 struct device 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 struct device *sc_wsmousedev; 109 }; 110 111 static struct zsops zsops_zsms; 112 113 static int zsms_match(struct device *, struct cfdata *, void *); 114 static void zsms_attach(struct device *, struct device *, void *); 115 static void zsms_input(void *, int); 116 117 CFATTACH_DECL(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(struct device *parent, struct cfdata *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(struct device *parent, struct device *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 cs = zsc->zsc_cs[args->channel]; 157 cs->cs_private = zsms; 158 cs->cs_ops = &zsops_zsms; 159 zsms->zsms_cs = cs; 160 161 printf("\n"); 162 163 /* Initialize the speed, etc. */ 164 s = splzs(); 165 /* May need reset... */ 166 zs_write_reg(cs, 9, ZSWR9_A_RESET); 167 /* These are OK as set by zscc: WR3, WR5 */ 168 /* We don't care about status or tx interrupts. */ 169 cs->cs_preg[1] = ZSWR1_RIE; 170 (void) zs_set_speed(cs, ZSMS_BPS); 171 172 /* mouse wants odd parity */ 173 cs->cs_preg[4] |= ZSWR4_PARENB; 174 /* cs->cs_preg[4] &= ~ZSWR4_EVENP; (no-op) */ 175 176 zs_loadchannelregs(cs); 177 splx(s); 178 179 a.accessops = &zsms_accessops; 180 a.accesscookie = zsms; 181 182 zsms->sc_enabled = 0; 183 zsms->sc_selftest = 0; 184 zsms->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 185 } 186 187 static int 188 zsms_enable(void *v) 189 { 190 struct zsms_softc *sc = v; 191 192 if (sc->sc_enabled) 193 return EBUSY; 194 195 sc->sc_selftest = 4; /* wait for 4 byte reply upto 1/2 sec */ 196 zs_write_data(sc->zsms_cs, MOUSE_SELF_TEST); 197 (void)tsleep(zsms_enable, TTIPRI, "zsmsopen", hz / 2); 198 if (sc->sc_selftest != 0) { 199 sc->sc_selftest = 0; 200 return ENXIO; 201 } 202 /* XXX DELAY before mode set? */ 203 zs_write_data(sc->zsms_cs, MOUSE_INCREMENTAL); 204 sc->sc_enabled = 1; 205 sc->inputstate = 0; 206 return 0; 207 } 208 209 static void 210 zsms_disable(void *v) 211 { 212 struct zsms_softc *sc = v; 213 214 sc->sc_enabled = 0; 215 } 216 217 static int 218 zsms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 219 { 220 221 if (cmd == WSMOUSEIO_GTYPE) { 222 *(u_int *)data = WSMOUSE_TYPE_VSXXX; 223 return 0; 224 } 225 return EPASSTHROUGH; 226 } 227 228 static void 229 zsms_input(void *vsc, int data) 230 { 231 struct zsms_softc *sc = vsc; 232 233 if (sc->sc_enabled == 0) { 234 if (sc->sc_selftest > 0) { 235 sc->sc_selftest -= 1; 236 if (sc->sc_selftest == 0) 237 wakeup(zsms_enable); 238 } 239 return; 240 } 241 242 if ((data & MOUSE_START_FRAME) != 0) 243 sc->inputstate = 1; 244 else 245 sc->inputstate++; 246 247 if (sc->inputstate == 1) { 248 /* LMR -> RML: wsevents counts 0 for the left-most */ 249 sc->buttons = data & 02; 250 if (data & 01) 251 sc->buttons |= 04; 252 if (data & 04) 253 sc->buttons |= 01; 254 sc->dx = data & MOUSE_X_SIGN; 255 sc->dy = data & MOUSE_Y_SIGN; 256 } else if (sc->inputstate == 2) { 257 if (sc->dx == 0) 258 sc->dx = -data; 259 else 260 sc->dx = data; 261 } else if (sc->inputstate == 3) { 262 sc->inputstate = 0; 263 if (sc->dy == 0) 264 sc->dy = -data; 265 else 266 sc->dy = data; 267 wsmouse_input(sc->sc_wsmousedev, 268 sc->buttons, 269 sc->dx, sc->dy, 0, 0, 270 WSMOUSE_INPUT_DELTA); 271 } 272 273 return; 274 } 275 276 /**************************************************************** 277 * Interface to the lower layer (zscc) 278 ****************************************************************/ 279 280 static void zsms_rxint(struct zs_chanstate *); 281 static void zsms_stint(struct zs_chanstate *, int); 282 static void zsms_txint(struct zs_chanstate *); 283 static void zsms_softint(struct zs_chanstate *); 284 285 static void 286 zsms_rxint(struct zs_chanstate *cs) 287 { 288 struct zsms_softc *zsms; 289 int put, put_next; 290 u_char c, rr1; 291 292 zsms = cs->cs_private; 293 put = zsms->zsms_rbput; 294 295 /* 296 * First read the status, because reading the received char 297 * destroys the status of this char. 298 */ 299 rr1 = zs_read_reg(cs, 1); 300 c = zs_read_data(cs); 301 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) { 302 /* Clear the receive error. */ 303 zs_write_csr(cs, ZSWR0_RESET_ERRORS); 304 } 305 306 zsms->zsms_rbuf[put] = (c << 8) | rr1; 307 put_next = (put + 1) & ZSMS_RX_RING_MASK; 308 309 /* Would overrun if increment makes (put==get). */ 310 if (put_next == zsms->zsms_rbget) { 311 zsms->zsms_intr_flags |= INTR_RX_OVERRUN; 312 } else { 313 /* OK, really increment. */ 314 put = put_next; 315 } 316 317 /* Done reading. */ 318 zsms->zsms_rbput = put; 319 320 /* Ask for softint() call. */ 321 cs->cs_softreq = 1; 322 } 323 324 325 static void 326 zsms_txint(struct zs_chanstate *cs) 327 { 328 struct zsms_softc *zsms; 329 330 zsms = cs->cs_private; 331 zs_write_csr(cs, ZSWR0_RESET_TXINT); 332 zsms->zsms_intr_flags |= INTR_TX_EMPTY; 333 /* Ask for softint() call. */ 334 cs->cs_softreq = 1; 335 } 336 337 338 static void 339 zsms_stint(struct zs_chanstate *cs, int force) 340 { 341 struct zsms_softc *zsms; 342 int rr0; 343 344 zsms = cs->cs_private; 345 346 rr0 = zs_read_csr(cs); 347 zs_write_csr(cs, ZSWR0_RESET_STATUS); 348 349 /* 350 * We have to accumulate status line changes here. 351 * Otherwise, if we get multiple status interrupts 352 * before the softint runs, we could fail to notice 353 * some status line changes in the softint routine. 354 * Fix from Bill Studenmund, October 1996. 355 */ 356 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0); 357 cs->cs_rr0 = rr0; 358 zsms->zsms_intr_flags |= INTR_ST_CHECK; 359 360 /* Ask for softint() call. */ 361 cs->cs_softreq = 1; 362 } 363 364 365 static void 366 zsms_softint(struct zs_chanstate *cs) 367 { 368 struct zsms_softc *zsms; 369 int get, c, s; 370 int intr_flags; 371 u_short ring_data; 372 373 zsms = cs->cs_private; 374 375 /* Atomically get and clear flags. */ 376 s = splzs(); 377 intr_flags = zsms->zsms_intr_flags; 378 zsms->zsms_intr_flags = 0; 379 380 /* Now lower to spltty for the rest. */ 381 (void) spltty(); 382 383 /* 384 * Copy data from the receive ring to the event layer. 385 */ 386 get = zsms->zsms_rbget; 387 while (get != zsms->zsms_rbput) { 388 ring_data = zsms->zsms_rbuf[get]; 389 get = (get + 1) & ZSMS_RX_RING_MASK; 390 391 /* low byte of ring_data is rr1 */ 392 c = (ring_data >> 8) & 0xff; 393 394 if (ring_data & ZSRR1_DO) 395 intr_flags |= INTR_RX_OVERRUN; 396 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) { 397 log(LOG_ERR, "%s: input error (0x%x)\n", 398 zsms->zsms_dev.dv_xname, ring_data); 399 c = -1; /* signal input error */ 400 } 401 402 /* Pass this up to the "middle" layer. */ 403 zsms_input(zsms, c); 404 } 405 if (intr_flags & INTR_RX_OVERRUN) { 406 log(LOG_ERR, "%s: input overrun\n", 407 zsms->zsms_dev.dv_xname); 408 } 409 zsms->zsms_rbget = get; 410 411 if (intr_flags & INTR_TX_EMPTY) { 412 /* 413 * Transmit done. (Not expected.) 414 */ 415 log(LOG_ERR, "%s: transmit interrupt?\n", 416 zsms->zsms_dev.dv_xname); 417 } 418 419 if (intr_flags & INTR_ST_CHECK) { 420 /* 421 * Status line change. (Not expected.) 422 */ 423 log(LOG_ERR, "%s: status interrupt?\n", 424 zsms->zsms_dev.dv_xname); 425 cs->cs_rr0_delta = 0; 426 } 427 428 splx(s); 429 } 430 431 static struct zsops zsops_zsms = { 432 zsms_rxint, /* receive char available */ 433 zsms_stint, /* external/status */ 434 zsms_txint, /* xmit buffer empty */ 435 zsms_softint, /* process software interrupt */ 436 }; 437