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