1 /* $NetBSD: wsmouse.c,v 1.22 2001/11/22 00:57:14 augustss Exp $ */ 2 3 /* 4 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Christopher G. Demetriou 17 * for the NetBSD Project. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1992, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * This software was developed by the Computer Systems Engineering group 38 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 39 * contributed to Berkeley. 40 * 41 * All advertising materials mentioning features or use of this software 42 * must display the following acknowledgement: 43 * This product includes software developed by the University of 44 * California, Lawrence Berkeley Laboratory. 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 3. All advertising materials mentioning features or use of this software 55 * must display the following acknowledgement: 56 * This product includes software developed by the University of 57 * California, Berkeley and its contributors. 58 * 4. Neither the name of the University nor the names of its contributors 59 * may be used to endorse or promote products derived from this software 60 * without specific prior written permission. 61 * 62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 72 * SUCH DAMAGE. 73 * 74 * @(#)ms.c 8.1 (Berkeley) 6/11/93 75 */ 76 77 /* 78 * Mouse driver. 79 */ 80 81 #include <sys/cdefs.h> 82 __KERNEL_RCSID(0, "$NetBSD: wsmouse.c,v 1.22 2001/11/22 00:57:14 augustss Exp $"); 83 84 #include "wsmouse.h" 85 #include "wsdisplay.h" 86 #include "wsmux.h" 87 88 #include <sys/param.h> 89 #include <sys/conf.h> 90 #include <sys/ioctl.h> 91 #include <sys/fcntl.h> 92 #include <sys/kernel.h> 93 #include <sys/proc.h> 94 #include <sys/syslog.h> 95 #include <sys/systm.h> 96 #include <sys/tty.h> 97 #include <sys/signalvar.h> 98 #include <sys/device.h> 99 #include <sys/vnode.h> 100 101 #include <dev/wscons/wsconsio.h> 102 #include <dev/wscons/wsmousevar.h> 103 #include <dev/wscons/wseventvar.h> 104 #include <dev/wscons/wsmuxvar.h> 105 106 #if defined(WSMUX_DEBUG) && NWSMUX > 0 107 #define DPRINTF(x) if (wsmuxdebug) printf x 108 #define DPRINTFN(n,x) if (wsmuxdebug > (n)) printf x 109 extern int wsmuxdebug; 110 #else 111 #define DPRINTF(x) 112 #define DPRINTFN(n,x) 113 #endif 114 115 #define INVALID_X INT_MAX 116 #define INVALID_Y INT_MAX 117 #define INVALID_Z INT_MAX 118 119 struct wsmouse_softc { 120 struct wsevsrc sc_base; 121 122 const struct wsmouse_accessops *sc_accessops; 123 void *sc_accesscookie; 124 125 u_int sc_mb; /* mouse button state */ 126 u_int sc_ub; /* user button state */ 127 int sc_dx; /* delta-x */ 128 int sc_dy; /* delta-y */ 129 int sc_dz; /* delta-z */ 130 int sc_x; /* absolute-x */ 131 int sc_y; /* absolute-y */ 132 int sc_z; /* absolute-z */ 133 134 int sc_refcnt; 135 u_char sc_dying; /* device is being detached */ 136 }; 137 138 static int wsmouse_match(struct device *, struct cfdata *, void *); 139 static void wsmouse_attach(struct device *, struct device *, void *); 140 static int wsmouse_detach(struct device *, int); 141 static int wsmouse_activate(struct device *, enum devact); 142 143 static int wsmouse_do_ioctl(struct wsmouse_softc *, u_long, caddr_t, 144 int, struct proc *); 145 146 #if NWSMUX > 0 147 static int wsmouse_mux_open(struct wsevsrc *, struct wseventvar *); 148 static int wsmouse_mux_close(struct wsevsrc *); 149 #endif 150 151 static int wsmousedoioctl(struct device *, u_long, caddr_t, int, struct proc *); 152 153 static int wsmousedoopen(struct wsmouse_softc *, struct wseventvar *); 154 155 struct cfattach wsmouse_ca = { 156 sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach, 157 wsmouse_detach, wsmouse_activate 158 }; 159 160 #if NWSMOUSE > 0 161 extern struct cfdriver wsmouse_cd; 162 #endif /* NWSMOUSE > 0 */ 163 164 cdev_decl(wsmouse); 165 166 #if NWSMUX > 0 167 struct wssrcops wsmouse_srcops = { 168 WSMUX_MOUSE, 169 wsmouse_mux_open, wsmouse_mux_close, wsmousedoioctl, NULL, NULL 170 }; 171 #endif 172 173 /* 174 * Print function (for parent devices). 175 */ 176 int 177 wsmousedevprint(void *aux, const char *pnp) 178 { 179 180 if (pnp) 181 printf("wsmouse at %s", pnp); 182 return (UNCONF); 183 } 184 185 int 186 wsmouse_match(struct device *parent, struct cfdata *match, void *aux) 187 { 188 return (1); 189 } 190 191 void 192 wsmouse_attach(struct device *parent, struct device *self, void *aux) 193 { 194 struct wsmouse_softc *sc = (struct wsmouse_softc *)self; 195 struct wsmousedev_attach_args *ap = aux; 196 #if NWSMUX > 0 197 int mux, error; 198 #endif 199 200 sc->sc_accessops = ap->accessops; 201 sc->sc_accesscookie = ap->accesscookie; 202 203 #if NWSMUX > 0 204 sc->sc_base.me_ops = &wsmouse_srcops; 205 mux = sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux; 206 if (mux >= 0) { 207 error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base); 208 if (error) 209 printf(" attach error=%d", error); 210 else 211 printf(" mux %d", mux); 212 } 213 #else 214 if (sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux >= 0) 215 printf(" (mux ignored)"); 216 #endif 217 218 printf("\n"); 219 } 220 221 int 222 wsmouse_activate(struct device *self, enum devact act) 223 { 224 struct wsmouse_softc *sc = (struct wsmouse_softc *)self; 225 226 if (act == DVACT_DEACTIVATE) 227 sc->sc_dying = 1; 228 return (0); 229 } 230 231 /* 232 * Detach a mouse. To keep track of users of the softc we keep 233 * a reference count that's incremented while inside, e.g., read. 234 * If the mouse is active and the reference count is > 0 (0 is the 235 * normal state) we post an event and then wait for the process 236 * that had the reference to wake us up again. Then we blow away the 237 * vnode and return (which will deallocate the softc). 238 */ 239 int 240 wsmouse_detach(struct device *self, int flags) 241 { 242 struct wsmouse_softc *sc = (struct wsmouse_softc *)self; 243 struct wseventvar *evar; 244 int maj, mn; 245 int s; 246 247 #if NWSMUX > 0 248 /* Tell parent mux we're leaving. */ 249 if (sc->sc_base.me_parent != NULL) { 250 DPRINTF(("wsmouse_detach:\n")); 251 wsmux_detach_sc(&sc->sc_base); 252 } 253 #endif 254 255 /* If we're open ... */ 256 evar = sc->sc_base.me_evp; 257 if (evar != NULL && evar->io != NULL) { 258 s = spltty(); 259 if (--sc->sc_refcnt >= 0) { 260 /* Wake everyone by generating a dummy event. */ 261 if (++evar->put >= WSEVENT_QSIZE) 262 evar->put = 0; 263 WSEVENT_WAKEUP(evar); 264 /* Wait for processes to go away. */ 265 if (tsleep(sc, PZERO, "wsmdet", hz * 60)) 266 printf("wsmouse_detach: %s didn't detach\n", 267 sc->sc_base.me_dv.dv_xname); 268 } 269 splx(s); 270 } 271 272 /* locate the major number */ 273 for (maj = 0; maj < nchrdev; maj++) 274 if (cdevsw[maj].d_open == wsmouseopen) 275 break; 276 277 /* Nuke the vnodes for any open instances (calls close). */ 278 mn = self->dv_unit; 279 vdevgone(maj, mn, mn, VCHR); 280 281 return (0); 282 } 283 284 void 285 wsmouse_input(struct device *wsmousedev, u_int btns /* 0 is up */, 286 int x, int y, int z, u_int flags) 287 { 288 struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev; 289 struct wscons_event *ev; 290 struct wseventvar *evar; 291 int mb, ub, d, get, put, any; 292 293 /* 294 * Discard input if not open. 295 */ 296 evar = sc->sc_base.me_evp; 297 if (evar == NULL) 298 return; 299 300 #ifdef DIAGNOSTIC 301 if (evar->q == NULL) { 302 printf("wsmouse_input: evar->q=NULL\n"); 303 return; 304 } 305 #endif 306 307 #if NWSMUX > 0 308 DPRINTFN(5,("wsmouse_input: %s mux=%p, evar=%p\n", 309 sc->sc_base.me_dv.dv_xname, sc->sc_base.me_parent, evar)); 310 #endif 311 312 sc->sc_mb = btns; 313 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_X)) 314 sc->sc_dx += x; 315 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Y)) 316 sc->sc_dy += y; 317 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Z)) 318 sc->sc_dz += z; 319 320 /* 321 * We have at least one event (mouse button, delta-X, or 322 * delta-Y; possibly all three, and possibly three separate 323 * button events). Deliver these events until we are out 324 * of changes or out of room. As events get delivered, 325 * mark them `unchanged'. 326 */ 327 ub = sc->sc_ub; 328 any = 0; 329 get = evar->get; 330 put = evar->put; 331 ev = &evar->q[put]; 332 333 /* NEXT prepares to put the next event, backing off if necessary */ 334 #define NEXT \ 335 if ((++put) % WSEVENT_QSIZE == get) { \ 336 put--; \ 337 goto out; \ 338 } 339 /* ADVANCE completes the `put' of the event */ 340 #define ADVANCE \ 341 ev++; \ 342 if (put >= WSEVENT_QSIZE) { \ 343 put = 0; \ 344 ev = &evar->q[0]; \ 345 } \ 346 any = 1 347 /* TIMESTAMP sets `time' field of the event to the current time */ 348 #define TIMESTAMP \ 349 do { \ 350 int s; \ 351 s = splhigh(); \ 352 TIMEVAL_TO_TIMESPEC(&time, &ev->time); \ 353 splx(s); \ 354 } while (0) 355 356 if (flags & WSMOUSE_INPUT_ABSOLUTE_X) { 357 if (sc->sc_x != x) { 358 NEXT; 359 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_X; 360 ev->value = x; 361 TIMESTAMP; 362 ADVANCE; 363 sc->sc_x = x; 364 } 365 } else { 366 if (sc->sc_dx) { 367 NEXT; 368 ev->type = WSCONS_EVENT_MOUSE_DELTA_X; 369 ev->value = sc->sc_dx; 370 TIMESTAMP; 371 ADVANCE; 372 sc->sc_dx = 0; 373 } 374 } 375 if (flags & WSMOUSE_INPUT_ABSOLUTE_Y) { 376 if (sc->sc_y != y) { 377 NEXT; 378 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Y; 379 ev->value = y; 380 TIMESTAMP; 381 ADVANCE; 382 sc->sc_y = y; 383 } 384 } else { 385 if (sc->sc_dy) { 386 NEXT; 387 ev->type = WSCONS_EVENT_MOUSE_DELTA_Y; 388 ev->value = sc->sc_dy; 389 TIMESTAMP; 390 ADVANCE; 391 sc->sc_dy = 0; 392 } 393 } 394 if (flags & WSMOUSE_INPUT_ABSOLUTE_Z) { 395 if (sc->sc_z != z) { 396 NEXT; 397 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Z; 398 ev->value = z; 399 TIMESTAMP; 400 ADVANCE; 401 sc->sc_z = z; 402 } 403 } else { 404 if (sc->sc_dz) { 405 NEXT; 406 ev->type = WSCONS_EVENT_MOUSE_DELTA_Z; 407 ev->value = sc->sc_dz; 408 TIMESTAMP; 409 ADVANCE; 410 sc->sc_dz = 0; 411 } 412 } 413 414 mb = sc->sc_mb; 415 while ((d = mb ^ ub) != 0) { 416 /* 417 * Mouse button change. Find the first change and drop 418 * it into the event queue. 419 */ 420 NEXT; 421 ev->value = ffs(d) - 1; 422 423 KASSERT(ev->value >= 0); 424 425 d = 1 << ev->value; 426 ev->type = 427 (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP; 428 TIMESTAMP; 429 ADVANCE; 430 ub ^= d; 431 } 432 out: 433 if (any) { 434 sc->sc_ub = ub; 435 evar->put = put; 436 WSEVENT_WAKEUP(evar); 437 #if NWSMUX > 0 438 DPRINTFN(5,("wsmouse_input: %s wakeup evar=%p\n", 439 sc->sc_base.me_dv.dv_xname, evar)); 440 #endif 441 } 442 } 443 444 int 445 wsmouseopen(dev_t dev, int flags, int mode, struct proc *p) 446 { 447 struct wsmouse_softc *sc; 448 struct wseventvar *evar; 449 int error, unit; 450 451 unit = minor(dev); 452 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */ 453 (sc = wsmouse_cd.cd_devs[unit]) == NULL) 454 return (ENXIO); 455 456 #if NWSMUX > 0 457 DPRINTF(("wsmouseopen: %s mux=%p p=%p\n", sc->sc_base.me_dv.dv_xname, 458 sc->sc_base.me_parent, p)); 459 #endif 460 461 if (sc->sc_dying) 462 return (EIO); 463 464 if ((flags & (FREAD | FWRITE)) == FWRITE) 465 return (0); /* always allow open for write 466 so ioctl() is possible. */ 467 468 #if NWSMUX > 0 469 if (sc->sc_base.me_parent != NULL) { 470 /* Grab the mouse out of the greedy hands of the mux. */ 471 DPRINTF(("wsmouseopen: detach\n")); 472 wsmux_detach_sc(&sc->sc_base); 473 } 474 #endif 475 476 if (sc->sc_base.me_evp != NULL) 477 return (EBUSY); 478 479 evar = &sc->sc_base.me_evar; 480 wsevent_init(evar); 481 sc->sc_base.me_evp = evar; 482 evar->io = p; 483 484 error = wsmousedoopen(sc, evar); 485 if (error) { 486 DPRINTF(("wsmouseopen: %s open failed\n", 487 sc->sc_base.me_dv.dv_xname)); 488 sc->sc_base.me_evp = NULL; 489 wsevent_fini(evar); 490 } 491 return (error); 492 } 493 494 int 495 wsmouseclose(dev_t dev, int flags, int mode, struct proc *p) 496 { 497 struct wsmouse_softc *sc = 498 (struct wsmouse_softc *)wsmouse_cd.cd_devs[minor(dev)]; 499 struct wseventvar *evar = sc->sc_base.me_evp; 500 501 if (evar == NULL) 502 /* not open for read */ 503 return (0); 504 sc->sc_base.me_evp = NULL; 505 (*sc->sc_accessops->disable)(sc->sc_accesscookie); 506 wsevent_fini(evar); 507 508 return (0); 509 } 510 511 int 512 wsmousedoopen(struct wsmouse_softc *sc, struct wseventvar *evp) 513 { 514 sc->sc_base.me_evp = evp; 515 sc->sc_x = INVALID_X; 516 sc->sc_y = INVALID_Y; 517 sc->sc_z = INVALID_Z; 518 519 /* enable the device, and punt if that's not possible */ 520 return (*sc->sc_accessops->enable)(sc->sc_accesscookie); 521 } 522 523 int 524 wsmouseread(dev_t dev, struct uio *uio, int flags) 525 { 526 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)]; 527 int error; 528 529 if (sc->sc_dying) 530 return (EIO); 531 532 #ifdef DIAGNOSTIC 533 if (sc->sc_base.me_evp == NULL) { 534 printf("wsmouseread: evp == NULL\n"); 535 return (EINVAL); 536 } 537 #endif 538 539 sc->sc_refcnt++; 540 error = wsevent_read(sc->sc_base.me_evp, uio, flags); 541 if (--sc->sc_refcnt < 0) { 542 wakeup(sc); 543 error = EIO; 544 } 545 return (error); 546 } 547 548 int 549 wsmouseioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 550 { 551 return (wsmousedoioctl(wsmouse_cd.cd_devs[minor(dev)], 552 cmd, data, flag, p)); 553 } 554 555 /* A wrapper around the ioctl() workhorse to make reference counting easy. */ 556 int 557 wsmousedoioctl(struct device *dv, u_long cmd, caddr_t data, int flag, 558 struct proc *p) 559 { 560 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv; 561 int error; 562 563 sc->sc_refcnt++; 564 error = wsmouse_do_ioctl(sc, cmd, data, flag, p); 565 if (--sc->sc_refcnt < 0) 566 wakeup(sc); 567 return (error); 568 } 569 570 int 571 wsmouse_do_ioctl(struct wsmouse_softc *sc, u_long cmd, caddr_t data, 572 int flag, struct proc *p) 573 { 574 int error; 575 576 if (sc->sc_dying) 577 return (EIO); 578 579 /* 580 * Try the generic ioctls that the wsmouse interface supports. 581 */ 582 switch (cmd) { 583 case FIONBIO: /* we will remove this someday (soon???) */ 584 return (0); 585 586 case FIOASYNC: 587 if (sc->sc_base.me_evp == NULL) 588 return (EINVAL); 589 sc->sc_base.me_evp->async = *(int *)data != 0; 590 return (0); 591 592 case TIOCSPGRP: 593 if (sc->sc_base.me_evp == NULL) 594 return (EINVAL); 595 if (*(int *)data != sc->sc_base.me_evp->io->p_pgid) 596 return (EPERM); 597 return (0); 598 } 599 600 /* 601 * Try the mouse driver for WSMOUSEIO ioctls. It returns -1 602 * if it didn't recognize the request. 603 */ 604 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, 605 data, flag, p); 606 return (error != -1 ? error : ENOTTY); 607 } 608 609 int 610 wsmousepoll(dev_t dev, int events, struct proc *p) 611 { 612 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)]; 613 614 if (sc->sc_base.me_evp == NULL) 615 return (EINVAL); 616 return (wsevent_poll(sc->sc_base.me_evp, events, p)); 617 } 618 619 #if NWSMUX > 0 620 int 621 wsmouse_mux_open(struct wsevsrc *me, struct wseventvar *evp) 622 { 623 struct wsmouse_softc *sc = (struct wsmouse_softc *)me; 624 625 if (sc->sc_base.me_evp != NULL) 626 return (EBUSY); 627 628 return wsmousedoopen(sc, evp); 629 } 630 631 int 632 wsmouse_mux_close(struct wsevsrc *me) 633 { 634 struct wsmouse_softc *sc = (struct wsmouse_softc *)me; 635 636 sc->sc_base.me_evp = NULL; 637 (*sc->sc_accessops->disable)(sc->sc_accesscookie); 638 639 return (0); 640 } 641 642 int 643 wsmouse_add_mux(int unit, struct wsmux_softc *muxsc) 644 { 645 struct wsmouse_softc *sc; 646 647 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs || 648 (sc = wsmouse_cd.cd_devs[unit]) == NULL) 649 return (ENXIO); 650 651 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL) 652 return (EBUSY); 653 654 return (wsmux_attach_sc(muxsc, &sc->sc_base)); 655 } 656 #endif /* NWSMUX > 0 */ 657