xref: /netbsd-src/sys/dev/sun/event.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: event.c,v 1.6 2000/08/22 07:36:04 mrg 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. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by the University of
27  *	California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *	@(#)event.c	8.1 (Berkeley) 6/11/93
45  */
46 
47 /*
48  * Internal `Firm_event' interface for the keyboard and mouse drivers.
49  */
50 #include "opt_compat_netbsd32.h"
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 <machine/vuid_event.h>
62 #include <dev/sun/event_var.h>
63 
64 #ifdef COMPAT_NETBSD32
65 #include <compat/netbsd32/netbsd32.h>
66 
67 int ev_out32 __P((struct firm_event *, int, struct uio *));
68 
69 /*
70  * Write out a series of 32-bit firm_events.
71  */
72 int
73 ev_out32(e, n, uio)
74 	struct firm_event *e;
75 	int n;
76 	struct uio *uio;
77 {
78 	struct firm_event32 e32;
79 	int error = 0;
80 
81 	while (n-- && error == 0) {
82 		e32.id = e->id;
83 		e32.value = e->value;
84 		e32.time.tv_sec = e->time.tv_sec;
85 		e32.time.tv_usec = e->time.tv_usec;
86 		error = uiomove((caddr_t)&e32, sizeof(e32), uio);
87 		e++;
88 	}
89 	return (error);
90 }
91 #endif
92 
93 /*
94  * Initialize a firm_event queue.
95  */
96 void
97 ev_init(ev)
98 	struct evvar *ev;
99 {
100 
101 	ev->ev_get = ev->ev_put = 0;
102 	ev->ev_q = malloc((u_long)EV_QSIZE * sizeof(struct firm_event),
103 	    M_DEVBUF, M_WAITOK);
104 	bzero((caddr_t)ev->ev_q, EV_QSIZE * sizeof(struct firm_event));
105 }
106 
107 /*
108  * Tear down a firm_event queue.
109  */
110 void
111 ev_fini(ev)
112 	struct evvar *ev;
113 {
114 
115 	free(ev->ev_q, M_DEVBUF);
116 }
117 
118 /*
119  * User-level interface: read, select.
120  * (User cannot write an event queue.)
121  */
122 int
123 ev_read(ev, uio, flags)
124 	struct evvar *ev;
125 	struct uio *uio;
126 	int flags;
127 {
128 	int s, n, cnt, error;
129 
130 	/*
131 	 * Make sure we can return at least 1.
132 	 */
133 	if (uio->uio_resid < sizeof(struct firm_event))
134 		return (EMSGSIZE);	/* ??? */
135 	s = splev();
136 	while (ev->ev_get == ev->ev_put) {
137 		if (flags & IO_NDELAY) {
138 			splx(s);
139 			return (EWOULDBLOCK);
140 		}
141 		ev->ev_wanted = 1;
142 		error = tsleep((caddr_t)ev, PEVENT | PCATCH, "firm_event", 0);
143 		if (error) {
144 			splx(s);
145 			return (error);
146 		}
147 	}
148 	/*
149 	 * Move firm_events from tail end of queue (there is at least one
150 	 * there).
151 	 */
152 	if (ev->ev_put < ev->ev_get)
153 		cnt = EV_QSIZE - ev->ev_get;	/* events in [get..QSIZE) */
154 	else
155 		cnt = ev->ev_put - ev->ev_get;	/* events in [get..put) */
156 	splx(s);
157 	n = howmany(uio->uio_resid, sizeof(struct firm_event));
158 	if (cnt > n)
159 		cnt = n;
160 #ifdef COMPAT_NETBSD32
161 	if (curproc->p_flag & P_32)
162 		error = ev_out32(&ev->ev_q[ev->ev_get], cnt, uio);
163 	else
164 #endif
165 	error = uiomove((caddr_t)&ev->ev_q[ev->ev_get],
166 	    cnt * sizeof(struct firm_event), uio);
167 	n -= cnt;
168 	/*
169 	 * If we do not wrap to 0, used up all our space, or had an error,
170 	 * stop.  Otherwise move from front of queue to put index, if there
171 	 * is anything there to move.
172 	 */
173 	if ((ev->ev_get = (ev->ev_get + cnt) % EV_QSIZE) != 0 ||
174 	    n == 0 || error || (cnt = ev->ev_put) == 0)
175 		return (error);
176 	if (cnt > n)
177 		cnt = n;
178 #ifdef COMPAT_NETBSD32
179 	if (curproc->p_flag & P_32)
180 		error = ev_out32(&ev->ev_q[0], cnt, uio);
181 	else
182 #endif
183 	error = uiomove((caddr_t)&ev->ev_q[0],
184 	    cnt * sizeof(struct firm_event), uio);
185 	ev->ev_get = cnt;
186 	return (error);
187 }
188 
189 int
190 ev_poll(ev, events, p)
191 	struct evvar *ev;
192 	int events;
193 	struct proc *p;
194 {
195 	int s = splev(), revents = 0;
196 
197 	if (events & (POLLIN | POLLRDNORM)) {
198 		if (ev->ev_get == ev->ev_put)
199 			selrecord(p, &ev->ev_sel);
200 		else
201 			revents |= events & (POLLIN | POLLRDNORM);
202 	}
203 	revents |= events & (POLLOUT | POLLWRNORM);
204 
205 	splx(s);
206 	return (revents);
207 }
208