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