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