wsevent.c revision 1.30 1 /* $NetBSD: wsevent.c,v 1.30 2009/01/15 04:22:11 yamt 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.30 2009/01/15 04:22:11 yamt Exp $");
108
109 #include "opt_compat_netbsd.h"
110
111 #include <sys/param.h>
112 #include <sys/kernel.h>
113 #include <sys/fcntl.h>
114 #include <sys/malloc.h>
115 #include <sys/proc.h>
116 #include <sys/systm.h>
117 #include <sys/vnode.h>
118 #include <sys/select.h>
119 #include <sys/poll.h>
120
121 #include <dev/wscons/wsconsio.h>
122 #include <dev/wscons/wseventvar.h>
123
124 /*
125 * Size of a wsevent queue (measured in number of events).
126 * Should be a power of two so that `%' is fast.
127 * At the moment, the value below makes the queues use 2 Kbytes each; this
128 * value may need tuning.
129 */
130 #define WSEVENT_QSIZE 256
131
132 /*
133 * Priority of code managing wsevent queues. PWSEVENT is set just above
134 * PSOCK, which is just above TTIPRI, on the theory that mouse and keyboard
135 * `user' input should be quick.
136 */
137 #define PWSEVENT 23
138 #define splwsevent() spltty()
139
140 static void wsevent_intr(void *);
141
142 /*
143 * Initialize a wscons_event queue.
144 */
145 void
146 wsevent_init(struct wseventvar *ev, struct proc *p)
147 {
148
149 if (ev->q != NULL) {
150 #ifdef DIAGNOSTIC
151 printf("wsevent_init: already init\n");
152 #endif
153 return;
154 }
155 #if defined(COMPAT_50) || defined(MODULAR)
156 ev->version = 0;
157 #endif /* defined(COMPAT_50) || defined(MODULAR) */
158 ev->get = ev->put = 0;
159 /*
160 * We allocate the maximum structure size here, so that we don't
161 * need to malloc/free again in setversion.
162 */
163 ev->q = malloc(WSEVENT_QSIZE * sizeof(struct wscons_event),
164 M_DEVBUF, M_WAITOK|M_ZERO);
165 selinit(&ev->sel);
166 ev->io = p;
167 ev->sih = softint_establish(SOFTINT_MPSAFE | SOFTINT_CLOCK,
168 wsevent_intr, ev);
169 }
170
171 /*
172 * Tear down a wscons_event queue.
173 */
174 void
175 wsevent_fini(struct wseventvar *ev)
176 {
177 if (ev->q == NULL) {
178 #ifdef DIAGNOSTIC
179 printf("wsevent_fini: already fini\n");
180 #endif
181 return;
182 }
183 seldestroy(&ev->sel);
184 free(ev->q, M_DEVBUF);
185 ev->q = NULL;
186 softint_disestablish(ev->sih);
187 }
188
189 /*
190 * User-level interface: read, poll.
191 * (User cannot write an event queue.)
192 */
193 int
194 wsevent_read(struct wseventvar *ev, struct uio *uio, int flags)
195 {
196 int s, n, cnt, error;
197
198 /*
199 * Make sure we can return at least 1.
200 */
201 if (uio->uio_resid < EVSIZE(ev))
202 return (EMSGSIZE); /* ??? */
203 s = splwsevent();
204 while (ev->get == ev->put) {
205 if (flags & IO_NDELAY) {
206 splx(s);
207 return (EWOULDBLOCK);
208 }
209 ev->wanted = 1;
210 error = tsleep(ev, PWSEVENT | PCATCH, "wsevent_read", 0);
211 if (error) {
212 splx(s);
213 return (error);
214 }
215 }
216 /*
217 * Move wscons_event from tail end of queue (there is at least one
218 * there).
219 */
220 if (ev->put < ev->get)
221 cnt = WSEVENT_QSIZE - ev->get; /* events in [get..QSIZE) */
222 else
223 cnt = ev->put - ev->get; /* events in [get..put) */
224 splx(s);
225 n = howmany(uio->uio_resid, EVSIZE(ev));
226 if (cnt > n)
227 cnt = n;
228 error = uiomove(EVARRAY(ev, ev->get), cnt * EVSIZE(ev), uio);
229 n -= cnt;
230 /*
231 * If we do not wrap to 0, used up all our space, or had an error,
232 * stop. Otherwise move from front of queue to put index, if there
233 * is anything there to move.
234 */
235 if ((ev->get = (ev->get + cnt) % WSEVENT_QSIZE) != 0 ||
236 n == 0 || error || (cnt = ev->put) == 0)
237 return (error);
238 if (cnt > n)
239 cnt = n;
240 error = uiomove(EVARRAY(ev, 0), cnt * EVSIZE(ev), uio);
241 ev->get = cnt;
242 return (error);
243 }
244
245 int
246 wsevent_poll(struct wseventvar *ev, int events, struct lwp *l)
247 {
248 int revents = 0;
249 int s = splwsevent();
250
251 if (events & (POLLIN | POLLRDNORM)) {
252 if (ev->get != ev->put)
253 revents |= events & (POLLIN | POLLRDNORM);
254 else
255 selrecord(l, &ev->sel);
256 }
257
258 splx(s);
259 return (revents);
260 }
261
262 static void
263 filt_wseventrdetach(struct knote *kn)
264 {
265 struct wseventvar *ev = kn->kn_hook;
266 int s;
267
268 s = splwsevent();
269 SLIST_REMOVE(&ev->sel.sel_klist, kn, knote, kn_selnext);
270 splx(s);
271 }
272
273 static int
274 filt_wseventread(struct knote *kn, long hint)
275 {
276 struct wseventvar *ev = kn->kn_hook;
277
278 if (ev->get == ev->put)
279 return (0);
280
281 if (ev->get < ev->put)
282 kn->kn_data = ev->put - ev->get;
283 else
284 kn->kn_data = (WSEVENT_QSIZE - ev->get) + ev->put;
285
286 kn->kn_data *= EVSIZE(ev);
287
288 return (1);
289 }
290
291 static const struct filterops wsevent_filtops =
292 { 1, NULL, filt_wseventrdetach, filt_wseventread };
293
294 int
295 wsevent_kqfilter(struct wseventvar *ev, struct knote *kn)
296 {
297 struct klist *klist;
298 int s;
299
300 switch (kn->kn_filter) {
301 case EVFILT_READ:
302 klist = &ev->sel.sel_klist;
303 kn->kn_fop = &wsevent_filtops;
304 break;
305
306 default:
307 return (EINVAL);
308 }
309
310 kn->kn_hook = ev;
311
312 s = splwsevent();
313 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
314 splx(s);
315
316 return (0);
317 }
318
319 /*
320 * Wakes up all listener of the 'ev' queue.
321 */
322 void
323 wsevent_wakeup(struct wseventvar *ev)
324 {
325
326 selnotify(&ev->sel, 0, 0);
327
328 if (ev->wanted) {
329 ev->wanted = 0;
330 wakeup(ev);
331 }
332
333 if (ev->async) {
334 softint_schedule(ev->sih);
335 }
336 }
337
338 /*
339 * Soft interrupt handler: sends signal to async proc.
340 */
341 static void
342 wsevent_intr(void *cookie)
343 {
344 struct wseventvar *ev;
345
346 ev = cookie;
347
348 if (ev->async) {
349 mutex_enter(proc_lock);
350 psignal(ev->io, SIGIO);
351 mutex_exit(proc_lock);
352 }
353 }
354
355 /*
356 * Injects the set of events given in 'events', whose size is 'nevents',
357 * into the 'ev' queue. If there is not enough free space to inject them
358 * all, returns ENOSPC and the queue is left intact; otherwise returns 0
359 * and wakes up all listeners.
360 */
361 int
362 wsevent_inject(struct wseventvar *ev, struct wscons_event *events,
363 size_t nevents)
364 {
365 size_t avail, i;
366 struct timespec t;
367
368 /* Calculate number of free slots in the queue. */
369 if (ev->put < ev->get)
370 avail = ev->get - ev->put;
371 else
372 avail = WSEVENT_QSIZE - (ev->put - ev->get);
373 KASSERT(avail <= WSEVENT_QSIZE);
374
375 /* Fail if there is all events will not fit in the queue. */
376 if (avail < nevents)
377 return ENOSPC;
378
379 /* Use the current time for all events. */
380 getnanotime(&t);
381
382 /* Inject the events. */
383 switch (ev->version) {
384 #if defined(COMPAT_50) || defined(MODULAR)
385 case 0:
386 for (i = 0; i < nevents; i++) {
387 struct owscons_event *we;
388
389 we = EVARRAY(ev, ev->put);
390 we->type = events[i].type;
391 we->value = events[i].value;
392 timespec_to_timespec50(&t, &we->time);
393
394 ev->put = (ev->put + 1) % WSEVENT_QSIZE;
395 }
396 break;
397 #endif /* defined(COMPAT_50) || defined(MODULAR) */
398 case WSEVENT_VERSION:
399 for (i = 0; i < nevents; i++) {
400 struct wscons_event *we;
401
402 we = EVARRAY(ev, ev->put);
403 we->type = events[i].type;
404 we->value = events[i].value;
405 we->time = t;
406
407 ev->put = (ev->put + 1) % WSEVENT_QSIZE;
408 }
409 break;
410
411 default:
412 return EINVAL;
413 }
414
415 wsevent_wakeup(ev);
416
417 return 0;
418 }
419
420 int
421 wsevent_setversion(struct wseventvar *ev, int vers)
422 {
423 if (ev == NULL)
424 return EINVAL;
425
426 switch (vers) {
427 case 0:
428 case WSEVENT_VERSION:
429 break;
430 default:
431 return EINVAL;
432 }
433
434 if (vers == ev->version)
435 return 0;
436
437 ev->get = ev->put = 0;
438 ev->version = vers;
439 return 0;
440 }
441