xref: /netbsd-src/sys/arch/atari/dev/event.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: event.c,v 1.12 2008/03/01 14:16:49 rmind 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.12 2008/03/01 14:16:49 rmind Exp $");
51 
52 #include <sys/param.h>
53 #include <sys/fcntl.h>
54 #include <sys/malloc.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 /*
65  * Initialize a firm_event queue.
66  */
67 void
68 ev_init(ev)
69 	register struct evvar *ev;
70 {
71 
72 	ev->ev_get = ev->ev_put = 0;
73 	ev->ev_q = malloc((u_long)EV_QSIZE * sizeof(struct firm_event),
74 	    M_DEVBUF, M_WAITOK|M_ZERO);
75 	selinit(&ev->ev_sel);
76 }
77 
78 /*
79  * Tear down a firm_event queue.
80  */
81 void
82 ev_fini(ev)
83 	register struct evvar *ev;
84 {
85 
86 	seldestroy(&ev->ev_sel);
87 	free(ev->ev_q, M_DEVBUF);
88 }
89 
90 /*
91  * User-level interface: read, select.
92  * (User cannot write an event queue.)
93  */
94 int
95 ev_read(ev, uio, flags)
96 	register struct evvar *ev;
97 	struct uio *uio;
98 	int flags;
99 {
100 	int s, n, cnt, error;
101 
102 	/*
103 	 * Make sure we can return at least 1.
104 	 */
105 	if (uio->uio_resid < sizeof(struct firm_event))
106 		return (EMSGSIZE);	/* ??? */
107 	s = splev();
108 	while (ev->ev_get == ev->ev_put) {
109 		if (flags & IO_NDELAY) {
110 			splx(s);
111 			return (EWOULDBLOCK);
112 		}
113 		ev->ev_wanted = 1;
114 		error = tsleep((void *)ev, PEVENT | PCATCH, "firm_event", 0);
115 		if (error) {
116 			splx(s);
117 			return (error);
118 		}
119 	}
120 	/*
121 	 * Move firm_events from tail end of queue (there is at least one
122 	 * there).
123 	 */
124 	if (ev->ev_put < ev->ev_get)
125 		cnt = EV_QSIZE - ev->ev_get;	/* events in [get..QSIZE) */
126 	else
127 		cnt = ev->ev_put - ev->ev_get;	/* events in [get..put) */
128 	splx(s);
129 	n = howmany(uio->uio_resid, sizeof(struct firm_event));
130 	if (cnt > n)
131 		cnt = n;
132 	error = uiomove((void *)&ev->ev_q[ev->ev_get],
133 	    cnt * sizeof(struct firm_event), uio);
134 	n -= cnt;
135 	/*
136 	 * If we do not wrap to 0, used up all our space, or had an error,
137 	 * stop.  Otherwise move from front of queue to put index, if there
138 	 * is anything there to move.
139 	 */
140 	if ((ev->ev_get = (ev->ev_get + cnt) % EV_QSIZE) != 0 ||
141 	    n == 0 || error || (cnt = ev->ev_put) == 0)
142 		return (error);
143 	if (cnt > n)
144 		cnt = n;
145 	error = uiomove((void *)&ev->ev_q[0],
146 	    cnt * sizeof(struct firm_event), uio);
147 	ev->ev_get = cnt;
148 	return (error);
149 }
150 
151 int
152 ev_poll(ev, events, l)
153 	register struct evvar *ev;
154 	int events;
155 	struct lwp *l;
156 {
157 	int revents = 0;
158 	int s = splev();
159 
160 	if (events & (POLLIN | POLLRDNORM)) {
161 		if (ev->ev_get != ev->ev_put)
162 			revents |= events & (POLLIN | POLLRDNORM);
163 		else
164 			selrecord(l, &ev->ev_sel);
165 	}
166 	splx(s);
167 	return (revents);
168 }
169 
170 static void
171 filt_evrdetach(struct knote *kn)
172 {
173 	struct evvar *ev = kn->kn_hook;
174 	int s;
175 
176 	s = splev();
177 	SLIST_REMOVE(&ev->ev_sel.sel_klist, kn, knote, kn_selnext);
178 	splx(s);
179 }
180 
181 static int
182 filt_evread(struct knote *kn, long hint)
183 {
184 	struct evvar *ev = kn->kn_hook;
185 
186 	if (ev->ev_get == ev->ev_put)
187 		return (0);
188 
189 	if (ev->ev_get < ev->ev_put)
190 		kn->kn_data = ev->ev_put - ev->ev_get;
191 	else
192 		kn->kn_data = (EV_QSIZE - ev->ev_get) +
193 		    ev->ev_put;
194 
195 	kn->kn_data *= sizeof(struct firm_event);
196 
197 	return (1);
198 }
199 
200 static const struct filterops ev_filtops =
201 	{ 1, NULL, filt_evrdetach, filt_evread };
202 
203 int
204 ev_kqfilter(struct evvar *ev, struct knote *kn)
205 {
206 	struct klist *klist;
207 	int s;
208 
209 	switch (kn->kn_filter) {
210 	case EVFILT_READ:
211 		klist = &ev->ev_sel.sel_klist;
212 		kn->kn_fop = &ev_filtops;
213 		break;
214 
215 	default:
216 		return (1);
217 	}
218 
219 	kn->kn_hook = ev;
220 
221 	s = splev();
222 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
223 	splx(s);
224 
225 	return (0);
226 }
227