1 /* $NetBSD: wsmux.c,v 1.57 2014/03/16 05:20:29 dholland Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2005 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Author: Lennart Augustsson <lennart@augustsson.net> 8 * Carlstedt Research & Technology 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * wscons mux device. 34 * 35 * The mux device is a collection of real mice and keyboards and acts as 36 * a merge point for all the events from the different real devices. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: wsmux.c,v 1.57 2014/03/16 05:20:29 dholland Exp $"); 41 42 #include "opt_compat_netbsd.h" 43 #include "opt_modular.h" 44 45 #include "wsdisplay.h" 46 #include "wsmux.h" 47 #include "wskbd.h" 48 #include "wsmouse.h" 49 50 #include <sys/param.h> 51 #include <sys/conf.h> 52 #include <sys/ioctl.h> 53 #include <sys/poll.h> 54 #include <sys/fcntl.h> 55 #include <sys/kernel.h> 56 #include <sys/malloc.h> 57 #include <sys/proc.h> 58 #include <sys/queue.h> 59 #include <sys/syslog.h> 60 #include <sys/systm.h> 61 #include <sys/tty.h> 62 #include <sys/signalvar.h> 63 #include <sys/device.h> 64 65 #include "opt_wsdisplay_compat.h" 66 67 #include <dev/wscons/wsconsio.h> 68 #include <dev/wscons/wsksymdef.h> 69 #include <dev/wscons/wseventvar.h> 70 #include <dev/wscons/wscons_callbacks.h> 71 #include <dev/wscons/wsmuxvar.h> 72 73 #ifdef WSMUX_DEBUG 74 #define DPRINTF(x) if (wsmuxdebug) printf x 75 #define DPRINTFN(n,x) if (wsmuxdebug > (n)) printf x 76 int wsmuxdebug = 0; 77 #else 78 #define DPRINTF(x) 79 #define DPRINTFN(n,x) 80 #endif 81 82 /* 83 * The wsmux pseudo device is used to multiplex events from several wsmouse, 84 * wskbd, and/or wsmux devices together. 85 * The devices connected together form a tree with muxes in the interior 86 * and real devices (mouse and kbd) at the leaves. The special case of 87 * a tree with one node (mux or other) is supported as well. 88 * Only the device at the root of the tree can be opened (if a non-root 89 * device is opened the subtree rooted at that point is severed from the 90 * containing tree). When the root is opened it allocates a wseventvar 91 * struct which all the nodes in the tree will send their events too. 92 * An ioctl() performed on the root is propagated to all the nodes. 93 * There are also ioctl() operations to add and remove nodes from a tree. 94 */ 95 96 static int wsmux_mux_open(struct wsevsrc *, struct wseventvar *); 97 static int wsmux_mux_close(struct wsevsrc *); 98 99 static void wsmux_do_open(struct wsmux_softc *, struct wseventvar *); 100 101 static void wsmux_do_close(struct wsmux_softc *); 102 #if NWSDISPLAY > 0 103 static int wsmux_evsrc_set_display(device_t, struct wsevsrc *); 104 #else 105 #define wsmux_evsrc_set_display NULL 106 #endif 107 108 static int wsmux_do_displayioctl(device_t dev, u_long cmd, 109 void *data, int flag, struct lwp *l); 110 static int wsmux_do_ioctl(device_t, u_long, void *,int,struct lwp *); 111 112 static int wsmux_add_mux(int, struct wsmux_softc *); 113 114 void wsmuxattach(int); 115 116 #define WSMUXDEV(n) ((n) & 0x7f) 117 #define WSMUXCTL(n) ((n) & 0x80) 118 119 dev_type_open(wsmuxopen); 120 dev_type_close(wsmuxclose); 121 dev_type_read(wsmuxread); 122 dev_type_ioctl(wsmuxioctl); 123 dev_type_poll(wsmuxpoll); 124 dev_type_kqfilter(wsmuxkqfilter); 125 126 const struct cdevsw wsmux_cdevsw = { 127 .d_open = wsmuxopen, 128 .d_close = wsmuxclose, 129 .d_read = wsmuxread, 130 .d_write = nowrite, 131 .d_ioctl = wsmuxioctl, 132 .d_stop = nostop, 133 .d_tty = notty, 134 .d_poll = wsmuxpoll, 135 .d_mmap = nommap, 136 .d_kqfilter = wsmuxkqfilter, 137 .d_flag = D_OTHER 138 }; 139 140 struct wssrcops wsmux_srcops = { 141 WSMUX_MUX, 142 wsmux_mux_open, wsmux_mux_close, wsmux_do_ioctl, wsmux_do_displayioctl, 143 wsmux_evsrc_set_display 144 }; 145 146 /* From upper level */ 147 void 148 wsmuxattach(int n) 149 { 150 } 151 152 /* Keep track of all muxes that have been allocated */ 153 static struct wsmux_softc **wsmuxdevs = NULL; 154 static int nwsmux = 0; 155 156 /* Return mux n, create if necessary */ 157 struct wsmux_softc * 158 wsmux_getmux(int n) 159 { 160 struct wsmux_softc *sc; 161 162 n = WSMUXDEV(n); /* limit range */ 163 164 /* Make sure there is room for mux n in the table */ 165 if (n >= nwsmux) { 166 void *new; 167 168 new = realloc(wsmuxdevs, (n + 1) * sizeof(*wsmuxdevs), 169 M_DEVBUF, M_ZERO | M_NOWAIT); 170 if (new == NULL) { 171 printf("wsmux_getmux: no memory for mux %d\n", n); 172 return NULL; 173 } 174 wsmuxdevs = new; 175 nwsmux = n + 1; 176 } 177 178 sc = wsmuxdevs[n]; 179 if (sc == NULL) { 180 sc = wsmux_create("wsmux", n); 181 if (sc == NULL) 182 printf("wsmux: attach out of memory\n"); 183 wsmuxdevs[n] = sc; 184 } 185 return (sc); 186 } 187 188 /* 189 * open() of the pseudo device from device table. 190 */ 191 int 192 wsmuxopen(dev_t dev, int flags, int mode, struct lwp *l) 193 { 194 struct wsmux_softc *sc; 195 struct wseventvar *evar; 196 int minr, unit; 197 198 minr = minor(dev); 199 unit = WSMUXDEV(minr); 200 sc = wsmux_getmux(unit); 201 if (sc == NULL) 202 return (ENXIO); 203 204 DPRINTF(("wsmuxopen: %s: sc=%p l=%p\n", 205 device_xname(sc->sc_base.me_dv), sc, l)); 206 207 if (WSMUXCTL(minr)) { 208 /* This is the control device which does not allow reads. */ 209 if (flags & FREAD) 210 return (EINVAL); 211 return (0); 212 } 213 if ((flags & (FREAD | FWRITE)) == FWRITE) 214 /* Allow write only open */ 215 return (0); 216 217 if (sc->sc_base.me_parent != NULL) { 218 /* Grab the mux out of the greedy hands of the parent mux. */ 219 DPRINTF(("wsmuxopen: detach\n")); 220 wsmux_detach_sc(&sc->sc_base); 221 } 222 223 if (sc->sc_base.me_evp != NULL) 224 /* Already open. */ 225 return (EBUSY); 226 227 evar = &sc->sc_base.me_evar; 228 wsevent_init(evar, l->l_proc); 229 #ifdef WSDISPLAY_COMPAT_RAWKBD 230 sc->sc_rawkbd = 0; 231 #endif 232 233 wsmux_do_open(sc, evar); 234 235 return (0); 236 } 237 238 /* 239 * Open of a mux via the parent mux. 240 */ 241 int 242 wsmux_mux_open(struct wsevsrc *me, struct wseventvar *evar) 243 { 244 struct wsmux_softc *sc = (struct wsmux_softc *)me; 245 246 #ifdef DIAGNOSTIC 247 if (sc->sc_base.me_evp != NULL) { 248 printf("wsmux_mux_open: busy\n"); 249 return (EBUSY); 250 } 251 if (sc->sc_base.me_parent == NULL) { 252 printf("wsmux_mux_open: no parent\n"); 253 return (EINVAL); 254 } 255 #endif 256 257 wsmux_do_open(sc, evar); 258 259 return (0); 260 } 261 262 /* Common part of opening a mux. */ 263 void 264 wsmux_do_open(struct wsmux_softc *sc, struct wseventvar *evar) 265 { 266 struct wsevsrc *me; 267 268 sc->sc_base.me_evp = evar; /* remember event variable, mark as open */ 269 270 /* Open all children. */ 271 TAILQ_FOREACH(me, &sc->sc_cld, me_next) { 272 DPRINTF(("wsmuxopen: %s: m=%p dev=%s\n", 273 device_xname(sc->sc_base.me_dv), me, 274 device_xname(me->me_dv))); 275 #ifdef DIAGNOSTIC 276 if (me->me_evp != NULL) { 277 printf("wsmuxopen: dev already in use\n"); 278 continue; 279 } 280 if (me->me_parent != sc) { 281 printf("wsmux_do_open: bad child=%p\n", me); 282 continue; 283 } 284 { 285 int error = wsevsrc_open(me, evar); 286 if (error) { 287 DPRINTF(("wsmuxopen: open failed %d\n", error)); 288 } 289 } 290 #else 291 /* ignore errors, failing children will not be marked open */ 292 (void)wsevsrc_open(me, evar); 293 #endif 294 } 295 } 296 297 /* 298 * close() of the pseudo device from device table. 299 */ 300 int 301 wsmuxclose(dev_t dev, int flags, int mode, 302 struct lwp *l) 303 { 304 int minr = minor(dev); 305 struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)]; 306 struct wseventvar *evar = sc->sc_base.me_evp; 307 308 if (WSMUXCTL(minr)) 309 /* control device */ 310 return (0); 311 if (evar == NULL) 312 /* Not open for read */ 313 return (0); 314 315 wsmux_do_close(sc); 316 sc->sc_base.me_evp = NULL; 317 wsevent_fini(evar); 318 return (0); 319 } 320 321 /* 322 * Close of a mux via the parent mux. 323 */ 324 int 325 wsmux_mux_close(struct wsevsrc *me) 326 { 327 me->me_evp = NULL; 328 wsmux_do_close((struct wsmux_softc *)me); 329 return (0); 330 } 331 332 /* Common part of closing a mux. */ 333 void 334 wsmux_do_close(struct wsmux_softc *sc) 335 { 336 struct wsevsrc *me; 337 338 DPRINTF(("wsmuxclose: %s: sc=%p\n", 339 device_xname(sc->sc_base.me_dv), sc)); 340 341 /* Close all the children. */ 342 TAILQ_FOREACH(me, &sc->sc_cld, me_next) { 343 DPRINTF(("wsmuxclose %s: m=%p dev=%s\n", 344 device_xname(sc->sc_base.me_dv), me, 345 device_xname(me->me_dv))); 346 #ifdef DIAGNOSTIC 347 if (me->me_parent != sc) { 348 printf("wsmuxclose: bad child=%p\n", me); 349 continue; 350 } 351 #endif 352 (void)wsevsrc_close(me); 353 me->me_evp = NULL; 354 } 355 } 356 357 /* 358 * read() of the pseudo device from device table. 359 */ 360 int 361 wsmuxread(dev_t dev, struct uio *uio, int flags) 362 { 363 int minr = minor(dev); 364 struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)]; 365 struct wseventvar *evar; 366 int error; 367 368 if (WSMUXCTL(minr)) { 369 /* control device */ 370 return (EINVAL); 371 } 372 373 evar = sc->sc_base.me_evp; 374 if (evar == NULL) { 375 #ifdef DIAGNOSTIC 376 /* XXX can we get here? */ 377 printf("wsmuxread: not open\n"); 378 #endif 379 return (EINVAL); 380 } 381 382 DPRINTFN(5,("wsmuxread: %s event read evar=%p\n", 383 device_xname(sc->sc_base.me_dv), evar)); 384 error = wsevent_read(evar, uio, flags); 385 DPRINTFN(5,("wsmuxread: %s event read ==> error=%d\n", 386 device_xname(sc->sc_base.me_dv), error)); 387 return (error); 388 } 389 390 /* 391 * ioctl of the pseudo device from device table. 392 */ 393 int 394 wsmuxioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 395 { 396 int u = WSMUXDEV(minor(dev)); 397 398 return wsmux_do_ioctl(wsmuxdevs[u]->sc_base.me_dv, cmd, data, flag, l); 399 } 400 401 /* 402 * ioctl of a mux via the parent mux, continuation of wsmuxioctl(). 403 */ 404 int 405 wsmux_do_ioctl(device_t dv, u_long cmd, void *data, int flag, 406 struct lwp *lwp) 407 { 408 struct wsmux_softc *sc = device_private(dv); 409 struct wsevsrc *me; 410 int error, ok; 411 int s, n; 412 struct wseventvar *evar; 413 struct wscons_event event; 414 struct wsmux_device_list *l; 415 416 DPRINTF(("wsmux_do_ioctl: %s: enter sc=%p, cmd=%08lx\n", 417 device_xname(sc->sc_base.me_dv), sc, cmd)); 418 419 switch (cmd) { 420 #if defined(COMPAT_50) || defined(MODULAR) 421 case WSMUXIO_OINJECTEVENT: 422 #endif /* defined(COMPAT_50) || defined(MODULAR) */ 423 case WSMUXIO_INJECTEVENT: 424 /* Inject an event, e.g., from moused. */ 425 DPRINTF(("%s: inject\n", device_xname(sc->sc_base.me_dv))); 426 427 evar = sc->sc_base.me_evp; 428 if (evar == NULL) { 429 /* No event sink, so ignore it. */ 430 DPRINTF(("wsmux_do_ioctl: event ignored\n")); 431 return (0); 432 } 433 434 s = spltty(); 435 event.type = ((struct wscons_event *)data)->type; 436 event.value = ((struct wscons_event *)data)->value; 437 error = wsevent_inject(evar, &event, 1); 438 splx(s); 439 440 return error; 441 case WSMUXIO_ADD_DEVICE: 442 #define d ((struct wsmux_device *)data) 443 DPRINTF(("%s: add type=%d, no=%d\n", 444 device_xname(sc->sc_base.me_dv), d->type, d->idx)); 445 switch (d->type) { 446 #if NWSMOUSE > 0 447 case WSMUX_MOUSE: 448 return (wsmouse_add_mux(d->idx, sc)); 449 #endif 450 #if NWSKBD > 0 451 case WSMUX_KBD: 452 return (wskbd_add_mux(d->idx, sc)); 453 #endif 454 case WSMUX_MUX: 455 return (wsmux_add_mux(d->idx, sc)); 456 default: 457 return (EINVAL); 458 } 459 case WSMUXIO_REMOVE_DEVICE: 460 DPRINTF(("%s: rem type=%d, no=%d\n", 461 device_xname(sc->sc_base.me_dv), d->type, d->idx)); 462 /* Locate the device */ 463 TAILQ_FOREACH(me, &sc->sc_cld, me_next) { 464 if (me->me_ops->type == d->type && 465 device_unit(me->me_dv) == d->idx) { 466 DPRINTF(("wsmux_do_ioctl: detach\n")); 467 wsmux_detach_sc(me); 468 return (0); 469 } 470 } 471 return (EINVAL); 472 #undef d 473 474 case WSMUXIO_LIST_DEVICES: 475 DPRINTF(("%s: list\n", device_xname(sc->sc_base.me_dv))); 476 l = (struct wsmux_device_list *)data; 477 n = 0; 478 TAILQ_FOREACH(me, &sc->sc_cld, me_next) { 479 if (n >= WSMUX_MAXDEV) 480 break; 481 l->devices[n].type = me->me_ops->type; 482 l->devices[n].idx = device_unit(me->me_dv); 483 n++; 484 } 485 l->ndevices = n; 486 return (0); 487 #ifdef WSDISPLAY_COMPAT_RAWKBD 488 case WSKBDIO_SETMODE: 489 sc->sc_rawkbd = *(int *)data; 490 DPRINTF(("wsmux_do_ioctl: save rawkbd = %d\n", sc->sc_rawkbd)); 491 break; 492 #endif 493 494 case WSKBDIO_SETVERSION: 495 case WSMOUSEIO_SETVERSION: 496 case WSDISPLAYIO_SETVERSION: 497 DPRINTF(("%s: WSxxxIO_SETVERSION\n", device_xname(sc->sc_base.me_dv))); 498 evar = sc->sc_base.me_evp; 499 if (evar == NULL) 500 return (EINVAL); 501 return wsevent_setversion(evar, *(int *)data); 502 503 case FIONBIO: 504 DPRINTF(("%s: FIONBIO\n", device_xname(sc->sc_base.me_dv))); 505 return (0); 506 507 case FIOASYNC: 508 DPRINTF(("%s: FIOASYNC\n", device_xname(sc->sc_base.me_dv))); 509 evar = sc->sc_base.me_evp; 510 if (evar == NULL) 511 return (EINVAL); 512 evar->async = *(int *)data != 0; 513 return (0); 514 case FIOSETOWN: 515 DPRINTF(("%s: FIOSETOWN\n", device_xname(sc->sc_base.me_dv))); 516 evar = sc->sc_base.me_evp; 517 if (evar == NULL) 518 return (EINVAL); 519 if (-*(int *)data != evar->io->p_pgid 520 && *(int *)data != evar->io->p_pid) 521 return (EPERM); 522 return (0); 523 case TIOCSPGRP: 524 DPRINTF(("%s: TIOCSPGRP\n", device_xname(sc->sc_base.me_dv))); 525 evar = sc->sc_base.me_evp; 526 if (evar == NULL) 527 return (EINVAL); 528 if (*(int *)data != evar->io->p_pgid) 529 return (EPERM); 530 return (0); 531 default: 532 DPRINTF(("%s: unknown\n", device_xname(sc->sc_base.me_dv))); 533 break; 534 } 535 536 if (sc->sc_base.me_evp == NULL 537 #if NWSDISPLAY > 0 538 && sc->sc_base.me_dispdv == NULL 539 #endif 540 ) 541 return (EACCES); 542 543 /* Return 0 if any of the ioctl() succeeds, otherwise the last error */ 544 error = 0; 545 ok = 0; 546 TAILQ_FOREACH(me, &sc->sc_cld, me_next) { 547 #ifdef DIAGNOSTIC 548 /* XXX check evp? */ 549 if (me->me_parent != sc) { 550 printf("wsmux_do_ioctl: bad child %p\n", me); 551 continue; 552 } 553 #endif 554 error = wsevsrc_ioctl(me, cmd, data, flag, lwp); 555 DPRINTF(("wsmux_do_ioctl: %s: me=%p dev=%s ==> %d\n", 556 device_xname(sc->sc_base.me_dv), me, 557 device_xname(me->me_dv), error)); 558 if (!error) 559 ok = 1; 560 } 561 if (ok) { 562 error = 0; 563 if (cmd == WSKBDIO_SETENCODING) { 564 sc->sc_kbd_layout = *((kbd_t *)data); 565 } 566 567 } 568 569 return (error); 570 } 571 572 /* 573 * poll() of the pseudo device from device table. 574 */ 575 int 576 wsmuxpoll(dev_t dev, int events, struct lwp *l) 577 { 578 int minr = minor(dev); 579 struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)]; 580 581 if (WSMUXCTL(minr)) { 582 /* control device */ 583 return (0); 584 } 585 586 if (sc->sc_base.me_evp == NULL) { 587 #ifdef DIAGNOSTIC 588 printf("wsmuxpoll: not open\n"); 589 #endif 590 return (POLLHUP); 591 } 592 593 return (wsevent_poll(sc->sc_base.me_evp, events, l)); 594 } 595 596 /* 597 * kqfilter() of the pseudo device from device table. 598 */ 599 int 600 wsmuxkqfilter(dev_t dev, struct knote *kn) 601 { 602 int minr = minor(dev); 603 struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)]; 604 605 if (WSMUXCTL(minr)) { 606 /* control device */ 607 return (1); 608 } 609 610 if (sc->sc_base.me_evp == NULL) { 611 #ifdef DIAGNOSTIC 612 printf("wsmuxkqfilter: not open\n"); 613 #endif 614 return (1); 615 } 616 617 return (wsevent_kqfilter(sc->sc_base.me_evp, kn)); 618 } 619 620 /* 621 * Add mux unit as a child to muxsc. 622 */ 623 int 624 wsmux_add_mux(int unit, struct wsmux_softc *muxsc) 625 { 626 struct wsmux_softc *sc, *m; 627 628 sc = wsmux_getmux(unit); 629 if (sc == NULL) 630 return (ENXIO); 631 632 DPRINTF(("wsmux_add_mux: %s(%p) to %s(%p)\n", 633 device_xname(sc->sc_base.me_dv), sc, 634 device_xname(muxsc->sc_base.me_dv), muxsc)); 635 636 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL) 637 return (EBUSY); 638 639 /* The mux we are adding must not be an ancestor of itself. */ 640 for (m = muxsc; m != NULL ; m = m->sc_base.me_parent) 641 if (m == sc) 642 return (EINVAL); 643 644 return (wsmux_attach_sc(muxsc, &sc->sc_base)); 645 } 646 647 /* Create a new mux softc. */ 648 struct wsmux_softc * 649 wsmux_create(const char *name, int unit) 650 { 651 struct wsmux_softc *sc; 652 653 /* XXX This is wrong -- should use autoconfiguraiton framework */ 654 655 DPRINTF(("wsmux_create: allocating\n")); 656 sc = malloc(sizeof *sc, M_DEVBUF, M_NOWAIT|M_ZERO); 657 if (sc == NULL) 658 return (NULL); 659 sc->sc_base.me_dv = malloc(sizeof(struct device), M_DEVBUF, M_NOWAIT|M_ZERO); 660 if (sc->sc_base.me_dv == NULL) { 661 free(sc, M_DEVBUF); 662 return NULL; 663 } 664 TAILQ_INIT(&sc->sc_cld); 665 snprintf(sc->sc_base.me_dv->dv_xname, sizeof sc->sc_base.me_dv->dv_xname, 666 "%s%d", name, unit); 667 sc->sc_base.me_dv->dv_private = sc; 668 sc->sc_base.me_dv->dv_unit = unit; 669 sc->sc_base.me_ops = &wsmux_srcops; 670 sc->sc_kbd_layout = KB_NONE; 671 return (sc); 672 } 673 674 /* Attach me as a child to sc. */ 675 int 676 wsmux_attach_sc(struct wsmux_softc *sc, struct wsevsrc *me) 677 { 678 int error; 679 680 if (sc == NULL) 681 return (EINVAL); 682 683 DPRINTF(("wsmux_attach_sc: %s(%p): type=%d\n", 684 device_xname(sc->sc_base.me_dv), sc, me->me_ops->type)); 685 686 #ifdef DIAGNOSTIC 687 if (me->me_parent != NULL) { 688 printf("wsmux_attach_sc: busy\n"); 689 return (EBUSY); 690 } 691 #endif 692 me->me_parent = sc; 693 TAILQ_INSERT_TAIL(&sc->sc_cld, me, me_next); 694 695 error = 0; 696 #if NWSDISPLAY > 0 697 if (sc->sc_base.me_dispdv != NULL) { 698 /* This is a display mux, so attach the new device to it. */ 699 DPRINTF(("wsmux_attach_sc: %s: set display %p\n", 700 device_xname(sc->sc_base.me_dv), 701 sc->sc_base.me_dispdv)); 702 if (me->me_ops->dsetdisplay != NULL) { 703 error = wsevsrc_set_display(me, &sc->sc_base); 704 /* Ignore that the console already has a display. */ 705 if (error == EBUSY) 706 error = 0; 707 if (!error) { 708 #ifdef WSDISPLAY_COMPAT_RAWKBD 709 DPRINTF(("wsmux_attach_sc: %s set rawkbd=%d\n", 710 device_xname(me->me_dv), 711 sc->sc_rawkbd)); 712 (void)wsevsrc_ioctl(me, WSKBDIO_SETMODE, 713 &sc->sc_rawkbd, 0, 0); 714 #endif 715 if (sc->sc_kbd_layout != KB_NONE) 716 (void)wsevsrc_ioctl(me, 717 WSKBDIO_SETENCODING, 718 &sc->sc_kbd_layout, FWRITE, 0); 719 } 720 } 721 } 722 #endif 723 if (sc->sc_base.me_evp != NULL) { 724 /* Mux is open, so open the new subdevice */ 725 DPRINTF(("wsmux_attach_sc: %s: calling open of %s\n", 726 device_xname(sc->sc_base.me_dv), 727 device_xname(me->me_dv))); 728 error = wsevsrc_open(me, sc->sc_base.me_evp); 729 } else { 730 DPRINTF(("wsmux_attach_sc: %s not open\n", 731 device_xname(sc->sc_base.me_dv))); 732 } 733 734 if (error) { 735 me->me_parent = NULL; 736 TAILQ_REMOVE(&sc->sc_cld, me, me_next); 737 } 738 739 DPRINTF(("wsmux_attach_sc: %s(%p) done, error=%d\n", 740 device_xname(sc->sc_base.me_dv), sc, error)); 741 return (error); 742 } 743 744 /* Remove me from the parent. */ 745 void 746 wsmux_detach_sc(struct wsevsrc *me) 747 { 748 struct wsmux_softc *sc = me->me_parent; 749 750 DPRINTF(("wsmux_detach_sc: %s(%p) parent=%p\n", 751 device_xname(me->me_dv), me, sc)); 752 753 #ifdef DIAGNOSTIC 754 if (sc == NULL) { 755 printf("wsmux_detach_sc: %s has no parent\n", 756 device_xname(me->me_dv)); 757 return; 758 } 759 #endif 760 761 #if NWSDISPLAY > 0 762 if (sc->sc_base.me_dispdv != NULL) { 763 if (me->me_ops->dsetdisplay != NULL) 764 /* ignore error, there's nothing we can do */ 765 (void)wsevsrc_set_display(me, NULL); 766 } else 767 #endif 768 if (me->me_evp != NULL) { 769 DPRINTF(("wsmux_detach_sc: close\n")); 770 /* mux device is open, so close multiplexee */ 771 (void)wsevsrc_close(me); 772 } 773 774 TAILQ_REMOVE(&sc->sc_cld, me, me_next); 775 me->me_parent = NULL; 776 777 DPRINTF(("wsmux_detach_sc: done sc=%p\n", sc)); 778 } 779 780 /* 781 * Display ioctl() of a mux via the parent mux. 782 */ 783 int 784 wsmux_do_displayioctl(device_t dv, u_long cmd, void *data, int flag, 785 struct lwp *l) 786 { 787 struct wsmux_softc *sc = device_private(dv); 788 struct wsevsrc *me; 789 int error, ok; 790 791 DPRINTF(("wsmux_displayioctl: %s: sc=%p, cmd=%08lx\n", 792 device_xname(sc->sc_base.me_dv), sc, cmd)); 793 794 #ifdef WSDISPLAY_COMPAT_RAWKBD 795 if (cmd == WSKBDIO_SETMODE) { 796 sc->sc_rawkbd = *(int *)data; 797 DPRINTF(("wsmux_displayioctl: rawkbd = %d\n", sc->sc_rawkbd)); 798 } 799 #endif 800 801 /* 802 * Return 0 if any of the ioctl() succeeds, otherwise the last error. 803 * Return EPASSTHROUGH if no mux component accepts the ioctl. 804 */ 805 error = EPASSTHROUGH; 806 ok = 0; 807 TAILQ_FOREACH(me, &sc->sc_cld, me_next) { 808 DPRINTF(("wsmux_displayioctl: me=%p\n", me)); 809 #ifdef DIAGNOSTIC 810 if (me->me_parent != sc) { 811 printf("wsmux_displayioctl: bad child %p\n", me); 812 continue; 813 } 814 #endif 815 if (me->me_ops->ddispioctl != NULL) { 816 error = wsevsrc_display_ioctl(me, cmd, data, flag, l); 817 DPRINTF(("wsmux_displayioctl: me=%p dev=%s ==> %d\n", 818 me, device_xname(me->me_dv), error)); 819 if (!error) 820 ok = 1; 821 } 822 } 823 if (ok) 824 error = 0; 825 826 return (error); 827 } 828 829 #if NWSDISPLAY > 0 830 /* 831 * Set display of a mux via the parent mux. 832 */ 833 int 834 wsmux_evsrc_set_display(device_t dv, struct wsevsrc *ame) 835 { 836 struct wsmux_softc *muxsc = (struct wsmux_softc *)ame; 837 struct wsmux_softc *sc = device_private(dv); 838 device_t displaydv = muxsc ? muxsc->sc_base.me_dispdv : NULL; 839 840 DPRINTF(("wsmux_set_display: %s: displaydv=%p\n", 841 device_xname(sc->sc_base.me_dv), displaydv)); 842 843 if (displaydv != NULL) { 844 if (sc->sc_base.me_dispdv != NULL) 845 return (EBUSY); 846 } else { 847 if (sc->sc_base.me_dispdv == NULL) 848 return (ENXIO); 849 } 850 851 return wsmux_set_display(sc, displaydv); 852 } 853 854 int 855 wsmux_set_display(struct wsmux_softc *sc, device_t displaydv) 856 { 857 device_t odisplaydv; 858 struct wsevsrc *me; 859 struct wsmux_softc *nsc = displaydv ? sc : NULL; 860 int error, ok; 861 862 odisplaydv = sc->sc_base.me_dispdv; 863 sc->sc_base.me_dispdv = displaydv; 864 865 if (displaydv) 866 aprint_verbose_dev(sc->sc_base.me_dv, "connecting to %s\n", 867 device_xname(displaydv)); 868 ok = 0; 869 error = 0; 870 TAILQ_FOREACH(me, &sc->sc_cld,me_next) { 871 #ifdef DIAGNOSTIC 872 if (me->me_parent != sc) { 873 printf("wsmux_set_display: bad child parent %p\n", me); 874 continue; 875 } 876 #endif 877 if (me->me_ops->dsetdisplay != NULL) { 878 error = wsevsrc_set_display(me, &nsc->sc_base); 879 DPRINTF(("wsmux_set_display: m=%p dev=%s error=%d\n", 880 me, device_xname(me->me_dv), error)); 881 if (!error) { 882 ok = 1; 883 #ifdef WSDISPLAY_COMPAT_RAWKBD 884 DPRINTF(("wsmux_set_display: %s set rawkbd=%d\n", 885 device_xname(me->me_dv), sc->sc_rawkbd)); 886 (void)wsevsrc_ioctl(me, WSKBDIO_SETMODE, 887 &sc->sc_rawkbd, 0, 0); 888 #endif 889 } 890 } 891 } 892 if (ok) 893 error = 0; 894 895 if (displaydv == NULL) 896 aprint_verbose("%s: disconnecting from %s\n", 897 device_xname(sc->sc_base.me_dv), 898 device_xname(odisplaydv)); 899 900 return (error); 901 } 902 #endif /* NWSDISPLAY > 0 */ 903