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