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