wsevent.c revision 1.20
1/* $OpenBSD: wsevent.c,v 1.20 2019/05/22 18:52:14 anton Exp $ */
2/* $NetBSD: wsevent.c,v 1.16 2003/08/07 16:31:29 agc Exp $ */
3
4/*
5 * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *      This product includes software developed by Christopher G. Demetriou
18 *	for the NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/*
35 * Copyright (c) 1992, 1993
36 *	The Regents of the University of California.  All rights reserved.
37 *
38 * This software was developed by the Computer Systems Engineering group
39 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
40 * contributed to Berkeley.
41 *
42 * All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 *	This product includes software developed by the University of
45 *	California, Lawrence Berkeley Laboratory.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 *    notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 *    notice, this list of conditions and the following disclaimer in the
54 *    documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 *    may be used to endorse or promote products derived from this software
57 *    without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 *	@(#)event.c	8.1 (Berkeley) 6/11/93
72 */
73
74/*
75 * Internal "wscons_event" queue interface for the keyboard and mouse drivers.
76 */
77
78#include <sys/param.h>
79#include <sys/malloc.h>
80#include <sys/systm.h>
81#include <sys/vnode.h>
82#include <sys/selinfo.h>
83#include <sys/poll.h>
84
85#include <dev/wscons/wsconsio.h>
86#include <dev/wscons/wseventvar.h>
87
88void	filt_wseventdetach(struct knote *);
89int	filt_wseventread(struct knote *, long);
90
91const struct filterops wsevent_filtops = {
92	1,
93	NULL,
94	filt_wseventdetach,
95	filt_wseventread
96};
97
98/*
99 * Initialize a wscons_event queue.
100 */
101int
102wsevent_init(struct wseventvar *ev)
103{
104	struct wscons_event *queue;
105
106	if (ev->q != NULL)
107		return (0);
108
109        queue = mallocarray(WSEVENT_QSIZE, sizeof(struct wscons_event),
110	    M_DEVBUF, M_WAITOK | M_ZERO);
111	if (ev->q != NULL) {
112		free(queue, M_DEVBUF, WSEVENT_QSIZE * sizeof(struct wscons_event));
113		return (1);
114	}
115
116	ev->q = queue;
117	ev->get = ev->put = 0;
118
119	sigio_init(&ev->sigio);
120
121	return (0);
122}
123
124/*
125 * Tear down a wscons_event queue.
126 */
127void
128wsevent_fini(struct wseventvar *ev)
129{
130	if (ev->q == NULL) {
131#ifdef DIAGNOSTIC
132		printf("wsevent_fini: already invoked\n");
133#endif
134		return;
135	}
136	free(ev->q, M_DEVBUF, WSEVENT_QSIZE * sizeof(struct wscons_event));
137	ev->q = NULL;
138
139	sigio_free(&ev->sigio);
140}
141
142/*
143 * User-level interface: read, poll.
144 * (User cannot write an event queue.)
145 */
146int
147wsevent_read(struct wseventvar *ev, struct uio *uio, int flags)
148{
149	int s, error;
150	u_int cnt;
151	size_t n;
152
153	/*
154	 * Make sure we can return at least 1.
155	 */
156	if (uio->uio_resid < sizeof(struct wscons_event))
157		return (EMSGSIZE);	/* ??? */
158	s = splwsevent();
159	while (ev->get == ev->put) {
160		if (flags & IO_NDELAY) {
161			splx(s);
162			return (EWOULDBLOCK);
163		}
164		ev->wanted = 1;
165		error = tsleep(ev, PWSEVENT | PCATCH,
166		    "wsevent_read", 0);
167		if (error) {
168			splx(s);
169			return (error);
170		}
171	}
172	/*
173	 * Move wscons_event from tail end of queue (there is at least one
174	 * there).
175	 */
176	if (ev->put < ev->get)
177		cnt = WSEVENT_QSIZE - ev->get;	/* events in [get..QSIZE) */
178	else
179		cnt = ev->put - ev->get;	/* events in [get..put) */
180	splx(s);
181	n = howmany(uio->uio_resid, sizeof(struct wscons_event));
182	if (cnt > n)
183		cnt = n;
184	error = uiomove((caddr_t)&ev->q[ev->get],
185	    cnt * sizeof(struct wscons_event), uio);
186	n -= cnt;
187	/*
188	 * If we do not wrap to 0, used up all our space, or had an error,
189	 * stop.  Otherwise move from front of queue to put index, if there
190	 * is anything there to move.
191	 */
192	if ((ev->get = (ev->get + cnt) % WSEVENT_QSIZE) != 0 ||
193	    n == 0 || error || (cnt = ev->put) == 0)
194		return (error);
195	if (cnt > n)
196		cnt = n;
197	error = uiomove((caddr_t)&ev->q[0],
198	    cnt * sizeof(struct wscons_event), uio);
199	ev->get = cnt;
200	return (error);
201}
202
203int
204wsevent_poll(struct wseventvar *ev, int events, struct proc *p)
205{
206	int revents = 0;
207	int s = splwsevent();
208
209	if (events & (POLLIN | POLLRDNORM)) {
210		if (ev->get != ev->put)
211			revents |= events & (POLLIN | POLLRDNORM);
212		else
213			selrecord(p, &ev->sel);
214	}
215
216	splx(s);
217	return (revents);
218}
219
220int
221wsevent_kqfilter(struct wseventvar *ev, struct knote *kn)
222{
223	struct klist *klist;
224	int s;
225
226	klist = &ev->sel.si_note;
227
228	switch (kn->kn_filter) {
229	case EVFILT_READ:
230		kn->kn_fop = &wsevent_filtops;
231		break;
232	default:
233		return (EINVAL);
234	}
235
236	kn->kn_hook = ev;
237
238	s = splwsevent();
239	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
240	splx(s);
241
242	return (0);
243}
244
245void
246filt_wseventdetach(struct knote *kn)
247{
248	struct wseventvar *ev = kn->kn_hook;
249	struct klist *klist = &ev->sel.si_note;
250	int s;
251
252	s = splwsevent();
253	SLIST_REMOVE(klist, kn, knote, kn_selnext);
254	splx(s);
255}
256
257int
258filt_wseventread(struct knote *kn, long hint)
259{
260	struct wseventvar *ev = kn->kn_hook;
261
262	if (ev->get == ev->put)
263		return (0);
264
265	if (ev->get < ev->put)
266		kn->kn_data = ev->put - ev->get;
267	else
268		kn->kn_data = (WSEVENT_QSIZE - ev->get) + ev->put;
269
270	return (1);
271}
272