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