1 /* $NetBSD: event.c,v 1.12 2007/03/04 06:01:05 christos 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 43 /* 44 * Internal `Firm_event' interface for the keyboard and mouse drivers. 45 */ 46 47 #include <sys/cdefs.h> 48 __KERNEL_RCSID(0, "$NetBSD: event.c,v 1.12 2007/03/04 06:01:05 christos Exp $"); 49 50 #include <sys/param.h> 51 #include <sys/fcntl.h> 52 #include <sys/malloc.h> 53 #include <sys/proc.h> 54 #include <sys/systm.h> 55 #include <sys/vnode.h> 56 #include <sys/select.h> 57 #include <sys/poll.h> 58 59 #include <machine/vuid_event.h> 60 #include <x68k/dev/event_var.h> 61 62 /* 63 * Initialize a firm_event queue. 64 */ 65 void 66 ev_init(struct evvar *ev) 67 { 68 69 ev->ev_get = ev->ev_put = 0; 70 ev->ev_q = malloc((u_long)EV_QSIZE * sizeof(struct firm_event), 71 M_DEVBUF, M_WAITOK); 72 memset((void *)ev->ev_q, 0, EV_QSIZE * sizeof(struct firm_event)); 73 } 74 75 /* 76 * Tear down a firm_event queue. 77 */ 78 void 79 ev_fini(struct evvar *ev) 80 { 81 82 free(ev->ev_q, M_DEVBUF); 83 } 84 85 /* 86 * User-level interface: read, select. 87 * (User cannot write an event queue.) 88 */ 89 int 90 ev_read(struct evvar *ev, struct uio *uio, int flags) 91 { 92 int s, n, cnt, error; 93 94 /* 95 * Make sure we can return at least 1. 96 */ 97 if (uio->uio_resid < sizeof(struct firm_event)) 98 return (EMSGSIZE); /* ??? */ 99 s = splev(); 100 while (ev->ev_get == ev->ev_put) { 101 if (flags & IO_NDELAY) { 102 splx(s); 103 return (EWOULDBLOCK); 104 } 105 ev->ev_wanted = 1; 106 error = tsleep((void *)ev, PEVENT | PCATCH, "firm_event", 0); 107 if (error) { 108 splx(s); 109 return (error); 110 } 111 } 112 /* 113 * Move firm_events from tail end of queue (there is at least one 114 * there). 115 */ 116 if (ev->ev_put < ev->ev_get) 117 cnt = EV_QSIZE - ev->ev_get; /* events in [get..QSIZE) */ 118 else 119 cnt = ev->ev_put - ev->ev_get; /* events in [get..put) */ 120 splx(s); 121 n = howmany(uio->uio_resid, sizeof(struct firm_event)); 122 if (cnt > n) 123 cnt = n; 124 error = uiomove((void *)&ev->ev_q[ev->ev_get], 125 cnt * sizeof(struct firm_event), uio); 126 n -= cnt; 127 /* 128 * If we do not wrap to 0, used up all our space, or had an error, 129 * stop. Otherwise move from front of queue to put index, if there 130 * is anything there to move. 131 */ 132 if ((ev->ev_get = (ev->ev_get + cnt) % EV_QSIZE) != 0 || 133 n == 0 || error || (cnt = ev->ev_put) == 0) 134 return (error); 135 if (cnt > n) 136 cnt = n; 137 error = uiomove((void *)&ev->ev_q[0], 138 cnt * sizeof(struct firm_event), uio); 139 ev->ev_get = cnt; 140 return (error); 141 } 142 143 int 144 ev_poll(struct evvar *ev, int events, struct lwp *l) 145 { 146 int s, revents = 0; 147 148 s = splev(); 149 if (events & (POLLIN | POLLRDNORM)) { 150 if (ev->ev_get == ev->ev_put) 151 selrecord(l, &ev->ev_sel); 152 else 153 revents |= events & (POLLIN | POLLRDNORM); 154 } 155 revents |= events & (POLLOUT | POLLWRNORM); 156 splx(s); 157 return (revents); 158 } 159 160 static void 161 filt_evrdetach(struct knote *kn) 162 { 163 struct evvar *ev = kn->kn_hook; 164 int s; 165 166 s = splev(); 167 SLIST_REMOVE(&ev->ev_sel.sel_klist, kn, knote, kn_selnext); 168 splx(s); 169 } 170 171 static int 172 filt_evread(struct knote *kn, long hint) 173 { 174 struct evvar *ev = kn->kn_hook; 175 176 if (ev->ev_get == ev->ev_put) 177 return (0); 178 179 if (ev->ev_get < ev->ev_put) 180 kn->kn_data = ev->ev_put - ev->ev_get; 181 else 182 kn->kn_data = (EV_QSIZE - ev->ev_get) + 183 ev->ev_put; 184 185 kn->kn_data *= sizeof(struct firm_event); 186 187 return (1); 188 } 189 190 static const struct filterops ev_filtops = 191 { 1, NULL, filt_evrdetach, filt_evread }; 192 193 int 194 ev_kqfilter(struct evvar *ev, struct knote *kn) 195 { 196 struct klist *klist; 197 int s; 198 199 switch (kn->kn_filter) { 200 case EVFILT_READ: 201 klist = &ev->ev_sel.sel_klist; 202 kn->kn_fop = &ev_filtops; 203 break; 204 205 default: 206 return (1); 207 } 208 209 kn->kn_hook = ev; 210 211 s = splev(); 212 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 213 splx(s); 214 215 return (0); 216 } 217