Home | History | Annotate | Line # | Download | only in wscons
wsevent.c revision 1.5.6.5
      1  1.5.6.4   nathanw /* $NetBSD: wsevent.c,v 1.5.6.5 2002/12/11 06:38:56 thorpej Exp $ */
      2      1.1  drochner 
      3      1.1  drochner /*
      4      1.1  drochner  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
      5      1.1  drochner  *
      6      1.1  drochner  * Redistribution and use in source and binary forms, with or without
      7      1.1  drochner  * modification, are permitted provided that the following conditions
      8      1.1  drochner  * are met:
      9      1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     10      1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     11      1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     13      1.1  drochner  *    documentation and/or other materials provided with the distribution.
     14      1.1  drochner  * 3. All advertising materials mentioning features or use of this software
     15      1.1  drochner  *    must display the following acknowledgement:
     16      1.1  drochner  *      This product includes software developed by Christopher G. Demetriou
     17      1.1  drochner  *	for the NetBSD Project.
     18      1.1  drochner  * 4. The name of the author may not be used to endorse or promote products
     19      1.1  drochner  *    derived from this software without specific prior written permission
     20      1.1  drochner  *
     21      1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.1  drochner  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23      1.1  drochner  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24      1.1  drochner  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.1  drochner  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26      1.1  drochner  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27      1.1  drochner  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28      1.1  drochner  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29      1.1  drochner  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30      1.1  drochner  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31      1.1  drochner  */
     32      1.1  drochner 
     33      1.1  drochner /*
     34      1.1  drochner  * Copyright (c) 1992, 1993
     35      1.1  drochner  *	The Regents of the University of California.  All rights reserved.
     36      1.1  drochner  *
     37      1.1  drochner  * This software was developed by the Computer Systems Engineering group
     38      1.1  drochner  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     39      1.1  drochner  * contributed to Berkeley.
     40      1.1  drochner  *
     41      1.1  drochner  * All advertising materials mentioning features or use of this software
     42      1.1  drochner  * must display the following acknowledgement:
     43      1.1  drochner  *	This product includes software developed by the University of
     44      1.1  drochner  *	California, Lawrence Berkeley Laboratory.
     45      1.1  drochner  *
     46      1.1  drochner  * Redistribution and use in source and binary forms, with or without
     47      1.1  drochner  * modification, are permitted provided that the following conditions
     48      1.1  drochner  * are met:
     49      1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     50      1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     51      1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     52      1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     53      1.1  drochner  *    documentation and/or other materials provided with the distribution.
     54      1.1  drochner  * 3. All advertising materials mentioning features or use of this software
     55      1.1  drochner  *    must display the following acknowledgement:
     56      1.1  drochner  *	This product includes software developed by the University of
     57      1.1  drochner  *	California, Berkeley and its contributors.
     58      1.1  drochner  * 4. Neither the name of the University nor the names of its contributors
     59      1.1  drochner  *    may be used to endorse or promote products derived from this software
     60      1.1  drochner  *    without specific prior written permission.
     61      1.1  drochner  *
     62      1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63      1.1  drochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64      1.1  drochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65      1.1  drochner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66      1.1  drochner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67      1.1  drochner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68      1.1  drochner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69      1.1  drochner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70      1.1  drochner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71      1.1  drochner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72      1.1  drochner  * SUCH DAMAGE.
     73      1.1  drochner  *
     74      1.1  drochner  *	@(#)event.c	8.1 (Berkeley) 6/11/93
     75      1.1  drochner  */
     76      1.1  drochner 
     77      1.1  drochner /*
     78      1.1  drochner  * Internal "wscons_event" queue interface for the keyboard and mouse drivers.
     79      1.1  drochner  */
     80      1.1  drochner 
     81  1.5.6.2   nathanw #include <sys/cdefs.h>
     82  1.5.6.4   nathanw __KERNEL_RCSID(0, "$NetBSD: wsevent.c,v 1.5.6.5 2002/12/11 06:38:56 thorpej Exp $");
     83  1.5.6.2   nathanw 
     84      1.1  drochner #include <sys/param.h>
     85      1.1  drochner #include <sys/fcntl.h>
     86      1.1  drochner #include <sys/malloc.h>
     87      1.1  drochner #include <sys/proc.h>
     88      1.1  drochner #include <sys/systm.h>
     89      1.1  drochner #include <sys/vnode.h>
     90      1.1  drochner #include <sys/select.h>
     91      1.1  drochner #include <sys/poll.h>
     92      1.1  drochner 
     93      1.1  drochner #include <dev/wscons/wsconsio.h>
     94      1.1  drochner #include <dev/wscons/wseventvar.h>
     95      1.1  drochner 
     96      1.1  drochner /*
     97      1.1  drochner  * Initialize a wscons_event queue.
     98      1.1  drochner  */
     99      1.1  drochner void
    100  1.5.6.1   nathanw wsevent_init(struct wseventvar *ev)
    101      1.1  drochner {
    102      1.1  drochner 
    103  1.5.6.2   nathanw #ifdef DIAGNOSTIC
    104  1.5.6.2   nathanw 	if (ev->q != NULL) {
    105  1.5.6.2   nathanw 		printf("wsevent_init: already init\n");
    106  1.5.6.2   nathanw 		return;
    107  1.5.6.2   nathanw 	}
    108  1.5.6.2   nathanw #endif
    109      1.1  drochner 	ev->get = ev->put = 0;
    110      1.1  drochner 	ev->q = malloc((u_long)WSEVENT_QSIZE * sizeof(struct wscons_event),
    111  1.5.6.3   nathanw 		       M_DEVBUF, M_WAITOK|M_ZERO);
    112      1.1  drochner }
    113      1.1  drochner 
    114      1.1  drochner /*
    115      1.1  drochner  * Tear down a wscons_event queue.
    116      1.1  drochner  */
    117      1.1  drochner void
    118  1.5.6.1   nathanw wsevent_fini(struct wseventvar *ev)
    119      1.1  drochner {
    120      1.1  drochner 
    121      1.1  drochner 	free(ev->q, M_DEVBUF);
    122  1.5.6.2   nathanw 	ev->q = NULL;
    123      1.1  drochner }
    124      1.1  drochner 
    125      1.1  drochner /*
    126      1.1  drochner  * User-level interface: read, poll.
    127      1.1  drochner  * (User cannot write an event queue.)
    128      1.1  drochner  */
    129      1.1  drochner int
    130  1.5.6.1   nathanw wsevent_read(struct wseventvar *ev, struct uio *uio, int flags)
    131      1.1  drochner {
    132      1.1  drochner 	int s, n, cnt, error;
    133      1.1  drochner 
    134      1.1  drochner 	/*
    135      1.1  drochner 	 * Make sure we can return at least 1.
    136      1.1  drochner 	 */
    137      1.1  drochner 	if (uio->uio_resid < sizeof(struct wscons_event))
    138      1.1  drochner 		return (EMSGSIZE);	/* ??? */
    139      1.1  drochner 	s = splwsevent();
    140      1.1  drochner 	while (ev->get == ev->put) {
    141      1.1  drochner 		if (flags & IO_NDELAY) {
    142      1.1  drochner 			splx(s);
    143      1.1  drochner 			return (EWOULDBLOCK);
    144      1.1  drochner 		}
    145      1.1  drochner 		ev->wanted = 1;
    146  1.5.6.2   nathanw 		error = tsleep(ev, PWSEVENT | PCATCH,
    147      1.1  drochner 		    "wsevent_read", 0);
    148      1.1  drochner 		if (error) {
    149      1.1  drochner 			splx(s);
    150      1.1  drochner 			return (error);
    151      1.1  drochner 		}
    152      1.1  drochner 	}
    153      1.1  drochner 	/*
    154      1.1  drochner 	 * Move wscons_event from tail end of queue (there is at least one
    155      1.1  drochner 	 * there).
    156      1.1  drochner 	 */
    157      1.1  drochner 	if (ev->put < ev->get)
    158      1.1  drochner 		cnt = WSEVENT_QSIZE - ev->get;	/* events in [get..QSIZE) */
    159      1.1  drochner 	else
    160      1.1  drochner 		cnt = ev->put - ev->get;	/* events in [get..put) */
    161      1.1  drochner 	splx(s);
    162      1.1  drochner 	n = howmany(uio->uio_resid, sizeof(struct wscons_event));
    163      1.1  drochner 	if (cnt > n)
    164      1.1  drochner 		cnt = n;
    165  1.5.6.2   nathanw 	error = uiomove(&ev->q[ev->get],
    166      1.1  drochner 	    cnt * sizeof(struct wscons_event), uio);
    167      1.1  drochner 	n -= cnt;
    168      1.1  drochner 	/*
    169      1.1  drochner 	 * If we do not wrap to 0, used up all our space, or had an error,
    170      1.1  drochner 	 * stop.  Otherwise move from front of queue to put index, if there
    171      1.1  drochner 	 * is anything there to move.
    172      1.1  drochner 	 */
    173      1.1  drochner 	if ((ev->get = (ev->get + cnt) % WSEVENT_QSIZE) != 0 ||
    174      1.1  drochner 	    n == 0 || error || (cnt = ev->put) == 0)
    175      1.1  drochner 		return (error);
    176      1.1  drochner 	if (cnt > n)
    177      1.1  drochner 		cnt = n;
    178  1.5.6.2   nathanw 	error = uiomove(&ev->q[0],
    179      1.1  drochner 	    cnt * sizeof(struct wscons_event), uio);
    180      1.1  drochner 	ev->get = cnt;
    181      1.1  drochner 	return (error);
    182      1.1  drochner }
    183      1.1  drochner 
    184      1.1  drochner int
    185  1.5.6.1   nathanw wsevent_poll(struct wseventvar *ev, int events, struct proc *p)
    186      1.1  drochner {
    187      1.1  drochner 	int revents = 0;
    188      1.1  drochner 	int s = splwsevent();
    189      1.1  drochner 
    190      1.2   thorpej         if (events & (POLLIN | POLLRDNORM)) {
    191      1.1  drochner 		if (ev->get != ev->put)
    192      1.2   thorpej 			revents |= events & (POLLIN | POLLRDNORM);
    193      1.2   thorpej 		else
    194      1.2   thorpej 			selrecord(p, &ev->sel);
    195      1.2   thorpej 	}
    196      1.1  drochner 
    197      1.1  drochner 	splx(s);
    198      1.1  drochner 	return (revents);
    199  1.5.6.4   nathanw }
    200  1.5.6.4   nathanw 
    201  1.5.6.4   nathanw static void
    202  1.5.6.4   nathanw filt_wseventrdetach(struct knote *kn)
    203  1.5.6.4   nathanw {
    204  1.5.6.4   nathanw 	struct wseventvar *ev = kn->kn_hook;
    205  1.5.6.4   nathanw 	int s;
    206  1.5.6.4   nathanw 
    207  1.5.6.4   nathanw 	s = splwsevent();
    208  1.5.6.5   thorpej 	SLIST_REMOVE(&ev->sel.sel_klist, kn, knote, kn_selnext);
    209  1.5.6.4   nathanw 	splx(s);
    210  1.5.6.4   nathanw }
    211  1.5.6.4   nathanw 
    212  1.5.6.4   nathanw static int
    213  1.5.6.4   nathanw filt_wseventread(struct knote *kn, long hint)
    214  1.5.6.4   nathanw {
    215  1.5.6.4   nathanw 	struct wseventvar *ev = kn->kn_hook;
    216  1.5.6.4   nathanw 
    217  1.5.6.4   nathanw 	if (ev->get == ev->put)
    218  1.5.6.4   nathanw 		return (0);
    219  1.5.6.4   nathanw 
    220  1.5.6.4   nathanw 	if (ev->get < ev->put)
    221  1.5.6.4   nathanw 		kn->kn_data = ev->put - ev->get;
    222  1.5.6.4   nathanw 	else
    223  1.5.6.4   nathanw 		kn->kn_data = (WSEVENT_QSIZE - ev->get) +
    224  1.5.6.4   nathanw 		    ev->put;
    225  1.5.6.4   nathanw 
    226  1.5.6.4   nathanw 	kn->kn_data *= sizeof(struct wscons_event);
    227  1.5.6.4   nathanw 
    228  1.5.6.4   nathanw 	return (1);
    229  1.5.6.4   nathanw }
    230  1.5.6.4   nathanw 
    231  1.5.6.4   nathanw static const struct filterops wsevent_filtops =
    232  1.5.6.4   nathanw 	{ 1, NULL, filt_wseventrdetach, filt_wseventread };
    233  1.5.6.4   nathanw 
    234  1.5.6.4   nathanw int
    235  1.5.6.4   nathanw wsevent_kqfilter(struct wseventvar *ev, struct knote *kn)
    236  1.5.6.4   nathanw {
    237  1.5.6.4   nathanw 	struct klist *klist;
    238  1.5.6.4   nathanw 	int s;
    239  1.5.6.4   nathanw 
    240  1.5.6.4   nathanw 	switch (kn->kn_filter) {
    241  1.5.6.4   nathanw 	case EVFILT_READ:
    242  1.5.6.5   thorpej 		klist = &ev->sel.sel_klist;
    243  1.5.6.4   nathanw 		kn->kn_fop = &wsevent_filtops;
    244  1.5.6.4   nathanw 		break;
    245  1.5.6.4   nathanw 
    246  1.5.6.4   nathanw 	default:
    247  1.5.6.4   nathanw 		return (1);
    248  1.5.6.4   nathanw 	}
    249  1.5.6.4   nathanw 
    250  1.5.6.4   nathanw 	kn->kn_hook = ev;
    251  1.5.6.4   nathanw 
    252  1.5.6.4   nathanw 	s = splwsevent();
    253  1.5.6.4   nathanw 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    254  1.5.6.4   nathanw 	splx(s);
    255  1.5.6.4   nathanw 
    256  1.5.6.4   nathanw 	return (0);
    257      1.1  drochner }
    258