wsevent.c revision 1.24.12.2 1 /* $NetBSD: wsevent.c,v 1.24.12.2 2008/06/02 13:23:57 mjf Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by Christopher G. Demetriou
46 * for the NetBSD Project.
47 * 4. The name of the author may not be used to endorse or promote products
48 * derived from this software without specific prior written permission
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 */
61
62 /*
63 * Copyright (c) 1992, 1993
64 * The Regents of the University of California. All rights reserved.
65 *
66 * This software was developed by the Computer Systems Engineering group
67 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
68 * contributed to Berkeley.
69 *
70 * All advertising materials mentioning features or use of this software
71 * must display the following acknowledgement:
72 * This product includes software developed by the University of
73 * California, Lawrence Berkeley Laboratory.
74 *
75 * Redistribution and use in source and binary forms, with or without
76 * modification, are permitted provided that the following conditions
77 * are met:
78 * 1. Redistributions of source code must retain the above copyright
79 * notice, this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright
81 * notice, this list of conditions and the following disclaimer in the
82 * documentation and/or other materials provided with the distribution.
83 * 3. Neither the name of the University nor the names of its contributors
84 * may be used to endorse or promote products derived from this software
85 * without specific prior written permission.
86 *
87 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
88 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
91 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
93 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
94 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
95 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
96 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
97 * SUCH DAMAGE.
98 *
99 * @(#)event.c 8.1 (Berkeley) 6/11/93
100 */
101
102 /*
103 * Internal "wscons_event" queue interface for the keyboard and mouse drivers.
104 */
105
106 #include <sys/cdefs.h>
107 __KERNEL_RCSID(0, "$NetBSD: wsevent.c,v 1.24.12.2 2008/06/02 13:23:57 mjf Exp $");
108
109 #include <sys/param.h>
110 #include <sys/kernel.h>
111 #include <sys/fcntl.h>
112 #include <sys/malloc.h>
113 #include <sys/proc.h>
114 #include <sys/systm.h>
115 #include <sys/vnode.h>
116 #include <sys/select.h>
117 #include <sys/poll.h>
118
119 #include <dev/wscons/wsconsio.h>
120 #include <dev/wscons/wseventvar.h>
121
122 /*
123 * Size of a wsevent queue (measured in number of events).
124 * Should be a power of two so that `%' is fast.
125 * At the moment, the value below makes the queues use 2 Kbytes each; this
126 * value may need tuning.
127 */
128 #define WSEVENT_QSIZE 256
129
130 /*
131 * Priority of code managing wsevent queues. PWSEVENT is set just above
132 * PSOCK, which is just above TTIPRI, on the theory that mouse and keyboard
133 * `user' input should be quick.
134 */
135 #define PWSEVENT 23
136 #define splwsevent() spltty()
137
138 static void wsevent_intr(void *);
139
140 /*
141 * Initialize a wscons_event queue.
142 */
143 void
144 wsevent_init(struct wseventvar *ev, struct proc *p)
145 {
146
147 if (ev->q != NULL) {
148 #ifdef DIAGNOSTIC
149 printf("wsevent_init: already init\n");
150 #endif
151 return;
152 }
153 ev->get = ev->put = 0;
154 ev->q = malloc((u_long)WSEVENT_QSIZE * sizeof(struct wscons_event),
155 M_DEVBUF, M_WAITOK|M_ZERO);
156 selinit(&ev->sel);
157 ev->io = p;
158 ev->sih = softint_establish(SOFTINT_MPSAFE | SOFTINT_CLOCK,
159 wsevent_intr, ev);
160 }
161
162 /*
163 * Tear down a wscons_event queue.
164 */
165 void
166 wsevent_fini(struct wseventvar *ev)
167 {
168 if (ev->q == NULL) {
169 #ifdef DIAGNOSTIC
170 printf("wsevent_fini: already fini\n");
171 #endif
172 return;
173 }
174 seldestroy(&ev->sel);
175 free(ev->q, M_DEVBUF);
176 ev->q = NULL;
177 softint_disestablish(ev->sih);
178 }
179
180 /*
181 * User-level interface: read, poll.
182 * (User cannot write an event queue.)
183 */
184 int
185 wsevent_read(struct wseventvar *ev, struct uio *uio, int flags)
186 {
187 int s, n, cnt, error;
188
189 /*
190 * Make sure we can return at least 1.
191 */
192 if (uio->uio_resid < sizeof(struct wscons_event))
193 return (EMSGSIZE); /* ??? */
194 s = splwsevent();
195 while (ev->get == ev->put) {
196 if (flags & IO_NDELAY) {
197 splx(s);
198 return (EWOULDBLOCK);
199 }
200 ev->wanted = 1;
201 error = tsleep(ev, PWSEVENT | PCATCH,
202 "wsevent_read", 0);
203 if (error) {
204 splx(s);
205 return (error);
206 }
207 }
208 /*
209 * Move wscons_event from tail end of queue (there is at least one
210 * there).
211 */
212 if (ev->put < ev->get)
213 cnt = WSEVENT_QSIZE - ev->get; /* events in [get..QSIZE) */
214 else
215 cnt = ev->put - ev->get; /* events in [get..put) */
216 splx(s);
217 n = howmany(uio->uio_resid, sizeof(struct wscons_event));
218 if (cnt > n)
219 cnt = n;
220 error = uiomove(&ev->q[ev->get],
221 cnt * sizeof(struct wscons_event), uio);
222 n -= cnt;
223 /*
224 * If we do not wrap to 0, used up all our space, or had an error,
225 * stop. Otherwise move from front of queue to put index, if there
226 * is anything there to move.
227 */
228 if ((ev->get = (ev->get + cnt) % WSEVENT_QSIZE) != 0 ||
229 n == 0 || error || (cnt = ev->put) == 0)
230 return (error);
231 if (cnt > n)
232 cnt = n;
233 error = uiomove(&ev->q[0],
234 cnt * sizeof(struct wscons_event), uio);
235 ev->get = cnt;
236 return (error);
237 }
238
239 int
240 wsevent_poll(struct wseventvar *ev, int events, struct lwp *l)
241 {
242 int revents = 0;
243 int s = splwsevent();
244
245 if (events & (POLLIN | POLLRDNORM)) {
246 if (ev->get != ev->put)
247 revents |= events & (POLLIN | POLLRDNORM);
248 else
249 selrecord(l, &ev->sel);
250 }
251
252 splx(s);
253 return (revents);
254 }
255
256 static void
257 filt_wseventrdetach(struct knote *kn)
258 {
259 struct wseventvar *ev = kn->kn_hook;
260 int s;
261
262 s = splwsevent();
263 SLIST_REMOVE(&ev->sel.sel_klist, kn, knote, kn_selnext);
264 splx(s);
265 }
266
267 static int
268 filt_wseventread(struct knote *kn, long hint)
269 {
270 struct wseventvar *ev = kn->kn_hook;
271
272 if (ev->get == ev->put)
273 return (0);
274
275 if (ev->get < ev->put)
276 kn->kn_data = ev->put - ev->get;
277 else
278 kn->kn_data = (WSEVENT_QSIZE - ev->get) +
279 ev->put;
280
281 kn->kn_data *= sizeof(struct wscons_event);
282
283 return (1);
284 }
285
286 static const struct filterops wsevent_filtops =
287 { 1, NULL, filt_wseventrdetach, filt_wseventread };
288
289 int
290 wsevent_kqfilter(struct wseventvar *ev, struct knote *kn)
291 {
292 struct klist *klist;
293 int s;
294
295 switch (kn->kn_filter) {
296 case EVFILT_READ:
297 klist = &ev->sel.sel_klist;
298 kn->kn_fop = &wsevent_filtops;
299 break;
300
301 default:
302 return (EINVAL);
303 }
304
305 kn->kn_hook = ev;
306
307 s = splwsevent();
308 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
309 splx(s);
310
311 return (0);
312 }
313
314 /*
315 * Wakes up all listener of the 'ev' queue.
316 */
317 void
318 wsevent_wakeup(struct wseventvar *ev)
319 {
320
321 selnotify(&ev->sel, 0, 0);
322
323 if (ev->wanted) {
324 ev->wanted = 0;
325 wakeup(ev);
326 }
327
328 if (ev->async) {
329 softint_schedule(ev->sih);
330 }
331 }
332
333 /*
334 * Soft interrupt handler: sends signal to async proc.
335 */
336 static void
337 wsevent_intr(void *cookie)
338 {
339 struct wseventvar *ev;
340
341 ev = cookie;
342
343 if (ev->async) {
344 mutex_enter(proc_lock);
345 psignal(ev->io, SIGIO);
346 mutex_exit(proc_lock);
347 }
348 }
349
350 /*
351 * Injects the set of events given in 'events', whose size is 'nevents',
352 * into the 'ev' queue. If there is not enough free space to inject them
353 * all, returns ENOSPC and the queue is left intact; otherwise returns 0
354 * and wakes up all listeners.
355 */
356 int
357 wsevent_inject(struct wseventvar *ev, struct wscons_event *events,
358 size_t nevents)
359 {
360 size_t avail, i;
361 struct timespec t;
362
363 /* Calculate number of free slots in the queue. */
364 if (ev->put < ev->get)
365 avail = ev->get - ev->put;
366 else
367 avail = WSEVENT_QSIZE - (ev->put - ev->get);
368 KASSERT(avail <= WSEVENT_QSIZE);
369
370 /* Fail if there is all events will not fit in the queue. */
371 if (avail < nevents)
372 return ENOSPC;
373
374 /* Use the current time for all events. */
375 getnanotime(&t);
376
377 /* Inject the events. */
378 for (i = 0; i < nevents; i++) {
379 struct wscons_event *we;
380
381 we = &ev->q[ev->put];
382 we->type = events[i].type;
383 we->value = events[i].value;
384 we->time = t;
385
386 ev->put = (ev->put + 1) % WSEVENT_QSIZE;
387 }
388
389 wsevent_wakeup(ev);
390
391 return 0;
392 }
393