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