1 /* $NetBSD: ms.c,v 1.34 2014/07/25 08:10:35 dholland 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.34 2014/07/25 08:10:35 dholland 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 * Mouse state. A SHARP X1/X680x0 mouse is a fairly simple device, 91 * producing three-byte blobs of the form: 92 * 93 * b dx dy 94 * 95 * where b is the button state, encoded as 0x80|(buttons)---there are 96 * two buttons (2=left, 1=right)---and dx,dy are X and Y delta values. 97 * 98 * It needs a trigger for the transmission. When zs RTS negated, the 99 * mouse begins the sequence. RTS assertion has no effect. 100 */ 101 struct ms_softc { 102 device_t ms_dev; /* required first: base device */ 103 struct zs_chanstate *ms_cs; 104 105 struct callout ms_modem_ch; 106 107 /* Flags to communicate with ms_softintr() */ 108 volatile int ms_intr_flags; 109 #define INTR_RX_OVERRUN 1 110 #define INTR_TX_EMPTY 2 111 #define INTR_ST_CHECK 4 112 113 /* 114 * The receive ring buffer. 115 */ 116 u_int ms_rbget; /* ring buffer `get' index */ 117 volatile u_int ms_rbput; /* ring buffer `put' index */ 118 u_short ms_rbuf[MS_RX_RING_SIZE]; /* rr1, data pairs */ 119 120 /* 121 * State of input translator 122 */ 123 short ms_byteno; /* input byte number, for decode */ 124 char ms_mb; /* mouse button state */ 125 char ms_ub; /* user button state */ 126 int ms_dx; /* delta-x */ 127 int ms_dy; /* delta-y */ 128 int ms_rts; /* MSCTRL */ 129 int ms_nodata; 130 131 /* 132 * State of upper interface. 133 */ 134 volatile int ms_ready; /* event queue is ready */ 135 struct evvar ms_events; /* event queue state */ 136 kmutex_t ms_lock; 137 } ms_softc; 138 139 static int ms_match(device_t, cfdata_t, void *); 140 static void ms_attach(device_t, device_t, void *); 141 static void ms_trigger(struct zs_chanstate *, int); 142 void ms_modem(void *); 143 144 CFATTACH_DECL_NEW(ms, sizeof(struct ms_softc), 145 ms_match, ms_attach, NULL, NULL); 146 147 static void ms_rxint(struct zs_chanstate *); 148 static void ms_stint(struct zs_chanstate *, int); 149 static void ms_txint(struct zs_chanstate *); 150 static void ms_softint(struct zs_chanstate *); 151 static void ms_input(struct ms_softc *, int); 152 153 struct zsops zsops_ms = { 154 ms_rxint, /* receive char available */ 155 ms_stint, /* external/status */ 156 ms_txint, /* xmit buffer empty */ 157 ms_softint, /* process software interrupt */ 158 }; 159 160 dev_type_open(msopen); 161 dev_type_close(msclose); 162 dev_type_read(msread); 163 dev_type_ioctl(msioctl); 164 dev_type_poll(mspoll); 165 dev_type_kqfilter(mskqfilter); 166 167 const struct cdevsw ms_cdevsw ={ 168 .d_open = msopen, 169 .d_close = msclose, 170 .d_read = msread, 171 .d_write = nowrite, 172 .d_ioctl = msioctl, 173 .d_stop = nostop, 174 .d_tty = notty, 175 .d_poll = mspoll, 176 .d_mmap = nommap, 177 .d_kqfilter = mskqfilter, 178 .d_discard = nodiscard, 179 .d_flag = 0 180 }; 181 182 /* 183 * ms_match: how is this zs channel configured? 184 */ 185 int 186 ms_match(device_t parent, cfdata_t cf, void *aux) 187 { 188 struct zsc_attach_args *args = aux; 189 struct zsc_softc *zsc = device_private(parent); 190 191 /* Exact match required for the mouse. */ 192 if (cf->cf_loc[ZSCCF_CHANNEL] != args->channel) 193 return 0; 194 if (args->channel != 1) 195 return 0; 196 if (&zsc->zsc_addr->zs_chan_b != (struct zschan *)ZSMS_PHYSADDR) 197 return 0; 198 199 return 2; 200 } 201 202 void 203 ms_attach(device_t parent, device_t self, void *aux) 204 { 205 struct ms_softc *ms = device_private(self); 206 struct zsc_softc *zsc = device_private(parent); 207 struct zs_chanstate *cs; 208 int reset; 209 210 ms->ms_dev = self; 211 callout_init(&ms->ms_modem_ch, 0); 212 mutex_init(&ms->ms_lock, MUTEX_DEFAULT, IPL_SERIAL); 213 214 cs = zsc->zsc_cs[1]; 215 cs->cs_private = ms; 216 cs->cs_ops = &zsops_ms; 217 ms->ms_cs = cs; 218 219 /* Initialize the speed, etc. */ 220 /* May need reset... */ 221 reset = ZSWR9_B_RESET; 222 zs_write_reg(cs, 9, reset); 223 /* We don't care about status or tx interrupts. */ 224 cs->cs_preg[1] = ZSWR1_RIE; 225 cs->cs_preg[4] = ZSWR4_CLK_X16 | ZSWR4_TWOSB; 226 (void)zs_set_speed(cs, MS_BPS); 227 zs_loadchannelregs(cs); 228 229 /* Initialize translator. */ 230 ms->ms_ready = 0; 231 232 aprint_normal("\n"); 233 } 234 235 /**************************************************************** 236 * Entry points for /dev/mouse 237 * (open,close,read,write,...) 238 ****************************************************************/ 239 240 int 241 msopen(dev_t dev, int flags, int mode, struct lwp *l) 242 { 243 struct ms_softc *ms; 244 245 ms = device_lookup_private(&ms_cd, minor(dev)); 246 if (ms == NULL) 247 return ENXIO; 248 249 /* This is an exclusive open device. */ 250 if (ms->ms_events.ev_io) 251 return EBUSY; 252 ms->ms_events.ev_io = l->l_proc; 253 ev_init(&ms->ms_events, device_xname(ms->ms_dev), &ms->ms_lock); 254 255 ms->ms_ready = 1; /* start accepting events */ 256 ms->ms_rts = 1; 257 ms->ms_byteno = -1; 258 ms->ms_nodata = 0; 259 260 /* start sequencer */ 261 ms_modem(ms); 262 263 return 0; 264 } 265 266 int 267 msclose(dev_t dev, int flags, int mode, struct lwp *l) 268 { 269 struct ms_softc *ms; 270 271 ms = device_lookup_private(&ms_cd, minor(dev)); 272 ms->ms_ready = 0; /* stop accepting events */ 273 callout_stop(&ms->ms_modem_ch); 274 ev_fini(&ms->ms_events); 275 276 ms->ms_events.ev_io = NULL; 277 return 0; 278 } 279 280 int 281 msread(dev_t dev, struct uio *uio, int flags) 282 { 283 struct ms_softc *ms; 284 285 ms = device_lookup_private(&ms_cd, minor(dev)); 286 return ev_read(&ms->ms_events, uio, flags); 287 } 288 289 int 290 msioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 291 { 292 struct ms_softc *ms; 293 294 ms = device_lookup_private(&ms_cd, minor(dev)); 295 296 switch (cmd) { 297 298 case FIONBIO: /* we will remove this someday (soon???) */ 299 return 0; 300 301 case FIOASYNC: 302 ms->ms_events.ev_async = *(int *)data != 0; 303 return 0; 304 305 case FIOSETOWN: 306 if (-*(int *)data != ms->ms_events.ev_io->p_pgid 307 && *(int *)data != ms->ms_events.ev_io->p_pid) 308 return EPERM; 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_t dev, int events, struct lwp *l) 331 { 332 struct ms_softc *ms; 333 334 ms = device_lookup_private(&ms_cd, minor(dev)); 335 return ev_poll(&ms->ms_events, events, l); 336 } 337 338 int 339 mskqfilter(dev_t dev, struct knote *kn) 340 { 341 struct ms_softc *ms; 342 343 ms = device_lookup_private(&ms_cd, minor(dev)); 344 return ev_kqfilter(&ms->ms_events, kn); 345 } 346 347 /**************************************************************** 348 * Middle layer (translator) 349 ****************************************************************/ 350 351 /* 352 * Called by our ms_softint() routine on input. 353 */ 354 static void 355 ms_input(struct ms_softc *ms, int c) 356 { 357 struct firm_event *fe; 358 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 firm_gettime(fe); 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 firm_gettime(fe); 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 firm_gettime(fe); 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 476 ms_rxint(struct zs_chanstate *cs) 477 { 478 struct ms_softc *ms; 479 int put, put_next; 480 u_char c, rr1; 481 482 ms = cs->cs_private; 483 put = ms->ms_rbput; 484 485 /* 486 * First read the status, because reading the received char 487 * destroys the status of this char. 488 */ 489 rr1 = zs_read_reg(cs, 1); 490 c = zs_read_data(cs); 491 492 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) { 493 /* Clear the receive error. */ 494 zs_write_csr(cs, ZSWR0_RESET_ERRORS); 495 } 496 497 ms->ms_rbuf[put] = (c << 8) | rr1; 498 put_next = (put + 1) & MS_RX_RING_MASK; 499 500 /* Would overrun if increment makes (put==get). */ 501 if (put_next == ms->ms_rbget) { 502 ms->ms_intr_flags |= INTR_RX_OVERRUN; 503 } else { 504 /* OK, really increment. */ 505 put = put_next; 506 } 507 508 /* Done reading. */ 509 ms->ms_rbput = put; 510 511 /* Ask for softint() call. */ 512 cs->cs_softreq = 1; 513 } 514 515 516 static void 517 ms_txint(struct zs_chanstate *cs) 518 { 519 struct ms_softc *ms; 520 521 ms = cs->cs_private; 522 zs_write_csr(cs, ZSWR0_RESET_TXINT); 523 ms->ms_intr_flags |= INTR_TX_EMPTY; 524 /* Ask for softint() call. */ 525 cs->cs_softreq = 1; 526 } 527 528 529 static void 530 ms_stint(struct zs_chanstate *cs, int force) 531 { 532 struct ms_softc *ms; 533 int rr0; 534 535 ms = cs->cs_private; 536 537 rr0 = zs_read_csr(cs); 538 zs_write_csr(cs, ZSWR0_RESET_STATUS); 539 540 /* 541 * We have to accumulate status line changes here. 542 * Otherwise, if we get multiple status interrupts 543 * before the softint runs, we could fail to notice 544 * some status line changes in the softint routine. 545 * Fix from Bill Studenmund, October 1996. 546 */ 547 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0); 548 cs->cs_rr0 = rr0; 549 ms->ms_intr_flags |= INTR_ST_CHECK; 550 551 /* Ask for softint() call. */ 552 cs->cs_softreq = 1; 553 } 554 555 556 static void 557 ms_softint(struct zs_chanstate *cs) 558 { 559 struct ms_softc *ms; 560 int get, c; 561 int intr_flags; 562 u_short ring_data; 563 564 ms = cs->cs_private; 565 566 mutex_enter(&ms->ms_lock); 567 intr_flags = ms->ms_intr_flags; 568 ms->ms_intr_flags = 0; 569 570 /* 571 * Copy data from the receive ring to the event layer. 572 */ 573 get = ms->ms_rbget; 574 while (get != ms->ms_rbput) { 575 ring_data = ms->ms_rbuf[get]; 576 mutex_exit(&ms->ms_lock); 577 get = (get + 1) & MS_RX_RING_MASK; 578 579 /* low byte of ring_data is rr1 */ 580 c = (ring_data >> 8) & 0xff; 581 582 if (ring_data & ZSRR1_DO) 583 intr_flags |= INTR_RX_OVERRUN; 584 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) { 585 log(LOG_ERR, "%s: input error (0x%x)\n", 586 device_xname(ms->ms_dev), ring_data); 587 c = -1; /* signal input error */ 588 } 589 590 /* Pass this up to the "middle" layer. */ 591 ms_input(ms, c); 592 mutex_enter(&ms->ms_lock); 593 } 594 mutex_exit(&ms->ms_lock); 595 596 if (intr_flags & INTR_RX_OVERRUN) { 597 log(LOG_ERR, "%s: input overrun\n", 598 device_xname(ms->ms_dev)); 599 } 600 ms->ms_rbget = get; 601 602 if (intr_flags & INTR_TX_EMPTY) { 603 /* 604 * Transmit done. (Not expected.) 605 */ 606 log(LOG_ERR, "%s: transmit interrupt?\n", 607 device_xname(ms->ms_dev)); 608 } 609 610 if (intr_flags & INTR_ST_CHECK) { 611 /* 612 * Status line change. (Not expected.) 613 */ 614 log(LOG_ERR, "%s: status interrupt?\n", 615 device_xname(ms->ms_dev)); 616 mutex_enter(&ms->ms_lock); 617 cs->cs_rr0_delta = 0; 618 mutex_exit(&ms->ms_lock); 619 } 620 } 621 622 623 static void 624 ms_trigger(struct zs_chanstate *cs, int onoff) 625 { 626 /* for front connected one */ 627 if (onoff) 628 cs->cs_preg[5] |= ZSWR5_RTS; 629 else 630 cs->cs_preg[5] &= ~ZSWR5_RTS; 631 cs->cs_creg[5] = cs->cs_preg[5]; 632 zs_write_reg(cs, 5, cs->cs_preg[5]); 633 634 /* for keyborad connected one */ 635 mfp_send_usart(onoff | 0x40); 636 } 637 638 /* 639 * mouse timer interrupt. 640 * called after system tick interrupt is done. 641 */ 642 void 643 ms_modem(void *arg) 644 { 645 struct ms_softc *ms = arg; 646 647 if (!ms->ms_ready) 648 return; 649 650 mutex_enter(&ms->ms_lock); 651 652 if (ms->ms_nodata++ > 250) { /* XXX */ 653 log(LOG_ERR, "%s: no data for 5 secs. resetting.\n", 654 device_xname(ms->ms_dev)); 655 ms->ms_byteno = -1; 656 ms->ms_nodata = 0; 657 ms->ms_rts = 0; 658 } 659 660 if (ms->ms_rts) { 661 if (ms->ms_byteno == -1) { 662 /* start next sequence */ 663 ms->ms_rts = 0; 664 ms_trigger(ms->ms_cs, ms->ms_rts); 665 ms->ms_byteno = 0; 666 } 667 } else { 668 ms->ms_rts = 1; 669 ms_trigger(ms->ms_cs, ms->ms_rts); 670 } 671 672 mutex_exit(&ms->ms_lock); 673 callout_reset(&ms->ms_modem_ch, 2, ms_modem, ms); 674 } 675