Home | History | Annotate | Line # | Download | only in wscons
wsevent.c revision 1.27.8.1
      1 /* $NetBSD: wsevent.c,v 1.27.8.1 2009/01/19 13:19:18 skrll 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.27.8.1 2009/01/19 13:19:18 skrll Exp $");
    108 
    109 #include "opt_compat_netbsd.h"
    110 
    111 #include <sys/param.h>
    112 #include <sys/kernel.h>
    113 #include <sys/fcntl.h>
    114 #include <sys/kmem.h>
    115 #include <sys/proc.h>
    116 #include <sys/systm.h>
    117 #include <sys/vnode.h>
    118 #include <sys/select.h>
    119 #include <sys/poll.h>
    120 
    121 #include <dev/wscons/wsconsio.h>
    122 #include <dev/wscons/wseventvar.h>
    123 
    124 /*
    125  * Size of a wsevent queue (measured in number of events).
    126  * Should be a power of two so that `%' is fast.
    127  * At the moment, the value below makes the queues use 2 Kbytes each; this
    128  * value may need tuning.
    129  */
    130 #define	WSEVENT_QSIZE	256
    131 
    132 #define EVSIZE(ver)	((ver) == WSEVENT_VERSION ? \
    133     sizeof(struct wscons_event) : \
    134     sizeof(struct owscons_event))
    135 #define EVARRAY(ev, idx) (&(ev)->q[(idx)])
    136 
    137 /*
    138  * Priority of code managing wsevent queues.  PWSEVENT is set just above
    139  * PSOCK, which is just above TTIPRI, on the theory that mouse and keyboard
    140  * `user' input should be quick.
    141  */
    142 #define	PWSEVENT	23
    143 #define	splwsevent()	spltty()
    144 
    145 static void	wsevent_intr(void *);
    146 
    147 /*
    148  * Initialize a wscons_event queue.
    149  */
    150 void
    151 wsevent_init(struct wseventvar *ev, struct proc *p)
    152 {
    153 
    154 	if (ev->q != NULL) {
    155 #ifdef DIAGNOSTIC
    156 		printf("wsevent_init: already init\n");
    157 #endif
    158 		return;
    159 	}
    160 	ev->version = 0;
    161 	ev->get = ev->put = 0;
    162 	ev->q = kmem_alloc(WSEVENT_QSIZE * sizeof(*ev->q), KM_SLEEP);
    163 	selinit(&ev->sel);
    164 	ev->io = p;
    165 	ev->sih = softint_establish(SOFTINT_MPSAFE | SOFTINT_CLOCK,
    166 	    wsevent_intr, ev);
    167 }
    168 
    169 /*
    170  * Tear down a wscons_event queue.
    171  */
    172 void
    173 wsevent_fini(struct wseventvar *ev)
    174 {
    175 	if (ev->q == NULL) {
    176 #ifdef DIAGNOSTIC
    177 		printf("wsevent_fini: already fini\n");
    178 #endif
    179 		return;
    180 	}
    181 	seldestroy(&ev->sel);
    182 	kmem_free(ev->q, WSEVENT_QSIZE * sizeof(*ev->q));
    183 	ev->q = NULL;
    184 	softint_disestablish(ev->sih);
    185 }
    186 
    187 #if defined(COMPAT_50) || defined(MODULAR)
    188 static int
    189 wsevent_copyout_events50(const struct wscons_event *events, int cnt,
    190     struct uio *uio)
    191 {
    192 	int i;
    193 
    194 	for (i = 0; i < cnt; i++) {
    195 		const struct wscons_event *ev = &events[i];
    196 		struct owscons_event ev50;
    197 		int error;
    198 
    199 		ev50.type = ev->type;
    200 		ev50.value = ev->value;
    201 		timespec_to_timespec50(&ev->time, &ev50.time);
    202 
    203 		error = uiomove(&ev50, sizeof(ev50), uio);
    204 		if (error) {
    205 			return error;
    206 		}
    207 	}
    208 	return 0;
    209 }
    210 #else /* defined(COMPAT_50) || defined(MODULAR) */
    211 static int
    212 wsevent_copyout_events50(const struct wscons_event *events, int cnt,
    213     struct uio *uio)
    214 {
    215 
    216 	panic("%s: unexpected version", __func__);
    217 }
    218 #endif /* defined(COMPAT_50) || defined(MODULAR) */
    219 
    220 static int
    221 wsevent_copyout_events(const struct wscons_event *events, int cnt,
    222     struct uio *uio, int ver)
    223 {
    224 
    225 	switch (ver) {
    226 	case 0:
    227 		return wsevent_copyout_events50(events, cnt, uio);
    228 	case WSEVENT_VERSION:
    229 		return uiomove(__UNCONST(events), cnt * sizeof(*events), uio);
    230 	default:
    231 		panic("%s: unknown version %d", __func__, ver);
    232 	}
    233 }
    234 
    235 /*
    236  * User-level interface: read, poll.
    237  * (User cannot write an event queue.)
    238  */
    239 int
    240 wsevent_read(struct wseventvar *ev, struct uio *uio, int flags)
    241 {
    242 	int s, n, cnt, error;
    243 	const int ver = ev->version;
    244 	const size_t evsize = EVSIZE(ver);
    245 
    246 	/*
    247 	 * Make sure we can return at least 1.
    248 	 */
    249 	if (uio->uio_resid < evsize)
    250 		return (EMSGSIZE);	/* ??? */
    251 	s = splwsevent();
    252 	while (ev->get == ev->put) {
    253 		if (flags & IO_NDELAY) {
    254 			splx(s);
    255 			return (EWOULDBLOCK);
    256 		}
    257 		ev->wanted = 1;
    258 		error = tsleep(ev, PWSEVENT | PCATCH, "wsevent_read", 0);
    259 		if (error) {
    260 			splx(s);
    261 			return (error);
    262 		}
    263 	}
    264 	/*
    265 	 * Move wscons_event from tail end of queue (there is at least one
    266 	 * there).
    267 	 */
    268 	if (ev->put < ev->get)
    269 		cnt = WSEVENT_QSIZE - ev->get;	/* events in [get..QSIZE) */
    270 	else
    271 		cnt = ev->put - ev->get;	/* events in [get..put) */
    272 	splx(s);
    273 	n = howmany(uio->uio_resid, evsize);
    274 	if (cnt > n)
    275 		cnt = n;
    276 	error = wsevent_copyout_events(EVARRAY(ev, ev->get), cnt, uio, ver);
    277 	n -= cnt;
    278 	/*
    279 	 * If we do not wrap to 0, used up all our space, or had an error,
    280 	 * stop.  Otherwise move from front of queue to put index, if there
    281 	 * is anything there to move.
    282 	 */
    283 	if ((ev->get = (ev->get + cnt) % WSEVENT_QSIZE) != 0 ||
    284 	    n == 0 || error || (cnt = ev->put) == 0)
    285 		return (error);
    286 	if (cnt > n)
    287 		cnt = n;
    288 	error = wsevent_copyout_events(EVARRAY(ev, 0), cnt, uio, ver);
    289 	ev->get = cnt;
    290 	return (error);
    291 }
    292 
    293 int
    294 wsevent_poll(struct wseventvar *ev, int events, struct lwp *l)
    295 {
    296 	int revents = 0;
    297 	int s = splwsevent();
    298 
    299         if (events & (POLLIN | POLLRDNORM)) {
    300 		if (ev->get != ev->put)
    301 			revents |= events & (POLLIN | POLLRDNORM);
    302 		else
    303 			selrecord(l, &ev->sel);
    304 	}
    305 
    306 	splx(s);
    307 	return (revents);
    308 }
    309 
    310 static void
    311 filt_wseventrdetach(struct knote *kn)
    312 {
    313 	struct wseventvar *ev = kn->kn_hook;
    314 	int s;
    315 
    316 	s = splwsevent();
    317 	SLIST_REMOVE(&ev->sel.sel_klist, kn, knote, kn_selnext);
    318 	splx(s);
    319 }
    320 
    321 static int
    322 filt_wseventread(struct knote *kn, long hint)
    323 {
    324 	struct wseventvar *ev = kn->kn_hook;
    325 
    326 	if (ev->get == ev->put)
    327 		return (0);
    328 
    329 	if (ev->get < ev->put)
    330 		kn->kn_data = ev->put - ev->get;
    331 	else
    332 		kn->kn_data = (WSEVENT_QSIZE - ev->get) + ev->put;
    333 
    334 	kn->kn_data *= EVSIZE(ev->version);
    335 
    336 	return (1);
    337 }
    338 
    339 static const struct filterops wsevent_filtops =
    340 	{ 1, NULL, filt_wseventrdetach, filt_wseventread };
    341 
    342 int
    343 wsevent_kqfilter(struct wseventvar *ev, struct knote *kn)
    344 {
    345 	struct klist *klist;
    346 	int s;
    347 
    348 	switch (kn->kn_filter) {
    349 	case EVFILT_READ:
    350 		klist = &ev->sel.sel_klist;
    351 		kn->kn_fop = &wsevent_filtops;
    352 		break;
    353 
    354 	default:
    355 		return (EINVAL);
    356 	}
    357 
    358 	kn->kn_hook = ev;
    359 
    360 	s = splwsevent();
    361 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    362 	splx(s);
    363 
    364 	return (0);
    365 }
    366 
    367 /*
    368  * Wakes up all listener of the 'ev' queue.
    369  */
    370 void
    371 wsevent_wakeup(struct wseventvar *ev)
    372 {
    373 
    374 	selnotify(&ev->sel, 0, 0);
    375 
    376 	if (ev->wanted) {
    377 		ev->wanted = 0;
    378 		wakeup(ev);
    379 	}
    380 
    381 	if (ev->async) {
    382 		softint_schedule(ev->sih);
    383 	}
    384 }
    385 
    386 /*
    387  * Soft interrupt handler: sends signal to async proc.
    388  */
    389 static void
    390 wsevent_intr(void *cookie)
    391 {
    392 	struct wseventvar *ev;
    393 
    394 	ev = cookie;
    395 
    396 	if (ev->async) {
    397 		mutex_enter(proc_lock);
    398 		psignal(ev->io, SIGIO);
    399 		mutex_exit(proc_lock);
    400 	}
    401 }
    402 
    403 /*
    404  * Injects the set of events given in 'events', whose size is 'nevents',
    405  * into the 'ev' queue.  If there is not enough free space to inject them
    406  * all, returns ENOSPC and the queue is left intact; otherwise returns 0
    407  * and wakes up all listeners.
    408  */
    409 int
    410 wsevent_inject(struct wseventvar *ev, struct wscons_event *events,
    411     size_t nevents)
    412 {
    413 	size_t avail, i;
    414 	struct timespec t;
    415 
    416 	/* Calculate number of free slots in the queue. */
    417 	if (ev->put < ev->get)
    418 		avail = ev->get - ev->put;
    419 	else
    420 		avail = WSEVENT_QSIZE - (ev->put - ev->get);
    421 	KASSERT(avail <= WSEVENT_QSIZE);
    422 
    423 	/* Fail if there is all events will not fit in the queue. */
    424 	if (avail < nevents)
    425 		return ENOSPC;
    426 
    427 	/* Use the current time for all events. */
    428 	getnanotime(&t);
    429 
    430 	/* Inject the events. */
    431 	for (i = 0; i < nevents; i++) {
    432 		struct wscons_event *we;
    433 
    434 		we = EVARRAY(ev, ev->put);
    435 		we->type = events[i].type;
    436 		we->value = events[i].value;
    437 		we->time = t;
    438 
    439 		ev->put = (ev->put + 1) % WSEVENT_QSIZE;
    440 	}
    441 	wsevent_wakeup(ev);
    442 
    443 	return 0;
    444 }
    445 
    446 int
    447 wsevent_setversion(struct wseventvar *ev, int vers)
    448 {
    449 	if (ev == NULL)
    450 		return EINVAL;
    451 
    452 	switch (vers) {
    453 	case 0:
    454 	case WSEVENT_VERSION:
    455 		break;
    456 	default:
    457 		return EINVAL;
    458 	}
    459 
    460 	if (vers == ev->version)
    461 		return 0;
    462 
    463 	ev->get = ev->put = 0;
    464 	ev->version = vers;
    465 	return 0;
    466 }
    467