Home | History | Annotate | Line # | Download | only in wscons
wsevent.c revision 1.24.12.3
      1  1.24.12.1       mjf /* $NetBSD: wsevent.c,v 1.24.12.3 2009/01/17 13:29:10 mjf Exp $ */
      2       1.18      jmmv 
      3       1.18      jmmv /*-
      4  1.24.12.2       mjf  * Copyright (c) 2006, 2008 The NetBSD Foundation, Inc.
      5       1.18      jmmv  * All rights reserved.
      6       1.18      jmmv  *
      7       1.18      jmmv  * This code is derived from software contributed to The NetBSD Foundation
      8       1.18      jmmv  * by Julio M. Merino Vidal.
      9       1.18      jmmv  *
     10       1.18      jmmv  * Redistribution and use in source and binary forms, with or without
     11       1.18      jmmv  * modification, are permitted provided that the following conditions
     12       1.18      jmmv  * are met:
     13       1.18      jmmv  * 1. Redistributions of source code must retain the above copyright
     14       1.18      jmmv  *    notice, this list of conditions and the following disclaimer.
     15       1.18      jmmv  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.18      jmmv  *    notice, this list of conditions and the following disclaimer in the
     17       1.18      jmmv  *    documentation and/or other materials provided with the distribution.
     18       1.18      jmmv  *
     19       1.18      jmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.18      jmmv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.18      jmmv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.18      jmmv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.18      jmmv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.18      jmmv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.18      jmmv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.18      jmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.18      jmmv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.18      jmmv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.18      jmmv  * POSSIBILITY OF SUCH DAMAGE.
     30       1.18      jmmv  */
     31        1.1  drochner 
     32        1.1  drochner /*
     33        1.1  drochner  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
     34        1.1  drochner  *
     35        1.1  drochner  * Redistribution and use in source and binary forms, with or without
     36        1.1  drochner  * modification, are permitted provided that the following conditions
     37        1.1  drochner  * are met:
     38        1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     39        1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     40        1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     41        1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     42        1.1  drochner  *    documentation and/or other materials provided with the distribution.
     43        1.1  drochner  * 3. All advertising materials mentioning features or use of this software
     44        1.1  drochner  *    must display the following acknowledgement:
     45        1.1  drochner  *      This product includes software developed by Christopher G. Demetriou
     46        1.1  drochner  *	for the NetBSD Project.
     47        1.1  drochner  * 4. The name of the author may not be used to endorse or promote products
     48        1.1  drochner  *    derived from this software without specific prior written permission
     49        1.1  drochner  *
     50        1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     51        1.1  drochner  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     52        1.1  drochner  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     53        1.1  drochner  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     54        1.1  drochner  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     55        1.1  drochner  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     56        1.1  drochner  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     57        1.1  drochner  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     58        1.1  drochner  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     59        1.1  drochner  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     60        1.1  drochner  */
     61        1.1  drochner 
     62        1.1  drochner /*
     63        1.1  drochner  * Copyright (c) 1992, 1993
     64        1.1  drochner  *	The Regents of the University of California.  All rights reserved.
     65        1.1  drochner  *
     66        1.1  drochner  * This software was developed by the Computer Systems Engineering group
     67        1.1  drochner  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     68        1.1  drochner  * contributed to Berkeley.
     69        1.1  drochner  *
     70        1.1  drochner  * All advertising materials mentioning features or use of this software
     71        1.1  drochner  * must display the following acknowledgement:
     72        1.1  drochner  *	This product includes software developed by the University of
     73        1.1  drochner  *	California, Lawrence Berkeley Laboratory.
     74        1.1  drochner  *
     75        1.1  drochner  * Redistribution and use in source and binary forms, with or without
     76        1.1  drochner  * modification, are permitted provided that the following conditions
     77        1.1  drochner  * are met:
     78        1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     79        1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     80        1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     81        1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     82        1.1  drochner  *    documentation and/or other materials provided with the distribution.
     83       1.16       agc  * 3. Neither the name of the University nor the names of its contributors
     84        1.1  drochner  *    may be used to endorse or promote products derived from this software
     85        1.1  drochner  *    without specific prior written permission.
     86        1.1  drochner  *
     87        1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     88        1.1  drochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     89        1.1  drochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     90        1.1  drochner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     91        1.1  drochner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     92        1.1  drochner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     93        1.1  drochner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     94        1.1  drochner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     95        1.1  drochner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     96        1.1  drochner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     97        1.1  drochner  * SUCH DAMAGE.
     98        1.1  drochner  *
     99        1.1  drochner  *	@(#)event.c	8.1 (Berkeley) 6/11/93
    100        1.1  drochner  */
    101        1.1  drochner 
    102        1.1  drochner /*
    103        1.1  drochner  * Internal "wscons_event" queue interface for the keyboard and mouse drivers.
    104        1.1  drochner  */
    105        1.9     lukem 
    106        1.9     lukem #include <sys/cdefs.h>
    107  1.24.12.1       mjf __KERNEL_RCSID(0, "$NetBSD: wsevent.c,v 1.24.12.3 2009/01/17 13:29:10 mjf Exp $");
    108        1.1  drochner 
    109  1.24.12.3       mjf #include "opt_compat_netbsd.h"
    110  1.24.12.3       mjf 
    111        1.1  drochner #include <sys/param.h>
    112       1.18      jmmv #include <sys/kernel.h>
    113        1.1  drochner #include <sys/fcntl.h>
    114  1.24.12.3       mjf #include <sys/kmem.h>
    115        1.1  drochner #include <sys/proc.h>
    116        1.1  drochner #include <sys/systm.h>
    117        1.1  drochner #include <sys/vnode.h>
    118        1.1  drochner #include <sys/select.h>
    119        1.1  drochner #include <sys/poll.h>
    120        1.1  drochner 
    121        1.1  drochner #include <dev/wscons/wsconsio.h>
    122        1.1  drochner #include <dev/wscons/wseventvar.h>
    123        1.1  drochner 
    124        1.1  drochner /*
    125       1.18      jmmv  * Size of a wsevent queue (measured in number of events).
    126       1.18      jmmv  * Should be a power of two so that `%' is fast.
    127       1.18      jmmv  * At the moment, the value below makes the queues use 2 Kbytes each; this
    128       1.18      jmmv  * value may need tuning.
    129       1.18      jmmv  */
    130       1.18      jmmv #define	WSEVENT_QSIZE	256
    131       1.18      jmmv 
    132  1.24.12.3       mjf #define EVSIZE(ver)	((ver) == WSEVENT_VERSION ? \
    133  1.24.12.3       mjf     sizeof(struct wscons_event) : \
    134  1.24.12.3       mjf     sizeof(struct owscons_event))
    135  1.24.12.3       mjf #define EVARRAY(ev, idx) (&(ev)->q[(idx)])
    136  1.24.12.3       mjf 
    137       1.18      jmmv /*
    138       1.18      jmmv  * Priority of code managing wsevent queues.  PWSEVENT is set just above
    139       1.18      jmmv  * PSOCK, which is just above TTIPRI, on the theory that mouse and keyboard
    140       1.18      jmmv  * `user' input should be quick.
    141       1.18      jmmv  */
    142       1.18      jmmv #define	PWSEVENT	23
    143       1.18      jmmv #define	splwsevent()	spltty()
    144       1.18      jmmv 
    145  1.24.12.2       mjf static void	wsevent_intr(void *);
    146  1.24.12.2       mjf 
    147       1.18      jmmv /*
    148        1.1  drochner  * Initialize a wscons_event queue.
    149        1.1  drochner  */
    150        1.1  drochner void
    151       1.18      jmmv wsevent_init(struct wseventvar *ev, struct proc *p)
    152        1.1  drochner {
    153        1.1  drochner 
    154       1.13  christos 	if (ev->q != NULL) {
    155        1.7  augustss #ifdef DIAGNOSTIC
    156        1.7  augustss 		printf("wsevent_init: already init\n");
    157       1.13  christos #endif
    158        1.7  augustss 		return;
    159        1.7  augustss 	}
    160  1.24.12.3       mjf 	ev->version = 0;
    161        1.1  drochner 	ev->get = ev->put = 0;
    162  1.24.12.3       mjf 	ev->q = kmem_alloc(WSEVENT_QSIZE * sizeof(*ev->q), KM_SLEEP);
    163  1.24.12.1       mjf 	selinit(&ev->sel);
    164       1.18      jmmv 	ev->io = p;
    165  1.24.12.2       mjf 	ev->sih = softint_establish(SOFTINT_MPSAFE | SOFTINT_CLOCK,
    166  1.24.12.2       mjf 	    wsevent_intr, ev);
    167        1.1  drochner }
    168        1.1  drochner 
    169        1.1  drochner /*
    170        1.1  drochner  * Tear down a wscons_event queue.
    171        1.1  drochner  */
    172        1.1  drochner void
    173        1.6  augustss wsevent_fini(struct wseventvar *ev)
    174        1.1  drochner {
    175       1.13  christos 	if (ev->q == NULL) {
    176       1.13  christos #ifdef DIAGNOSTIC
    177       1.13  christos 		printf("wsevent_fini: already fini\n");
    178       1.13  christos #endif
    179       1.13  christos 		return;
    180       1.13  christos 	}
    181  1.24.12.1       mjf 	seldestroy(&ev->sel);
    182  1.24.12.3       mjf 	kmem_free(ev->q, WSEVENT_QSIZE * sizeof(*ev->q));
    183        1.7  augustss 	ev->q = NULL;
    184  1.24.12.2       mjf 	softint_disestablish(ev->sih);
    185        1.1  drochner }
    186        1.1  drochner 
    187  1.24.12.3       mjf #if defined(COMPAT_50) || defined(MODULAR)
    188  1.24.12.3       mjf static int
    189  1.24.12.3       mjf wsevent_copyout_events50(const struct wscons_event *events, int cnt,
    190  1.24.12.3       mjf     struct uio *uio)
    191  1.24.12.3       mjf {
    192  1.24.12.3       mjf 	int i;
    193  1.24.12.3       mjf 
    194  1.24.12.3       mjf 	for (i = 0; i < cnt; i++) {
    195  1.24.12.3       mjf 		const struct wscons_event *ev = &events[i];
    196  1.24.12.3       mjf 		struct owscons_event ev50;
    197  1.24.12.3       mjf 		int error;
    198  1.24.12.3       mjf 
    199  1.24.12.3       mjf 		ev50.type = ev->type;
    200  1.24.12.3       mjf 		ev50.value = ev->value;
    201  1.24.12.3       mjf 		timespec_to_timespec50(&ev->time, &ev50.time);
    202  1.24.12.3       mjf 
    203  1.24.12.3       mjf 		error = uiomove(&ev50, sizeof(ev50), uio);
    204  1.24.12.3       mjf 		if (error) {
    205  1.24.12.3       mjf 			return error;
    206  1.24.12.3       mjf 		}
    207  1.24.12.3       mjf 	}
    208  1.24.12.3       mjf 	return 0;
    209  1.24.12.3       mjf }
    210  1.24.12.3       mjf #else /* defined(COMPAT_50) || defined(MODULAR) */
    211  1.24.12.3       mjf static int
    212  1.24.12.3       mjf wsevent_copyout_events50(const struct wscons_event *events, int cnt,
    213  1.24.12.3       mjf     struct uio *uio)
    214  1.24.12.3       mjf {
    215  1.24.12.3       mjf 
    216  1.24.12.3       mjf 	panic("%s: unexpected version", __func__);
    217  1.24.12.3       mjf }
    218  1.24.12.3       mjf #endif /* defined(COMPAT_50) || defined(MODULAR) */
    219  1.24.12.3       mjf 
    220  1.24.12.3       mjf static int
    221  1.24.12.3       mjf wsevent_copyout_events(const struct wscons_event *events, int cnt,
    222  1.24.12.3       mjf     struct uio *uio, int ver)
    223  1.24.12.3       mjf {
    224  1.24.12.3       mjf 
    225  1.24.12.3       mjf 	switch (ver) {
    226  1.24.12.3       mjf 	case 0:
    227  1.24.12.3       mjf 		return wsevent_copyout_events50(events, cnt, uio);
    228  1.24.12.3       mjf 	case WSEVENT_VERSION:
    229  1.24.12.3       mjf 		return uiomove(__UNCONST(events), cnt * sizeof(*events), uio);
    230  1.24.12.3       mjf 	default:
    231  1.24.12.3       mjf 		panic("%s: unknown version %d", __func__, ver);
    232  1.24.12.3       mjf 	}
    233  1.24.12.3       mjf }
    234  1.24.12.3       mjf 
    235        1.1  drochner /*
    236        1.1  drochner  * User-level interface: read, poll.
    237        1.1  drochner  * (User cannot write an event queue.)
    238        1.1  drochner  */
    239        1.1  drochner int
    240        1.6  augustss wsevent_read(struct wseventvar *ev, struct uio *uio, int flags)
    241        1.1  drochner {
    242        1.1  drochner 	int s, n, cnt, error;
    243  1.24.12.3       mjf 	const int ver = ev->version;
    244  1.24.12.3       mjf 	const size_t evsize = EVSIZE(ver);
    245        1.1  drochner 
    246        1.1  drochner 	/*
    247        1.1  drochner 	 * Make sure we can return at least 1.
    248        1.1  drochner 	 */
    249  1.24.12.3       mjf 	if (uio->uio_resid < evsize)
    250        1.1  drochner 		return (EMSGSIZE);	/* ??? */
    251        1.1  drochner 	s = splwsevent();
    252        1.1  drochner 	while (ev->get == ev->put) {
    253        1.1  drochner 		if (flags & IO_NDELAY) {
    254        1.1  drochner 			splx(s);
    255        1.1  drochner 			return (EWOULDBLOCK);
    256        1.1  drochner 		}
    257        1.1  drochner 		ev->wanted = 1;
    258  1.24.12.3       mjf 		error = tsleep(ev, PWSEVENT | PCATCH, "wsevent_read", 0);
    259        1.1  drochner 		if (error) {
    260        1.1  drochner 			splx(s);
    261        1.1  drochner 			return (error);
    262        1.1  drochner 		}
    263        1.1  drochner 	}
    264        1.1  drochner 	/*
    265        1.1  drochner 	 * Move wscons_event from tail end of queue (there is at least one
    266        1.1  drochner 	 * there).
    267        1.1  drochner 	 */
    268        1.1  drochner 	if (ev->put < ev->get)
    269        1.1  drochner 		cnt = WSEVENT_QSIZE - ev->get;	/* events in [get..QSIZE) */
    270        1.1  drochner 	else
    271        1.1  drochner 		cnt = ev->put - ev->get;	/* events in [get..put) */
    272        1.1  drochner 	splx(s);
    273  1.24.12.3       mjf 	n = howmany(uio->uio_resid, evsize);
    274        1.1  drochner 	if (cnt > n)
    275        1.1  drochner 		cnt = n;
    276  1.24.12.3       mjf 	error = wsevent_copyout_events(EVARRAY(ev, ev->get), cnt, uio, ver);
    277        1.1  drochner 	n -= cnt;
    278        1.1  drochner 	/*
    279        1.1  drochner 	 * If we do not wrap to 0, used up all our space, or had an error,
    280        1.1  drochner 	 * stop.  Otherwise move from front of queue to put index, if there
    281        1.1  drochner 	 * is anything there to move.
    282        1.1  drochner 	 */
    283        1.1  drochner 	if ((ev->get = (ev->get + cnt) % WSEVENT_QSIZE) != 0 ||
    284        1.1  drochner 	    n == 0 || error || (cnt = ev->put) == 0)
    285        1.1  drochner 		return (error);
    286        1.1  drochner 	if (cnt > n)
    287        1.1  drochner 		cnt = n;
    288  1.24.12.3       mjf 	error = wsevent_copyout_events(EVARRAY(ev, 0), cnt, uio, ver);
    289        1.1  drochner 	ev->get = cnt;
    290        1.1  drochner 	return (error);
    291        1.1  drochner }
    292        1.1  drochner 
    293        1.1  drochner int
    294       1.17  christos wsevent_poll(struct wseventvar *ev, int events, struct lwp *l)
    295        1.1  drochner {
    296        1.1  drochner 	int revents = 0;
    297        1.1  drochner 	int s = splwsevent();
    298        1.1  drochner 
    299        1.2   thorpej         if (events & (POLLIN | POLLRDNORM)) {
    300        1.1  drochner 		if (ev->get != ev->put)
    301        1.2   thorpej 			revents |= events & (POLLIN | POLLRDNORM);
    302        1.2   thorpej 		else
    303       1.17  christos 			selrecord(l, &ev->sel);
    304        1.2   thorpej 	}
    305        1.1  drochner 
    306        1.1  drochner 	splx(s);
    307        1.1  drochner 	return (revents);
    308       1.11  jdolecek }
    309       1.11  jdolecek 
    310       1.11  jdolecek static void
    311       1.11  jdolecek filt_wseventrdetach(struct knote *kn)
    312       1.11  jdolecek {
    313       1.11  jdolecek 	struct wseventvar *ev = kn->kn_hook;
    314       1.11  jdolecek 	int s;
    315       1.11  jdolecek 
    316       1.11  jdolecek 	s = splwsevent();
    317       1.12  christos 	SLIST_REMOVE(&ev->sel.sel_klist, kn, knote, kn_selnext);
    318       1.11  jdolecek 	splx(s);
    319       1.11  jdolecek }
    320       1.11  jdolecek 
    321       1.11  jdolecek static int
    322       1.22  christos filt_wseventread(struct knote *kn, long hint)
    323       1.11  jdolecek {
    324       1.11  jdolecek 	struct wseventvar *ev = kn->kn_hook;
    325       1.11  jdolecek 
    326       1.11  jdolecek 	if (ev->get == ev->put)
    327       1.11  jdolecek 		return (0);
    328       1.11  jdolecek 
    329       1.11  jdolecek 	if (ev->get < ev->put)
    330       1.11  jdolecek 		kn->kn_data = ev->put - ev->get;
    331       1.11  jdolecek 	else
    332  1.24.12.3       mjf 		kn->kn_data = (WSEVENT_QSIZE - ev->get) + ev->put;
    333       1.11  jdolecek 
    334  1.24.12.3       mjf 	kn->kn_data *= EVSIZE(ev->version);
    335       1.11  jdolecek 
    336       1.11  jdolecek 	return (1);
    337       1.11  jdolecek }
    338       1.11  jdolecek 
    339       1.11  jdolecek static const struct filterops wsevent_filtops =
    340       1.11  jdolecek 	{ 1, NULL, filt_wseventrdetach, filt_wseventread };
    341       1.11  jdolecek 
    342       1.11  jdolecek int
    343       1.11  jdolecek wsevent_kqfilter(struct wseventvar *ev, struct knote *kn)
    344       1.11  jdolecek {
    345       1.11  jdolecek 	struct klist *klist;
    346       1.11  jdolecek 	int s;
    347       1.11  jdolecek 
    348       1.11  jdolecek 	switch (kn->kn_filter) {
    349       1.11  jdolecek 	case EVFILT_READ:
    350       1.12  christos 		klist = &ev->sel.sel_klist;
    351       1.11  jdolecek 		kn->kn_fop = &wsevent_filtops;
    352       1.11  jdolecek 		break;
    353       1.11  jdolecek 
    354       1.11  jdolecek 	default:
    355       1.24     pooka 		return (EINVAL);
    356       1.11  jdolecek 	}
    357       1.11  jdolecek 
    358       1.11  jdolecek 	kn->kn_hook = ev;
    359       1.11  jdolecek 
    360       1.11  jdolecek 	s = splwsevent();
    361       1.11  jdolecek 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    362       1.11  jdolecek 	splx(s);
    363       1.11  jdolecek 
    364       1.11  jdolecek 	return (0);
    365        1.1  drochner }
    366       1.18      jmmv 
    367       1.18      jmmv /*
    368       1.18      jmmv  * Wakes up all listener of the 'ev' queue.
    369       1.18      jmmv  */
    370       1.18      jmmv void
    371       1.18      jmmv wsevent_wakeup(struct wseventvar *ev)
    372       1.18      jmmv {
    373       1.18      jmmv 
    374  1.24.12.1       mjf 	selnotify(&ev->sel, 0, 0);
    375       1.18      jmmv 
    376       1.18      jmmv 	if (ev->wanted) {
    377       1.18      jmmv 		ev->wanted = 0;
    378       1.18      jmmv 		wakeup(ev);
    379       1.18      jmmv 	}
    380       1.18      jmmv 
    381       1.23        ad 	if (ev->async) {
    382  1.24.12.2       mjf 		softint_schedule(ev->sih);
    383  1.24.12.2       mjf 	}
    384  1.24.12.2       mjf }
    385  1.24.12.2       mjf 
    386  1.24.12.2       mjf /*
    387  1.24.12.2       mjf  * Soft interrupt handler: sends signal to async proc.
    388  1.24.12.2       mjf  */
    389  1.24.12.2       mjf static void
    390  1.24.12.2       mjf wsevent_intr(void *cookie)
    391  1.24.12.2       mjf {
    392  1.24.12.2       mjf 	struct wseventvar *ev;
    393  1.24.12.2       mjf 
    394  1.24.12.2       mjf 	ev = cookie;
    395  1.24.12.2       mjf 
    396  1.24.12.2       mjf 	if (ev->async) {
    397  1.24.12.2       mjf 		mutex_enter(proc_lock);
    398       1.18      jmmv 		psignal(ev->io, SIGIO);
    399  1.24.12.2       mjf 		mutex_exit(proc_lock);
    400       1.23        ad 	}
    401       1.18      jmmv }
    402       1.18      jmmv 
    403       1.18      jmmv /*
    404       1.20  uebayasi  * Injects the set of events given in 'events', whose size is 'nevents',
    405       1.18      jmmv  * into the 'ev' queue.  If there is not enough free space to inject them
    406       1.18      jmmv  * all, returns ENOSPC and the queue is left intact; otherwise returns 0
    407       1.18      jmmv  * and wakes up all listeners.
    408       1.18      jmmv  */
    409       1.18      jmmv int
    410       1.18      jmmv wsevent_inject(struct wseventvar *ev, struct wscons_event *events,
    411       1.18      jmmv     size_t nevents)
    412       1.18      jmmv {
    413       1.18      jmmv 	size_t avail, i;
    414       1.18      jmmv 	struct timespec t;
    415       1.18      jmmv 
    416       1.18      jmmv 	/* Calculate number of free slots in the queue. */
    417       1.18      jmmv 	if (ev->put < ev->get)
    418       1.18      jmmv 		avail = ev->get - ev->put;
    419       1.18      jmmv 	else
    420       1.18      jmmv 		avail = WSEVENT_QSIZE - (ev->put - ev->get);
    421       1.18      jmmv 	KASSERT(avail <= WSEVENT_QSIZE);
    422       1.18      jmmv 
    423       1.18      jmmv 	/* Fail if there is all events will not fit in the queue. */
    424       1.18      jmmv 	if (avail < nevents)
    425       1.18      jmmv 		return ENOSPC;
    426       1.18      jmmv 
    427       1.18      jmmv 	/* Use the current time for all events. */
    428       1.19    kardel 	getnanotime(&t);
    429       1.18      jmmv 
    430       1.18      jmmv 	/* Inject the events. */
    431       1.18      jmmv 	for (i = 0; i < nevents; i++) {
    432       1.18      jmmv 		struct wscons_event *we;
    433       1.18      jmmv 
    434  1.24.12.3       mjf 		we = EVARRAY(ev, ev->put);
    435       1.18      jmmv 		we->type = events[i].type;
    436       1.18      jmmv 		we->value = events[i].value;
    437       1.18      jmmv 		we->time = t;
    438       1.18      jmmv 
    439       1.18      jmmv 		ev->put = (ev->put + 1) % WSEVENT_QSIZE;
    440       1.18      jmmv 	}
    441       1.18      jmmv 	wsevent_wakeup(ev);
    442       1.18      jmmv 
    443       1.18      jmmv 	return 0;
    444       1.18      jmmv }
    445  1.24.12.3       mjf 
    446  1.24.12.3       mjf int
    447  1.24.12.3       mjf wsevent_setversion(struct wseventvar *ev, int vers)
    448  1.24.12.3       mjf {
    449  1.24.12.3       mjf 	if (ev == NULL)
    450  1.24.12.3       mjf 		return EINVAL;
    451  1.24.12.3       mjf 
    452  1.24.12.3       mjf 	switch (vers) {
    453  1.24.12.3       mjf 	case 0:
    454  1.24.12.3       mjf 	case WSEVENT_VERSION:
    455  1.24.12.3       mjf 		break;
    456  1.24.12.3       mjf 	default:
    457  1.24.12.3       mjf 		return EINVAL;
    458  1.24.12.3       mjf 	}
    459  1.24.12.3       mjf 
    460  1.24.12.3       mjf 	if (vers == ev->version)
    461  1.24.12.3       mjf 		return 0;
    462  1.24.12.3       mjf 
    463  1.24.12.3       mjf 	ev->get = ev->put = 0;
    464  1.24.12.3       mjf 	ev->version = vers;
    465  1.24.12.3       mjf 	return 0;
    466  1.24.12.3       mjf }
    467