xref: /netbsd-src/sys/dev/sun/event.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: event.c,v 1.8 2001/11/13 06:54:32 lukem 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 
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: event.c,v 1.8 2001/11/13 06:54:32 lukem Exp $");
53 
54 #include <sys/param.h>
55 #include <sys/fcntl.h>
56 #include <sys/malloc.h>
57 #include <sys/proc.h>
58 #include <sys/systm.h>
59 #include <sys/vnode.h>
60 #include <sys/select.h>
61 #include <sys/poll.h>
62 
63 #include <machine/vuid_event.h>
64 #include <dev/sun/event_var.h>
65 
66 int (*ev_out32_hook) __P((struct firm_event *, int, struct uio *));
67 
68 /*
69  * Initialize a firm_event queue.
70  */
71 void
72 ev_init(ev)
73 	struct evvar *ev;
74 {
75 
76 	ev->ev_get = ev->ev_put = 0;
77 	ev->ev_q = malloc((u_long)EV_QSIZE * sizeof(struct firm_event),
78 	    M_DEVBUF, M_WAITOK);
79 	bzero((caddr_t)ev->ev_q, EV_QSIZE * sizeof(struct firm_event));
80 }
81 
82 /*
83  * Tear down a firm_event queue.
84  */
85 void
86 ev_fini(ev)
87 	struct evvar *ev;
88 {
89 
90 	free(ev->ev_q, M_DEVBUF);
91 }
92 
93 /*
94  * User-level interface: read, select.
95  * (User cannot write an event queue.)
96  */
97 int
98 ev_read(ev, uio, flags)
99 	struct evvar *ev;
100 	struct uio *uio;
101 	int flags;
102 {
103 	int s, n, cnt, error;
104 
105 	/*
106 	 * Make sure we can return at least 1.
107 	 */
108 	if (uio->uio_resid < sizeof(struct firm_event))
109 		return (EMSGSIZE);	/* ??? */
110 	s = splev();
111 	while (ev->ev_get == ev->ev_put) {
112 		if (flags & IO_NDELAY) {
113 			splx(s);
114 			return (EWOULDBLOCK);
115 		}
116 		ev->ev_wanted = 1;
117 		error = tsleep((caddr_t)ev, PEVENT | PCATCH, "firm_event", 0);
118 		if (error) {
119 			splx(s);
120 			return (error);
121 		}
122 	}
123 	/*
124 	 * Move firm_events from tail end of queue (there is at least one
125 	 * there).
126 	 */
127 	if (ev->ev_put < ev->ev_get)
128 		cnt = EV_QSIZE - ev->ev_get;	/* events in [get..QSIZE) */
129 	else
130 		cnt = ev->ev_put - ev->ev_get;	/* events in [get..put) */
131 	splx(s);
132 	n = howmany(uio->uio_resid, sizeof(struct firm_event));
133 	if (cnt > n)
134 		cnt = n;
135 	if ((curproc->p_flag & P_32) && ev_out32_hook != NULL)
136 		error = (*ev_out32_hook)(&ev->ev_q[ev->ev_get], cnt, uio);
137 	else
138 		error = uiomove((caddr_t)&ev->ev_q[ev->ev_get],
139 		    cnt * sizeof(struct firm_event), uio);
140 	n -= cnt;
141 	/*
142 	 * If we do not wrap to 0, used up all our space, or had an error,
143 	 * stop.  Otherwise move from front of queue to put index, if there
144 	 * is anything there to move.
145 	 */
146 	if ((ev->ev_get = (ev->ev_get + cnt) % EV_QSIZE) != 0 ||
147 	    n == 0 || error || (cnt = ev->ev_put) == 0)
148 		return (error);
149 	if (cnt > n)
150 		cnt = n;
151 	if ((curproc->p_flag & P_32) && ev_out32_hook != NULL)
152 		error = (*ev_out32_hook)(&ev->ev_q[0], cnt, uio);
153 	else
154 		error = uiomove((caddr_t)&ev->ev_q[0],
155 	    cnt * sizeof(struct firm_event), uio);
156 	ev->ev_get = cnt;
157 	return (error);
158 }
159 
160 int
161 ev_poll(ev, events, p)
162 	struct evvar *ev;
163 	int events;
164 	struct proc *p;
165 {
166 	int s = splev(), revents = 0;
167 
168 	if (events & (POLLIN | POLLRDNORM)) {
169 		if (ev->ev_get == ev->ev_put)
170 			selrecord(p, &ev->ev_sel);
171 		else
172 			revents |= events & (POLLIN | POLLRDNORM);
173 	}
174 	revents |= events & (POLLOUT | POLLWRNORM);
175 
176 	splx(s);
177 	return (revents);
178 }
179