wsmux.c revision 1.64 1 /* $NetBSD: wsmux.c,v 1.64 2019/11/10 21:16:38 chs Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Author: Lennart Augustsson <lennart (at) 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.64 2019/11/10 21:16:38 chs Exp $");
41
42 #ifdef _KERNEL_OPT
43 #include "opt_compat_netbsd.h"
44 #include "opt_modular.h"
45 #endif
46
47 #include "wsdisplay.h"
48 #include "wsmux.h"
49 #include "wskbd.h"
50 #include "wsmouse.h"
51
52 #include <sys/param.h>
53 #include <sys/conf.h>
54 #include <sys/ioctl.h>
55 #include <sys/poll.h>
56 #include <sys/fcntl.h>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/proc.h>
60 #include <sys/queue.h>
61 #include <sys/syslog.h>
62 #include <sys/systm.h>
63 #include <sys/tty.h>
64 #include <sys/signalvar.h>
65 #include <sys/device.h>
66
67 #include "opt_wsdisplay_compat.h"
68
69 #include <dev/wscons/wsconsio.h>
70 #include <dev/wscons/wsksymdef.h>
71 #include <dev/wscons/wseventvar.h>
72 #include <dev/wscons/wscons_callbacks.h>
73 #include <dev/wscons/wsmuxvar.h>
74
75 #include "ioconf.h"
76
77 #ifdef WSMUX_DEBUG
78 #define DPRINTF(x) if (wsmuxdebug) printf x
79 #define DPRINTFN(n,x) if (wsmuxdebug > (n)) printf x
80 int wsmuxdebug = 0;
81 #else
82 #define DPRINTF(x)
83 #define DPRINTFN(n,x)
84 #endif
85
86 /*
87 * The wsmux pseudo device is used to multiplex events from several wsmouse,
88 * wskbd, and/or wsmux devices together.
89 * The devices connected together form a tree with muxes in the interior
90 * and real devices (mouse and kbd) at the leaves. The special case of
91 * a tree with one node (mux or other) is supported as well.
92 * Only the device at the root of the tree can be opened (if a non-root
93 * device is opened the subtree rooted at that point is severed from the
94 * containing tree). When the root is opened it allocates a wseventvar
95 * struct which all the nodes in the tree will send their events too.
96 * An ioctl() performed on the root is propagated to all the nodes.
97 * There are also ioctl() operations to add and remove nodes from a tree.
98 */
99
100 static int wsmux_mux_open(struct wsevsrc *, struct wseventvar *);
101 static int wsmux_mux_close(struct wsevsrc *);
102
103 static void wsmux_do_open(struct wsmux_softc *, struct wseventvar *);
104
105 static void wsmux_do_close(struct wsmux_softc *);
106 #if NWSDISPLAY > 0
107 static int wsmux_evsrc_set_display(device_t, struct wsevsrc *);
108 #else
109 #define wsmux_evsrc_set_display NULL
110 #endif
111
112 static int wsmux_do_displayioctl(device_t dev, u_long cmd,
113 void *data, int flag, struct lwp *l);
114 static int wsmux_do_ioctl(device_t, u_long, void *,int,struct lwp *);
115
116 static int wsmux_add_mux(int, struct wsmux_softc *);
117
118 #define WSMUXDEV(n) ((n) & 0x7f)
119 #define WSMUXCTL(n) ((n) & 0x80)
120
121 dev_type_open(wsmuxopen);
122 dev_type_close(wsmuxclose);
123 dev_type_read(wsmuxread);
124 dev_type_ioctl(wsmuxioctl);
125 dev_type_poll(wsmuxpoll);
126 dev_type_kqfilter(wsmuxkqfilter);
127
128 const struct cdevsw wsmux_cdevsw = {
129 .d_open = wsmuxopen,
130 .d_close = wsmuxclose,
131 .d_read = wsmuxread,
132 .d_write = nowrite,
133 .d_ioctl = wsmuxioctl,
134 .d_stop = nostop,
135 .d_tty = notty,
136 .d_poll = wsmuxpoll,
137 .d_mmap = nommap,
138 .d_kqfilter = wsmuxkqfilter,
139 .d_discard = nodiscard,
140 .d_flag = D_OTHER
141 };
142
143 struct wssrcops wsmux_srcops = {
144 WSMUX_MUX,
145 wsmux_mux_open, wsmux_mux_close, wsmux_do_ioctl, wsmux_do_displayioctl,
146 wsmux_evsrc_set_display
147 };
148
149 /* From upper level */
150 void
151 wsmuxattach(int n)
152 {
153 }
154
155 /* Keep track of all muxes that have been allocated */
156 static struct wsmux_softc **wsmuxdevs = NULL;
157 static int nwsmux = 0;
158
159 /* Return mux n, create if necessary */
160 struct wsmux_softc *
161 wsmux_getmux(int n)
162 {
163 struct wsmux_softc *sc;
164
165 n = WSMUXDEV(n); /* limit range */
166
167 /* Make sure there is room for mux n in the table */
168 if (n >= nwsmux) {
169 void *new;
170
171 new = realloc(wsmuxdevs, (n + 1) * sizeof(*wsmuxdevs),
172 M_DEVBUF, M_ZERO | M_WAITOK);
173 wsmuxdevs = new;
174 nwsmux = n + 1;
175 }
176
177 sc = wsmuxdevs[n];
178 if (sc == NULL) {
179 sc = wsmux_create("wsmux", n);
180 wsmuxdevs[n] = sc;
181 }
182 return (sc);
183 }
184
185 /*
186 * open() of the pseudo device from device table.
187 */
188 int
189 wsmuxopen(dev_t dev, int flags, int mode, struct lwp *l)
190 {
191 struct wsmux_softc *sc;
192 struct wseventvar *evar;
193 int minr, unit;
194
195 minr = minor(dev);
196 unit = WSMUXDEV(minr);
197 sc = wsmux_getmux(unit);
198 if (sc == NULL)
199 return (ENXIO);
200
201 DPRINTF(("wsmuxopen: %s: sc=%p l=%p\n",
202 device_xname(sc->sc_base.me_dv), sc, l));
203
204 if (WSMUXCTL(minr)) {
205 /* This is the control device which does not allow reads. */
206 if (flags & FREAD)
207 return (EINVAL);
208 return (0);
209 }
210 if ((flags & (FREAD | FWRITE)) == FWRITE)
211 /* Allow write only open */
212 return (0);
213
214 if (sc->sc_base.me_parent != NULL) {
215 /* Grab the mux out of the greedy hands of the parent mux. */
216 DPRINTF(("wsmuxopen: detach\n"));
217 wsmux_detach_sc(&sc->sc_base);
218 }
219
220 if (sc->sc_base.me_evp != NULL)
221 /* Already open. */
222 return (EBUSY);
223
224 evar = &sc->sc_base.me_evar;
225 wsevent_init(evar, l->l_proc);
226 #ifdef WSDISPLAY_COMPAT_RAWKBD
227 sc->sc_rawkbd = 0;
228 #endif
229
230 wsmux_do_open(sc, evar);
231
232 return (0);
233 }
234
235 /*
236 * Open of a mux via the parent mux.
237 */
238 int
239 wsmux_mux_open(struct wsevsrc *me, struct wseventvar *evar)
240 {
241 struct wsmux_softc *sc = (struct wsmux_softc *)me;
242
243 #ifdef DIAGNOSTIC
244 if (sc->sc_base.me_evp != NULL) {
245 printf("wsmux_mux_open: busy\n");
246 return (EBUSY);
247 }
248 if (sc->sc_base.me_parent == NULL) {
249 printf("wsmux_mux_open: no parent\n");
250 return (EINVAL);
251 }
252 #endif
253
254 wsmux_do_open(sc, evar);
255
256 return (0);
257 }
258
259 /* Common part of opening a mux. */
260 void
261 wsmux_do_open(struct wsmux_softc *sc, struct wseventvar *evar)
262 {
263 struct wsevsrc *me;
264
265 sc->sc_base.me_evp = evar; /* remember event variable, mark as open */
266
267 /* Open all children. */
268 TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
269 DPRINTF(("wsmuxopen: %s: m=%p dev=%s\n",
270 device_xname(sc->sc_base.me_dv), me,
271 device_xname(me->me_dv)));
272 #ifdef DIAGNOSTIC
273 if (me->me_evp != NULL) {
274 printf("wsmuxopen: dev already in use\n");
275 continue;
276 }
277 if (me->me_parent != sc) {
278 printf("wsmux_do_open: bad child=%p\n", me);
279 continue;
280 }
281 {
282 int error = wsevsrc_open(me, evar);
283 if (error) {
284 DPRINTF(("wsmuxopen: open failed %d\n", error));
285 }
286 }
287 #else
288 /* ignore errors, failing children will not be marked open */
289 (void)wsevsrc_open(me, evar);
290 #endif
291 }
292 }
293
294 /*
295 * close() of the pseudo device from device table.
296 */
297 int
298 wsmuxclose(dev_t dev, int flags, int mode,
299 struct lwp *l)
300 {
301 int minr = minor(dev);
302 struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
303 struct wseventvar *evar = sc->sc_base.me_evp;
304
305 if (WSMUXCTL(minr))
306 /* control device */
307 return (0);
308 if (evar == NULL)
309 /* Not open for read */
310 return (0);
311
312 wsmux_do_close(sc);
313 sc->sc_base.me_evp = NULL;
314 wsevent_fini(evar);
315 return (0);
316 }
317
318 /*
319 * Close of a mux via the parent mux.
320 */
321 int
322 wsmux_mux_close(struct wsevsrc *me)
323 {
324 me->me_evp = NULL;
325 wsmux_do_close((struct wsmux_softc *)me);
326 return (0);
327 }
328
329 /* Common part of closing a mux. */
330 void
331 wsmux_do_close(struct wsmux_softc *sc)
332 {
333 struct wsevsrc *me;
334
335 DPRINTF(("wsmuxclose: %s: sc=%p\n",
336 device_xname(sc->sc_base.me_dv), sc));
337
338 /* Close all the children. */
339 TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
340 DPRINTF(("wsmuxclose %s: m=%p dev=%s\n",
341 device_xname(sc->sc_base.me_dv), me,
342 device_xname(me->me_dv)));
343 #ifdef DIAGNOSTIC
344 if (me->me_parent != sc) {
345 printf("wsmuxclose: bad child=%p\n", me);
346 continue;
347 }
348 #endif
349 (void)wsevsrc_close(me);
350 me->me_evp = NULL;
351 }
352 }
353
354 /*
355 * read() of the pseudo device from device table.
356 */
357 int
358 wsmuxread(dev_t dev, struct uio *uio, int flags)
359 {
360 int minr = minor(dev);
361 struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
362 struct wseventvar *evar;
363 int error;
364
365 if (WSMUXCTL(minr)) {
366 /* control device */
367 return (EINVAL);
368 }
369
370 evar = sc->sc_base.me_evp;
371 if (evar == NULL) {
372 #ifdef DIAGNOSTIC
373 /* XXX can we get here? */
374 printf("wsmuxread: not open\n");
375 #endif
376 return (EINVAL);
377 }
378
379 DPRINTFN(5,("wsmuxread: %s event read evar=%p\n",
380 device_xname(sc->sc_base.me_dv), evar));
381 error = wsevent_read(evar, uio, flags);
382 DPRINTFN(5,("wsmuxread: %s event read ==> error=%d\n",
383 device_xname(sc->sc_base.me_dv), error));
384 return (error);
385 }
386
387 /*
388 * ioctl of the pseudo device from device table.
389 */
390 int
391 wsmuxioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
392 {
393 int u = WSMUXDEV(minor(dev));
394
395 return wsmux_do_ioctl(wsmuxdevs[u]->sc_base.me_dv, cmd, data, flag, l);
396 }
397
398 /*
399 * ioctl of a mux via the parent mux, continuation of wsmuxioctl().
400 */
401 int
402 wsmux_do_ioctl(device_t dv, u_long cmd, void *data, int flag,
403 struct lwp *lwp)
404 {
405 struct wsmux_softc *sc = device_private(dv);
406 struct wsevsrc *me;
407 int error, ok;
408 int s, n;
409 struct wseventvar *evar;
410 struct wscons_event event;
411 struct wsmux_device_list *l;
412
413 DPRINTF(("wsmux_do_ioctl: %s: enter sc=%p, cmd=%08lx\n",
414 device_xname(sc->sc_base.me_dv), sc, cmd));
415
416 switch (cmd) {
417 #if defined(COMPAT_50) || defined(MODULAR)
418 case WSMUXIO_OINJECTEVENT:
419 #endif /* defined(COMPAT_50) || defined(MODULAR) */
420 case WSMUXIO_INJECTEVENT:
421 /* Inject an event, e.g., from moused. */
422 DPRINTF(("%s: inject\n", device_xname(sc->sc_base.me_dv)));
423
424 evar = sc->sc_base.me_evp;
425 if (evar == NULL) {
426 /* No event sink, so ignore it. */
427 DPRINTF(("wsmux_do_ioctl: event ignored\n"));
428 return (0);
429 }
430
431 s = spltty();
432 event.type = ((struct wscons_event *)data)->type;
433 event.value = ((struct wscons_event *)data)->value;
434 error = wsevent_inject(evar, &event, 1);
435 splx(s);
436
437 return error;
438 case WSMUXIO_ADD_DEVICE:
439 #define d ((struct wsmux_device *)data)
440 DPRINTF(("%s: add type=%d, no=%d\n",
441 device_xname(sc->sc_base.me_dv), d->type, d->idx));
442 switch (d->type) {
443 #if NWSMOUSE > 0
444 case WSMUX_MOUSE:
445 return (wsmouse_add_mux(d->idx, sc));
446 #endif
447 #if NWSKBD > 0
448 case WSMUX_KBD:
449 return (wskbd_add_mux(d->idx, sc));
450 #endif
451 case WSMUX_MUX:
452 return (wsmux_add_mux(d->idx, sc));
453 case WSMUX_BELL:
454 return (wsbell_add_mux(d->idx, sc));
455 default:
456 return (EINVAL);
457 }
458 case WSMUXIO_REMOVE_DEVICE:
459 DPRINTF(("%s: rem type=%d, no=%d\n",
460 device_xname(sc->sc_base.me_dv), d->type, d->idx));
461 /* Locate the device */
462 TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
463 if (me->me_ops->type == d->type &&
464 device_unit(me->me_dv) == d->idx) {
465 DPRINTF(("wsmux_do_ioctl: detach\n"));
466 wsmux_detach_sc(me);
467 return (0);
468 }
469 }
470 return (EINVAL);
471 #undef d
472
473 case WSMUXIO_LIST_DEVICES:
474 DPRINTF(("%s: list\n", device_xname(sc->sc_base.me_dv)));
475 l = (struct wsmux_device_list *)data;
476 n = 0;
477 TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
478 if (n >= WSMUX_MAXDEV)
479 break;
480 l->devices[n].type = me->me_ops->type;
481 l->devices[n].idx = device_unit(me->me_dv);
482 n++;
483 }
484 l->ndevices = n;
485 return (0);
486 #ifdef WSDISPLAY_COMPAT_RAWKBD
487 case WSKBDIO_SETMODE:
488 sc->sc_rawkbd = *(int *)data;
489 DPRINTF(("wsmux_do_ioctl: save rawkbd = %d\n", sc->sc_rawkbd));
490 break;
491 #endif
492
493 case WSKBDIO_SETVERSION:
494 case WSMOUSEIO_SETVERSION:
495 case WSDISPLAYIO_SETVERSION:
496 DPRINTF(("%s: WSxxxIO_SETVERSION\n",
497 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_WAITOK|M_ZERO);
657 sc->sc_base.me_dv = malloc(sizeof(struct device), M_DEVBUF,
658 M_WAITOK|M_ZERO);
659 TAILQ_INIT(&sc->sc_cld);
660 snprintf(sc->sc_base.me_dv->dv_xname,
661 sizeof sc->sc_base.me_dv->dv_xname, "%s%d", name, unit);
662 sc->sc_base.me_dv->dv_private = sc;
663 sc->sc_base.me_dv->dv_unit = unit;
664 sc->sc_base.me_ops = &wsmux_srcops;
665 sc->sc_kbd_layout = KB_NONE;
666 return (sc);
667 }
668
669 /* Attach me as a child to sc. */
670 int
671 wsmux_attach_sc(struct wsmux_softc *sc, struct wsevsrc *me)
672 {
673 int error;
674
675 if (sc == NULL)
676 return (EINVAL);
677
678 DPRINTF(("wsmux_attach_sc: %s(%p): type=%d\n",
679 device_xname(sc->sc_base.me_dv), sc, me->me_ops->type));
680
681 #ifdef DIAGNOSTIC
682 if (me->me_parent != NULL) {
683 printf("wsmux_attach_sc: busy\n");
684 return (EBUSY);
685 }
686 #endif
687 me->me_parent = sc;
688 TAILQ_INSERT_TAIL(&sc->sc_cld, me, me_next);
689
690 error = 0;
691 #if NWSDISPLAY > 0
692 if (sc->sc_base.me_dispdv != NULL) {
693 /* This is a display mux, so attach the new device to it. */
694 DPRINTF(("wsmux_attach_sc: %s: set display %p\n",
695 device_xname(sc->sc_base.me_dv),
696 sc->sc_base.me_dispdv));
697 if (me->me_ops->dsetdisplay != NULL) {
698 error = wsevsrc_set_display(me, &sc->sc_base);
699 /* Ignore that the console already has a display. */
700 if (error == EBUSY)
701 error = 0;
702 if (!error) {
703 #ifdef WSDISPLAY_COMPAT_RAWKBD
704 DPRINTF(("wsmux_attach_sc: %s set rawkbd=%d\n",
705 device_xname(me->me_dv),
706 sc->sc_rawkbd));
707 (void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
708 &sc->sc_rawkbd, 0, 0);
709 #endif
710 if (sc->sc_kbd_layout != KB_NONE)
711 (void)wsevsrc_ioctl(me,
712 WSKBDIO_SETENCODING,
713 &sc->sc_kbd_layout, FWRITE, 0);
714 }
715 }
716 }
717 #endif
718 if (sc->sc_base.me_evp != NULL) {
719 /* Mux is open, so open the new subdevice */
720 DPRINTF(("wsmux_attach_sc: %s: calling open of %s\n",
721 device_xname(sc->sc_base.me_dv),
722 device_xname(me->me_dv)));
723 error = wsevsrc_open(me, sc->sc_base.me_evp);
724 } else {
725 DPRINTF(("wsmux_attach_sc: %s not open\n",
726 device_xname(sc->sc_base.me_dv)));
727 }
728
729 if (error) {
730 me->me_parent = NULL;
731 TAILQ_REMOVE(&sc->sc_cld, me, me_next);
732 }
733
734 DPRINTF(("wsmux_attach_sc: %s(%p) done, error=%d\n",
735 device_xname(sc->sc_base.me_dv), sc, error));
736 return (error);
737 }
738
739 /* Remove me from the parent. */
740 void
741 wsmux_detach_sc(struct wsevsrc *me)
742 {
743 struct wsmux_softc *sc = me->me_parent;
744
745 DPRINTF(("wsmux_detach_sc: %s(%p) parent=%p\n",
746 device_xname(me->me_dv), me, sc));
747
748 #ifdef DIAGNOSTIC
749 if (sc == NULL) {
750 printf("wsmux_detach_sc: %s has no parent\n",
751 device_xname(me->me_dv));
752 return;
753 }
754 #endif
755
756 #if NWSDISPLAY > 0
757 if (sc->sc_base.me_dispdv != NULL) {
758 if (me->me_ops->dsetdisplay != NULL)
759 /* ignore error, there's nothing we can do */
760 (void)wsevsrc_set_display(me, NULL);
761 } else
762 #endif
763 if (me->me_evp != NULL) {
764 DPRINTF(("wsmux_detach_sc: close\n"));
765 /* mux device is open, so close multiplexee */
766 (void)wsevsrc_close(me);
767 }
768
769 TAILQ_REMOVE(&sc->sc_cld, me, me_next);
770 me->me_parent = NULL;
771
772 DPRINTF(("wsmux_detach_sc: done sc=%p\n", sc));
773 }
774
775 /*
776 * Display ioctl() of a mux via the parent mux.
777 */
778 int
779 wsmux_do_displayioctl(device_t dv, u_long cmd, void *data, int flag,
780 struct lwp *l)
781 {
782 struct wsmux_softc *sc = device_private(dv);
783 struct wsevsrc *me;
784 int error, ok;
785
786 DPRINTF(("wsmux_displayioctl: %s: sc=%p, cmd=%08lx\n",
787 device_xname(sc->sc_base.me_dv), sc, cmd));
788
789 #ifdef WSDISPLAY_COMPAT_RAWKBD
790 if (cmd == WSKBDIO_SETMODE) {
791 sc->sc_rawkbd = *(int *)data;
792 DPRINTF(("wsmux_displayioctl: rawkbd = %d\n", sc->sc_rawkbd));
793 }
794 #endif
795
796 /*
797 * Return 0 if any of the ioctl() succeeds, otherwise the last error.
798 * Return EPASSTHROUGH if no mux component accepts the ioctl.
799 */
800 error = EPASSTHROUGH;
801 ok = 0;
802 TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
803 DPRINTF(("wsmux_displayioctl: me=%p\n", me));
804 #ifdef DIAGNOSTIC
805 if (me->me_parent != sc) {
806 printf("wsmux_displayioctl: bad child %p\n", me);
807 continue;
808 }
809 #endif
810 if (me->me_ops->ddispioctl != NULL) {
811 error = wsevsrc_display_ioctl(me, cmd, data, flag, l);
812 DPRINTF(("wsmux_displayioctl: me=%p dev=%s ==> %d\n",
813 me, device_xname(me->me_dv), error));
814 if (!error)
815 ok = 1;
816 }
817 }
818 if (ok)
819 error = 0;
820
821 return (error);
822 }
823
824 #if NWSDISPLAY > 0
825 /*
826 * Set display of a mux via the parent mux.
827 */
828 int
829 wsmux_evsrc_set_display(device_t dv, struct wsevsrc *ame)
830 {
831 struct wsmux_softc *muxsc = (struct wsmux_softc *)ame;
832 struct wsmux_softc *sc = device_private(dv);
833 device_t displaydv = muxsc ? muxsc->sc_base.me_dispdv : NULL;
834
835 DPRINTF(("wsmux_set_display: %s: displaydv=%p\n",
836 device_xname(sc->sc_base.me_dv), displaydv));
837
838 if (displaydv != NULL) {
839 if (sc->sc_base.me_dispdv != NULL)
840 return (EBUSY);
841 } else {
842 if (sc->sc_base.me_dispdv == NULL)
843 return (ENXIO);
844 }
845
846 return wsmux_set_display(sc, displaydv);
847 }
848
849 int
850 wsmux_set_display(struct wsmux_softc *sc, device_t displaydv)
851 {
852 device_t odisplaydv;
853 struct wsevsrc *me;
854 struct wsmux_softc *nsc = displaydv ? sc : NULL;
855 int error, ok;
856
857 odisplaydv = sc->sc_base.me_dispdv;
858 sc->sc_base.me_dispdv = displaydv;
859
860 if (displaydv)
861 aprint_verbose_dev(sc->sc_base.me_dv, "connecting to %s\n",
862 device_xname(displaydv));
863 ok = 0;
864 error = 0;
865 TAILQ_FOREACH(me, &sc->sc_cld,me_next) {
866 #ifdef DIAGNOSTIC
867 if (me->me_parent != sc) {
868 printf("wsmux_set_display: bad child parent %p\n", me);
869 continue;
870 }
871 #endif
872 if (me->me_ops->dsetdisplay != NULL) {
873 error = wsevsrc_set_display(me, &nsc->sc_base);
874 DPRINTF(("wsmux_set_display: m=%p dev=%s error=%d\n",
875 me, device_xname(me->me_dv), error));
876 if (!error) {
877 ok = 1;
878 #ifdef WSDISPLAY_COMPAT_RAWKBD
879 DPRINTF(("wsmux_set_display: %s set rawkbd=%d\n",
880 device_xname(me->me_dv), sc->sc_rawkbd));
881 (void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
882 &sc->sc_rawkbd, 0, 0);
883 #endif
884 }
885 }
886 }
887 if (ok)
888 error = 0;
889
890 if (displaydv == NULL)
891 aprint_verbose("%s: disconnecting from %s\n",
892 device_xname(sc->sc_base.me_dv),
893 device_xname(odisplaydv));
894
895 return (error);
896 }
897 #endif /* NWSDISPLAY > 0 */
898