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