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