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