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