xref: /netbsd-src/sys/arch/atari/dev/event.c (revision 5ab06420d2c61f1ec805d65adf312f051340b253)
1 /*	$NetBSD: event.c,v 1.17 2021/09/26 16:36:18 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *	This product includes software developed by the University of
14  *	California, Lawrence Berkeley Laboratory.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)event.c	8.1 (Berkeley) 6/11/93
41  *
42  * from: Header: event.c,v 1.5 92/11/26 01:10:44 torek Exp  (LBL)
43  */
44 
45 /*
46  * Internal `Firm_event' interface for the keyboard and mouse drivers.
47  */
48 
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: event.c,v 1.17 2021/09/26 16:36:18 thorpej Exp $");
51 
52 #include <sys/param.h>
53 #include <sys/fcntl.h>
54 #include <sys/kmem.h>
55 #include <sys/proc.h>
56 #include <sys/systm.h>
57 #include <sys/vnode.h>
58 #include <sys/select.h>
59 #include <sys/poll.h>
60 
61 #include <atari/dev/vuid_event.h>
62 #include <atari/dev/event_var.h>
63 
64 #define	EV_Q_ALLOCSIZE		(EV_QSIZE * sizeof(struct firm_event))
65 
66 /*
67  * Initialize a firm_event queue.
68  */
69 void
ev_init(register struct evvar * ev)70 ev_init(register struct evvar *ev)
71 {
72 
73 	ev->ev_get = ev->ev_put = 0;
74 	ev->ev_q = kmem_zalloc(EV_Q_ALLOCSIZE, KM_SLEEP);
75 	selinit(&ev->ev_sel);
76 }
77 
78 /*
79  * Tear down a firm_event queue.
80  */
81 void
ev_fini(register struct evvar * ev)82 ev_fini(register struct evvar *ev)
83 {
84 
85 	seldestroy(&ev->ev_sel);
86 	kmem_free(ev->ev_q, EV_Q_ALLOCSIZE);
87 }
88 
89 /*
90  * User-level interface: read, select.
91  * (User cannot write an event queue.)
92  */
93 int
ev_read(register struct evvar * ev,struct uio * uio,int flags)94 ev_read(register struct evvar *ev, struct uio *uio, int flags)
95 {
96 	int s, n, cnt, error;
97 
98 	/*
99 	 * Make sure we can return at least 1.
100 	 */
101 	if (uio->uio_resid < sizeof(struct firm_event))
102 		return (EMSGSIZE);	/* ??? */
103 	s = splev();
104 	while (ev->ev_get == ev->ev_put) {
105 		if (flags & IO_NDELAY) {
106 			splx(s);
107 			return (EWOULDBLOCK);
108 		}
109 		ev->ev_wanted = 1;
110 		error = tsleep((void *)ev, PEVENT | PCATCH, "firm_event", 0);
111 		if (error) {
112 			splx(s);
113 			return (error);
114 		}
115 	}
116 	/*
117 	 * Move firm_events from tail end of queue (there is at least one
118 	 * there).
119 	 */
120 	if (ev->ev_put < ev->ev_get)
121 		cnt = EV_QSIZE - ev->ev_get;	/* events in [get..QSIZE) */
122 	else
123 		cnt = ev->ev_put - ev->ev_get;	/* events in [get..put) */
124 	splx(s);
125 	n = howmany(uio->uio_resid, sizeof(struct firm_event));
126 	if (cnt > n)
127 		cnt = n;
128 	error = uiomove((void *)&ev->ev_q[ev->ev_get],
129 	    cnt * sizeof(struct firm_event), uio);
130 	n -= cnt;
131 	/*
132 	 * If we do not wrap to 0, used up all our space, or had an error,
133 	 * stop.  Otherwise move from front of queue to put index, if there
134 	 * is anything there to move.
135 	 */
136 	if ((ev->ev_get = (ev->ev_get + cnt) % EV_QSIZE) != 0 ||
137 	    n == 0 || error || (cnt = ev->ev_put) == 0)
138 		return (error);
139 	if (cnt > n)
140 		cnt = n;
141 	error = uiomove((void *)&ev->ev_q[0],
142 	    cnt * sizeof(struct firm_event), uio);
143 	ev->ev_get = cnt;
144 	return (error);
145 }
146 
147 int
ev_poll(register struct evvar * ev,int events,struct lwp * l)148 ev_poll(register struct evvar *ev, int events, struct lwp *l)
149 {
150 	int revents = 0;
151 	int s = splev();
152 
153 	if (events & (POLLIN | POLLRDNORM)) {
154 		if (ev->ev_get != ev->ev_put)
155 			revents |= events & (POLLIN | POLLRDNORM);
156 		else
157 			selrecord(l, &ev->ev_sel);
158 	}
159 	splx(s);
160 	return (revents);
161 }
162 
163 static void
filt_evrdetach(struct knote * kn)164 filt_evrdetach(struct knote *kn)
165 {
166 	struct evvar *ev = kn->kn_hook;
167 	int s;
168 
169 	s = splev();
170 	selremove_knote(&ev->ev_sel, kn);
171 	splx(s);
172 }
173 
174 static int
filt_evread(struct knote * kn,long hint)175 filt_evread(struct knote *kn, long hint)
176 {
177 	struct evvar *ev = kn->kn_hook;
178 
179 	if (ev->ev_get == ev->ev_put)
180 		return (0);
181 
182 	if (ev->ev_get < ev->ev_put)
183 		kn->kn_data = ev->ev_put - ev->ev_get;
184 	else
185 		kn->kn_data = (EV_QSIZE - ev->ev_get) +
186 		    ev->ev_put;
187 
188 	kn->kn_data *= sizeof(struct firm_event);
189 
190 	return (1);
191 }
192 
193 static const struct filterops ev_filtops = {
194 	.f_flags = FILTEROP_ISFD,
195 	.f_attach = NULL,
196 	.f_detach = filt_evrdetach,
197 	.f_event = filt_evread,
198 };
199 
200 int
ev_kqfilter(struct evvar * ev,struct knote * kn)201 ev_kqfilter(struct evvar *ev, struct knote *kn)
202 {
203 	int s;
204 
205 	switch (kn->kn_filter) {
206 	case EVFILT_READ:
207 		kn->kn_fop = &ev_filtops;
208 		break;
209 
210 	default:
211 		return (EINVAL);
212 	}
213 
214 	kn->kn_hook = ev;
215 
216 	s = splev();
217 	selrecord_knote(&ev->ev_sel, kn);
218 	splx(s);
219 
220 	return (0);
221 }
222