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