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