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