xref: /openbsd/sys/dev/wscons/wsevent.c (revision 5dea098c)
1 /* $OpenBSD: wsevent.c,v 1.28 2024/03/25 13:01:49 mvs 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 
83 #include <dev/wscons/wsconsio.h>
84 #include <dev/wscons/wseventvar.h>
85 
86 void	filt_wseventdetach(struct knote *);
87 int	filt_wseventread(struct knote *, long);
88 
89 const struct filterops wsevent_filtops = {
90 	.f_flags	= FILTEROP_ISFD,
91 	.f_attach	= NULL,
92 	.f_detach	= filt_wseventdetach,
93 	.f_event	= filt_wseventread,
94 };
95 
96 /*
97  * Initialize a wscons_event queue.
98  */
99 int
100 wsevent_init(struct wseventvar *ev)
101 {
102 	struct wscons_event *queue;
103 
104 	if (ev->ws_q != NULL)
105 		return (0);
106 
107         queue = mallocarray(WSEVENT_QSIZE, sizeof(struct wscons_event),
108 	    M_DEVBUF, M_WAITOK | M_ZERO);
109 	if (ev->ws_q != NULL) {
110 		free(queue, M_DEVBUF,
111 		    WSEVENT_QSIZE * sizeof(struct wscons_event));
112 		return (1);
113 	}
114 
115 	ev->ws_q = queue;
116 	ev->ws_get = ev->ws_put = 0;
117 
118 	sigio_init(&ev->ws_sigio);
119 
120 	return (0);
121 }
122 
123 /*
124  * Tear down a wscons_event queue.
125  */
126 void
127 wsevent_fini(struct wseventvar *ev)
128 {
129 	if (ev->ws_q == NULL) {
130 #ifdef DIAGNOSTIC
131 		printf("wsevent_fini: already invoked\n");
132 #endif
133 		return;
134 	}
135 	free(ev->ws_q, M_DEVBUF, WSEVENT_QSIZE * sizeof(struct wscons_event));
136 	ev->ws_q = NULL;
137 
138 	klist_invalidate(&ev->ws_sel.si_note);
139 
140 	sigio_free(&ev->ws_sigio);
141 }
142 
143 /*
144  * User-level interface: read, kqueue.
145  * (User cannot write an event queue.)
146  */
147 int
148 wsevent_read(struct wseventvar *ev, struct uio *uio, int flags)
149 {
150 	int s, error;
151 	u_int cnt;
152 	size_t n;
153 
154 	/*
155 	 * Make sure we can return at least 1.
156 	 */
157 	if (uio->uio_resid < sizeof(struct wscons_event))
158 		return (EMSGSIZE);	/* ??? */
159 	s = splwsevent();
160 	while (ev->ws_get == ev->ws_put) {
161 		if (flags & IO_NDELAY) {
162 			splx(s);
163 			return (EWOULDBLOCK);
164 		}
165 		ev->ws_wanted = 1;
166 		error = tsleep_nsec(ev, PWSEVENT | PCATCH,
167 		    "wsevent_read", INFSLP);
168 		if (error) {
169 			splx(s);
170 			return (error);
171 		}
172 	}
173 	/*
174 	 * Move wscons_event from tail end of queue (there is at least one
175 	 * there).
176 	 */
177 	if (ev->ws_put < ev->ws_get)
178 		cnt = WSEVENT_QSIZE - ev->ws_get; /* events in [get..QSIZE) */
179 	else
180 		cnt = ev->ws_put - ev->ws_get;    /* events in [get..put) */
181 	splx(s);
182 	n = howmany(uio->uio_resid, sizeof(struct wscons_event));
183 	if (cnt > n)
184 		cnt = n;
185 	error = uiomove((caddr_t)&ev->ws_q[ev->ws_get],
186 	    cnt * sizeof(struct wscons_event), uio);
187 	n -= cnt;
188 	/*
189 	 * If we do not wrap to 0, used up all our space, or had an error,
190 	 * stop.  Otherwise move from front of queue to put index, if there
191 	 * is anything there to move.
192 	 */
193 	if ((ev->ws_get = (ev->ws_get + cnt) % WSEVENT_QSIZE) != 0 ||
194 	    n == 0 || error || (cnt = ev->ws_put) == 0)
195 		return (error);
196 	if (cnt > n)
197 		cnt = n;
198 	error = uiomove((caddr_t)&ev->ws_q[0],
199 	    cnt * sizeof(struct wscons_event), uio);
200 	ev->ws_get = cnt;
201 	return (error);
202 }
203 
204 int
205 wsevent_kqfilter(struct wseventvar *ev, struct knote *kn)
206 {
207 	struct klist *klist;
208 	int s;
209 
210 	klist = &ev->ws_sel.si_note;
211 
212 	switch (kn->kn_filter) {
213 	case EVFILT_READ:
214 		kn->kn_fop = &wsevent_filtops;
215 		break;
216 	default:
217 		return (EINVAL);
218 	}
219 
220 	kn->kn_hook = ev;
221 
222 	s = splwsevent();
223 	klist_insert_locked(klist, kn);
224 	splx(s);
225 
226 	return (0);
227 }
228 
229 void
230 filt_wseventdetach(struct knote *kn)
231 {
232 	struct wseventvar *ev = kn->kn_hook;
233 	struct klist *klist = &ev->ws_sel.si_note;
234 	int s;
235 
236 	s = splwsevent();
237 	klist_remove_locked(klist, kn);
238 	splx(s);
239 }
240 
241 int
242 filt_wseventread(struct knote *kn, long hint)
243 {
244 	struct wseventvar *ev = kn->kn_hook;
245 
246 	if (ev->ws_get == ev->ws_put)
247 		return (0);
248 
249 	if (ev->ws_get < ev->ws_put)
250 		kn->kn_data = ev->ws_put - ev->ws_get;
251 	else
252 		kn->kn_data = (WSEVENT_QSIZE - ev->ws_get) + ev->ws_put;
253 
254 	return (1);
255 }
256