xref: /netbsd-src/sys/dev/wscons/wsmux.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: wsmux.c,v 1.63 2017/06/12 08:19:22 pgoyette 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.63 2017/06/12 08:19:22 pgoyette 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_NOWAIT);
173 		if (new == NULL) {
174 			printf("wsmux_getmux: no memory for mux %d\n", n);
175 			return NULL;
176 		}
177 		wsmuxdevs = new;
178 		nwsmux = n + 1;
179 	}
180 
181 	sc = wsmuxdevs[n];
182 	if (sc == NULL) {
183 		sc = wsmux_create("wsmux", n);
184 		if (sc == NULL)
185 			printf("wsmux: attach out of memory\n");
186 		wsmuxdevs[n] = sc;
187 	}
188 	return (sc);
189 }
190 
191 /*
192  * open() of the pseudo device from device table.
193  */
194 int
195 wsmuxopen(dev_t dev, int flags, int mode, struct lwp *l)
196 {
197 	struct wsmux_softc *sc;
198 	struct wseventvar *evar;
199 	int minr, unit;
200 
201 	minr = minor(dev);
202 	unit = WSMUXDEV(minr);
203 	sc = wsmux_getmux(unit);
204 	if (sc == NULL)
205 		return (ENXIO);
206 
207 	DPRINTF(("wsmuxopen: %s: sc=%p l=%p\n",
208 		 device_xname(sc->sc_base.me_dv), sc, l));
209 
210 	if (WSMUXCTL(minr)) {
211 		/* This is the control device which does not allow reads. */
212 		if (flags & FREAD)
213 			return (EINVAL);
214 		return (0);
215 	}
216 	if ((flags & (FREAD | FWRITE)) == FWRITE)
217 		/* Allow write only open */
218 		return (0);
219 
220 	if (sc->sc_base.me_parent != NULL) {
221 		/* Grab the mux out of the greedy hands of the parent mux. */
222 		DPRINTF(("wsmuxopen: detach\n"));
223 		wsmux_detach_sc(&sc->sc_base);
224 	}
225 
226 	if (sc->sc_base.me_evp != NULL)
227 		/* Already open. */
228 		return (EBUSY);
229 
230 	evar = &sc->sc_base.me_evar;
231 	wsevent_init(evar, l->l_proc);
232 #ifdef WSDISPLAY_COMPAT_RAWKBD
233 	sc->sc_rawkbd = 0;
234 #endif
235 
236 	wsmux_do_open(sc, evar);
237 
238 	return (0);
239 }
240 
241 /*
242  * Open of a mux via the parent mux.
243  */
244 int
245 wsmux_mux_open(struct wsevsrc *me, struct wseventvar *evar)
246 {
247 	struct wsmux_softc *sc = (struct wsmux_softc *)me;
248 
249 #ifdef DIAGNOSTIC
250 	if (sc->sc_base.me_evp != NULL) {
251 		printf("wsmux_mux_open: busy\n");
252 		return (EBUSY);
253 	}
254 	if (sc->sc_base.me_parent == NULL) {
255 		printf("wsmux_mux_open: no parent\n");
256 		return (EINVAL);
257 	}
258 #endif
259 
260 	wsmux_do_open(sc, evar);
261 
262 	return (0);
263 }
264 
265 /* Common part of opening a mux. */
266 void
267 wsmux_do_open(struct wsmux_softc *sc, struct wseventvar *evar)
268 {
269 	struct wsevsrc *me;
270 
271 	sc->sc_base.me_evp = evar; /* remember event variable, mark as open */
272 
273 	/* Open all children. */
274 	TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
275 		DPRINTF(("wsmuxopen: %s: m=%p dev=%s\n",
276 			 device_xname(sc->sc_base.me_dv), me,
277 			 device_xname(me->me_dv)));
278 #ifdef DIAGNOSTIC
279 		if (me->me_evp != NULL) {
280 			printf("wsmuxopen: dev already in use\n");
281 			continue;
282 		}
283 		if (me->me_parent != sc) {
284 			printf("wsmux_do_open: bad child=%p\n", me);
285 			continue;
286 		}
287 		{
288 		int error = wsevsrc_open(me, evar);
289 		if (error) {
290 			DPRINTF(("wsmuxopen: open failed %d\n", error));
291 		}
292 		}
293 #else
294 		/* ignore errors, failing children will not be marked open */
295 		(void)wsevsrc_open(me, evar);
296 #endif
297 	}
298 }
299 
300 /*
301  * close() of the pseudo device from device table.
302  */
303 int
304 wsmuxclose(dev_t dev, int flags, int mode,
305     struct lwp *l)
306 {
307 	int minr = minor(dev);
308 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
309 	struct wseventvar *evar = sc->sc_base.me_evp;
310 
311 	if (WSMUXCTL(minr))
312 		/* control device */
313 		return (0);
314 	if (evar == NULL)
315 		/* Not open for read */
316 		return (0);
317 
318 	wsmux_do_close(sc);
319 	sc->sc_base.me_evp = NULL;
320 	wsevent_fini(evar);
321 	return (0);
322 }
323 
324 /*
325  * Close of a mux via the parent mux.
326  */
327 int
328 wsmux_mux_close(struct wsevsrc *me)
329 {
330 	me->me_evp = NULL;
331 	wsmux_do_close((struct wsmux_softc *)me);
332 	return (0);
333 }
334 
335 /* Common part of closing a mux. */
336 void
337 wsmux_do_close(struct wsmux_softc *sc)
338 {
339 	struct wsevsrc *me;
340 
341 	DPRINTF(("wsmuxclose: %s: sc=%p\n",
342 		 device_xname(sc->sc_base.me_dv), sc));
343 
344 	/* Close all the children. */
345 	TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
346 		DPRINTF(("wsmuxclose %s: m=%p dev=%s\n",
347 			 device_xname(sc->sc_base.me_dv), me,
348 			 device_xname(me->me_dv)));
349 #ifdef DIAGNOSTIC
350 		if (me->me_parent != sc) {
351 			printf("wsmuxclose: bad child=%p\n", me);
352 			continue;
353 		}
354 #endif
355 		(void)wsevsrc_close(me);
356 		me->me_evp = NULL;
357 	}
358 }
359 
360 /*
361  * read() of the pseudo device from device table.
362  */
363 int
364 wsmuxread(dev_t dev, struct uio *uio, int flags)
365 {
366 	int minr = minor(dev);
367 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
368 	struct wseventvar *evar;
369 	int error;
370 
371 	if (WSMUXCTL(minr)) {
372 		/* control device */
373 		return (EINVAL);
374 	}
375 
376 	evar = sc->sc_base.me_evp;
377 	if (evar == NULL) {
378 #ifdef DIAGNOSTIC
379 		/* XXX can we get here? */
380 		printf("wsmuxread: not open\n");
381 #endif
382 		return (EINVAL);
383 	}
384 
385 	DPRINTFN(5,("wsmuxread: %s event read evar=%p\n",
386 		    device_xname(sc->sc_base.me_dv), evar));
387 	error = wsevent_read(evar, uio, flags);
388 	DPRINTFN(5,("wsmuxread: %s event read ==> error=%d\n",
389 		    device_xname(sc->sc_base.me_dv), error));
390 	return (error);
391 }
392 
393 /*
394  * ioctl of the pseudo device from device table.
395  */
396 int
397 wsmuxioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
398 {
399 	int u = WSMUXDEV(minor(dev));
400 
401 	return wsmux_do_ioctl(wsmuxdevs[u]->sc_base.me_dv, cmd, data, flag, l);
402 }
403 
404 /*
405  * ioctl of a mux via the parent mux, continuation of wsmuxioctl().
406  */
407 int
408 wsmux_do_ioctl(device_t dv, u_long cmd, void *data, int flag,
409 	       struct lwp *lwp)
410 {
411 	struct wsmux_softc *sc = device_private(dv);
412 	struct wsevsrc *me;
413 	int error, ok;
414 	int s, n;
415 	struct wseventvar *evar;
416 	struct wscons_event event;
417 	struct wsmux_device_list *l;
418 
419 	DPRINTF(("wsmux_do_ioctl: %s: enter sc=%p, cmd=%08lx\n",
420 		 device_xname(sc->sc_base.me_dv), sc, cmd));
421 
422 	switch (cmd) {
423 #if defined(COMPAT_50) || defined(MODULAR)
424 	case WSMUXIO_OINJECTEVENT:
425 #endif /* defined(COMPAT_50) || defined(MODULAR) */
426 	case WSMUXIO_INJECTEVENT:
427 		/* Inject an event, e.g., from moused. */
428 		DPRINTF(("%s: inject\n", device_xname(sc->sc_base.me_dv)));
429 
430 		evar = sc->sc_base.me_evp;
431 		if (evar == NULL) {
432 			/* No event sink, so ignore it. */
433 			DPRINTF(("wsmux_do_ioctl: event ignored\n"));
434 			return (0);
435 		}
436 
437 		s = spltty();
438 		event.type = ((struct wscons_event *)data)->type;
439 		event.value = ((struct wscons_event *)data)->value;
440 		error = wsevent_inject(evar, &event, 1);
441 		splx(s);
442 
443 		return error;
444 	case WSMUXIO_ADD_DEVICE:
445 #define d ((struct wsmux_device *)data)
446 		DPRINTF(("%s: add type=%d, no=%d\n",
447 			 device_xname(sc->sc_base.me_dv), d->type, d->idx));
448 		switch (d->type) {
449 #if NWSMOUSE > 0
450 		case WSMUX_MOUSE:
451 			return (wsmouse_add_mux(d->idx, sc));
452 #endif
453 #if NWSKBD > 0
454 		case WSMUX_KBD:
455 			return (wskbd_add_mux(d->idx, sc));
456 #endif
457 		case WSMUX_MUX:
458 			return (wsmux_add_mux(d->idx, sc));
459 		case WSMUX_BELL:
460 			return (wsbell_add_mux(d->idx, sc));
461 		default:
462 			return (EINVAL);
463 		}
464 	case WSMUXIO_REMOVE_DEVICE:
465 		DPRINTF(("%s: rem type=%d, no=%d\n",
466 			 device_xname(sc->sc_base.me_dv), d->type, d->idx));
467 		/* Locate the device */
468 		TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
469 			if (me->me_ops->type == d->type &&
470 			    device_unit(me->me_dv) == d->idx) {
471 				DPRINTF(("wsmux_do_ioctl: detach\n"));
472 				wsmux_detach_sc(me);
473 				return (0);
474 			}
475 		}
476 		return (EINVAL);
477 #undef d
478 
479 	case WSMUXIO_LIST_DEVICES:
480 		DPRINTF(("%s: list\n", device_xname(sc->sc_base.me_dv)));
481 		l = (struct wsmux_device_list *)data;
482 		n = 0;
483 		TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
484 			if (n >= WSMUX_MAXDEV)
485 				break;
486 			l->devices[n].type = me->me_ops->type;
487 			l->devices[n].idx = device_unit(me->me_dv);
488 			n++;
489 		}
490 		l->ndevices = n;
491 		return (0);
492 #ifdef WSDISPLAY_COMPAT_RAWKBD
493 	case WSKBDIO_SETMODE:
494 		sc->sc_rawkbd = *(int *)data;
495 		DPRINTF(("wsmux_do_ioctl: save rawkbd = %d\n", sc->sc_rawkbd));
496 		break;
497 #endif
498 
499 	case WSKBDIO_SETVERSION:
500 	case WSMOUSEIO_SETVERSION:
501 	case WSDISPLAYIO_SETVERSION:
502 		DPRINTF(("%s: WSxxxIO_SETVERSION\n",
503 			device_xname(sc->sc_base.me_dv)));
504 		evar = sc->sc_base.me_evp;
505 		if (evar == NULL)
506 			return (EINVAL);
507 		return wsevent_setversion(evar, *(int *)data);
508 
509 	case FIONBIO:
510 		DPRINTF(("%s: FIONBIO\n", device_xname(sc->sc_base.me_dv)));
511 		return (0);
512 
513 	case FIOASYNC:
514 		DPRINTF(("%s: FIOASYNC\n", device_xname(sc->sc_base.me_dv)));
515 		evar = sc->sc_base.me_evp;
516 		if (evar == NULL)
517 			return (EINVAL);
518 		evar->async = *(int *)data != 0;
519 		return (0);
520 	case FIOSETOWN:
521 		DPRINTF(("%s: FIOSETOWN\n", device_xname(sc->sc_base.me_dv)));
522 		evar = sc->sc_base.me_evp;
523 		if (evar == NULL)
524 			return (EINVAL);
525 		if (-*(int *)data != evar->io->p_pgid
526 		    && *(int *)data != evar->io->p_pid)
527 			return (EPERM);
528 		return (0);
529 	case TIOCSPGRP:
530 		DPRINTF(("%s: TIOCSPGRP\n", device_xname(sc->sc_base.me_dv)));
531 		evar = sc->sc_base.me_evp;
532 		if (evar == NULL)
533 			return (EINVAL);
534 		if (*(int *)data != evar->io->p_pgid)
535 			return (EPERM);
536 		return (0);
537 	default:
538 		DPRINTF(("%s: unknown\n", device_xname(sc->sc_base.me_dv)));
539 		break;
540 	}
541 
542 	if (sc->sc_base.me_evp == NULL
543 #if NWSDISPLAY > 0
544 	    && sc->sc_base.me_dispdv == NULL
545 #endif
546 	    )
547 		return (EACCES);
548 
549 	/* Return 0 if any of the ioctl() succeeds, otherwise the last error */
550 	error = 0;
551 	ok = 0;
552 	TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
553 #ifdef DIAGNOSTIC
554 		/* XXX check evp? */
555 		if (me->me_parent != sc) {
556 			printf("wsmux_do_ioctl: bad child %p\n", me);
557 			continue;
558 		}
559 #endif
560 		error = wsevsrc_ioctl(me, cmd, data, flag, lwp);
561 		DPRINTF(("wsmux_do_ioctl: %s: me=%p dev=%s ==> %d\n",
562 			 device_xname(sc->sc_base.me_dv), me,
563 			 device_xname(me->me_dv), error));
564 		if (!error)
565 			ok = 1;
566 	}
567 	if (ok) {
568 		error = 0;
569 		if (cmd == WSKBDIO_SETENCODING) {
570 			sc->sc_kbd_layout = *((kbd_t *)data);
571 		}
572 
573 	}
574 
575 	return (error);
576 }
577 
578 /*
579  * poll() of the pseudo device from device table.
580  */
581 int
582 wsmuxpoll(dev_t dev, int events, struct lwp *l)
583 {
584 	int minr = minor(dev);
585 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
586 
587 	if (WSMUXCTL(minr)) {
588 		/* control device */
589 		return (0);
590 	}
591 
592 	if (sc->sc_base.me_evp == NULL) {
593 #ifdef DIAGNOSTIC
594 		printf("wsmuxpoll: not open\n");
595 #endif
596 		return (POLLHUP);
597 	}
598 
599 	return (wsevent_poll(sc->sc_base.me_evp, events, l));
600 }
601 
602 /*
603  * kqfilter() of the pseudo device from device table.
604  */
605 int
606 wsmuxkqfilter(dev_t dev, struct knote *kn)
607 {
608 	int minr = minor(dev);
609 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
610 
611 	if (WSMUXCTL(minr)) {
612 		/* control device */
613 		return (1);
614 	}
615 
616 	if (sc->sc_base.me_evp == NULL) {
617 #ifdef DIAGNOSTIC
618 		printf("wsmuxkqfilter: not open\n");
619 #endif
620 		return (1);
621 	}
622 
623 	return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
624 }
625 
626 /*
627  * Add mux unit as a child to muxsc.
628  */
629 int
630 wsmux_add_mux(int unit, struct wsmux_softc *muxsc)
631 {
632 	struct wsmux_softc *sc, *m;
633 
634 	sc = wsmux_getmux(unit);
635 	if (sc == NULL)
636 		return (ENXIO);
637 
638 	DPRINTF(("wsmux_add_mux: %s(%p) to %s(%p)\n",
639 		 device_xname(sc->sc_base.me_dv), sc,
640 		 device_xname(muxsc->sc_base.me_dv), muxsc));
641 
642 	if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
643 		return (EBUSY);
644 
645 	/* The mux we are adding must not be an ancestor of itself. */
646 	for (m = muxsc; m != NULL ; m = m->sc_base.me_parent)
647 		if (m == sc)
648 			return (EINVAL);
649 
650 	return (wsmux_attach_sc(muxsc, &sc->sc_base));
651 }
652 
653 /* Create a new mux softc. */
654 struct wsmux_softc *
655 wsmux_create(const char *name, int unit)
656 {
657 	struct wsmux_softc *sc;
658 
659 	/* XXX This is wrong -- should use autoconfiguraiton framework */
660 
661 	DPRINTF(("wsmux_create: allocating\n"));
662 	sc = malloc(sizeof *sc, M_DEVBUF, M_NOWAIT|M_ZERO);
663 	if (sc == NULL)
664 		return (NULL);
665 	sc->sc_base.me_dv = malloc(sizeof(struct device), M_DEVBUF,
666 	    M_NOWAIT|M_ZERO);
667 	if (sc->sc_base.me_dv == NULL) {
668 		free(sc, M_DEVBUF);
669 		return NULL;
670 	}
671 	TAILQ_INIT(&sc->sc_cld);
672 	snprintf(sc->sc_base.me_dv->dv_xname,
673 	    sizeof sc->sc_base.me_dv->dv_xname, "%s%d", name, unit);
674 	sc->sc_base.me_dv->dv_private = sc;
675 	sc->sc_base.me_dv->dv_unit = unit;
676 	sc->sc_base.me_ops = &wsmux_srcops;
677 	sc->sc_kbd_layout = KB_NONE;
678 	return (sc);
679 }
680 
681 /* Attach me as a child to sc. */
682 int
683 wsmux_attach_sc(struct wsmux_softc *sc, struct wsevsrc *me)
684 {
685 	int error;
686 
687 	if (sc == NULL)
688 		return (EINVAL);
689 
690 	DPRINTF(("wsmux_attach_sc: %s(%p): type=%d\n",
691 		 device_xname(sc->sc_base.me_dv), sc, me->me_ops->type));
692 
693 #ifdef DIAGNOSTIC
694 	if (me->me_parent != NULL) {
695 		printf("wsmux_attach_sc: busy\n");
696 		return (EBUSY);
697 	}
698 #endif
699 	me->me_parent = sc;
700 	TAILQ_INSERT_TAIL(&sc->sc_cld, me, me_next);
701 
702 	error = 0;
703 #if NWSDISPLAY > 0
704 	if (sc->sc_base.me_dispdv != NULL) {
705 		/* This is a display mux, so attach the new device to it. */
706 		DPRINTF(("wsmux_attach_sc: %s: set display %p\n",
707 			 device_xname(sc->sc_base.me_dv),
708 			 sc->sc_base.me_dispdv));
709 		if (me->me_ops->dsetdisplay != NULL) {
710 			error = wsevsrc_set_display(me, &sc->sc_base);
711 			/* Ignore that the console already has a display. */
712 			if (error == EBUSY)
713 				error = 0;
714 			if (!error) {
715 #ifdef WSDISPLAY_COMPAT_RAWKBD
716 				DPRINTF(("wsmux_attach_sc: %s set rawkbd=%d\n",
717 					 device_xname(me->me_dv),
718 					 sc->sc_rawkbd));
719 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
720 						    &sc->sc_rawkbd, 0, 0);
721 #endif
722 				if (sc->sc_kbd_layout != KB_NONE)
723 					(void)wsevsrc_ioctl(me,
724 					    WSKBDIO_SETENCODING,
725 					    &sc->sc_kbd_layout, FWRITE, 0);
726 			}
727 		}
728 	}
729 #endif
730 	if (sc->sc_base.me_evp != NULL) {
731 		/* Mux is open, so open the new subdevice */
732 		DPRINTF(("wsmux_attach_sc: %s: calling open of %s\n",
733 			 device_xname(sc->sc_base.me_dv),
734 			 device_xname(me->me_dv)));
735 		error = wsevsrc_open(me, sc->sc_base.me_evp);
736 	} else {
737 		DPRINTF(("wsmux_attach_sc: %s not open\n",
738 			 device_xname(sc->sc_base.me_dv)));
739 	}
740 
741 	if (error) {
742 		me->me_parent = NULL;
743 		TAILQ_REMOVE(&sc->sc_cld, me, me_next);
744 	}
745 
746 	DPRINTF(("wsmux_attach_sc: %s(%p) done, error=%d\n",
747 		 device_xname(sc->sc_base.me_dv), sc, error));
748 	return (error);
749 }
750 
751 /* Remove me from the parent. */
752 void
753 wsmux_detach_sc(struct wsevsrc *me)
754 {
755 	struct wsmux_softc *sc = me->me_parent;
756 
757 	DPRINTF(("wsmux_detach_sc: %s(%p) parent=%p\n",
758 		 device_xname(me->me_dv), me, sc));
759 
760 #ifdef DIAGNOSTIC
761 	if (sc == NULL) {
762 		printf("wsmux_detach_sc: %s has no parent\n",
763 		       device_xname(me->me_dv));
764 		return;
765 	}
766 #endif
767 
768 #if NWSDISPLAY > 0
769 	if (sc->sc_base.me_dispdv != NULL) {
770 		if (me->me_ops->dsetdisplay != NULL)
771 			/* ignore error, there's nothing we can do */
772 			(void)wsevsrc_set_display(me, NULL);
773 	} else
774 #endif
775 		if (me->me_evp != NULL) {
776 		DPRINTF(("wsmux_detach_sc: close\n"));
777 		/* mux device is open, so close multiplexee */
778 		(void)wsevsrc_close(me);
779 	}
780 
781 	TAILQ_REMOVE(&sc->sc_cld, me, me_next);
782 	me->me_parent = NULL;
783 
784 	DPRINTF(("wsmux_detach_sc: done sc=%p\n", sc));
785 }
786 
787 /*
788  * Display ioctl() of a mux via the parent mux.
789  */
790 int
791 wsmux_do_displayioctl(device_t dv, u_long cmd, void *data, int flag,
792 		      struct lwp *l)
793 {
794 	struct wsmux_softc *sc = device_private(dv);
795 	struct wsevsrc *me;
796 	int error, ok;
797 
798 	DPRINTF(("wsmux_displayioctl: %s: sc=%p, cmd=%08lx\n",
799 		 device_xname(sc->sc_base.me_dv), sc, cmd));
800 
801 #ifdef WSDISPLAY_COMPAT_RAWKBD
802 	if (cmd == WSKBDIO_SETMODE) {
803 		sc->sc_rawkbd = *(int *)data;
804 		DPRINTF(("wsmux_displayioctl: rawkbd = %d\n", sc->sc_rawkbd));
805 	}
806 #endif
807 
808 	/*
809 	 * Return 0 if any of the ioctl() succeeds, otherwise the last error.
810 	 * Return EPASSTHROUGH if no mux component accepts the ioctl.
811 	 */
812 	error = EPASSTHROUGH;
813 	ok = 0;
814 	TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
815 		DPRINTF(("wsmux_displayioctl: me=%p\n", me));
816 #ifdef DIAGNOSTIC
817 		if (me->me_parent != sc) {
818 			printf("wsmux_displayioctl: bad child %p\n", me);
819 			continue;
820 		}
821 #endif
822 		if (me->me_ops->ddispioctl != NULL) {
823 			error = wsevsrc_display_ioctl(me, cmd, data, flag, l);
824 			DPRINTF(("wsmux_displayioctl: me=%p dev=%s ==> %d\n",
825 				 me, device_xname(me->me_dv), error));
826 			if (!error)
827 				ok = 1;
828 		}
829 	}
830 	if (ok)
831 		error = 0;
832 
833 	return (error);
834 }
835 
836 #if NWSDISPLAY > 0
837 /*
838  * Set display of a mux via the parent mux.
839  */
840 int
841 wsmux_evsrc_set_display(device_t dv, struct wsevsrc *ame)
842 {
843 	struct wsmux_softc *muxsc = (struct wsmux_softc *)ame;
844 	struct wsmux_softc *sc = device_private(dv);
845 	device_t displaydv = muxsc ? muxsc->sc_base.me_dispdv : NULL;
846 
847 	DPRINTF(("wsmux_set_display: %s: displaydv=%p\n",
848 		 device_xname(sc->sc_base.me_dv), displaydv));
849 
850 	if (displaydv != NULL) {
851 		if (sc->sc_base.me_dispdv != NULL)
852 			return (EBUSY);
853 	} else {
854 		if (sc->sc_base.me_dispdv == NULL)
855 			return (ENXIO);
856 	}
857 
858 	return wsmux_set_display(sc, displaydv);
859 }
860 
861 int
862 wsmux_set_display(struct wsmux_softc *sc, device_t displaydv)
863 {
864 	device_t odisplaydv;
865 	struct wsevsrc *me;
866 	struct wsmux_softc *nsc = displaydv ? sc : NULL;
867 	int error, ok;
868 
869 	odisplaydv = sc->sc_base.me_dispdv;
870 	sc->sc_base.me_dispdv = displaydv;
871 
872 	if (displaydv)
873 		aprint_verbose_dev(sc->sc_base.me_dv, "connecting to %s\n",
874 		       device_xname(displaydv));
875 	ok = 0;
876 	error = 0;
877 	TAILQ_FOREACH(me, &sc->sc_cld,me_next) {
878 #ifdef DIAGNOSTIC
879 		if (me->me_parent != sc) {
880 			printf("wsmux_set_display: bad child parent %p\n", me);
881 			continue;
882 		}
883 #endif
884 		if (me->me_ops->dsetdisplay != NULL) {
885 			error = wsevsrc_set_display(me, &nsc->sc_base);
886 			DPRINTF(("wsmux_set_display: m=%p dev=%s error=%d\n",
887 				 me, device_xname(me->me_dv), error));
888 			if (!error) {
889 				ok = 1;
890 #ifdef WSDISPLAY_COMPAT_RAWKBD
891 				DPRINTF(("wsmux_set_display: %s set rawkbd=%d\n",
892 					 device_xname(me->me_dv), sc->sc_rawkbd));
893 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
894 						    &sc->sc_rawkbd, 0, 0);
895 #endif
896 			}
897 		}
898 	}
899 	if (ok)
900 		error = 0;
901 
902 	if (displaydv == NULL)
903 		aprint_verbose("%s: disconnecting from %s\n",
904 		       device_xname(sc->sc_base.me_dv),
905 		       device_xname(odisplaydv));
906 
907 	return (error);
908 }
909 #endif /* NWSDISPLAY > 0 */
910