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