event.c revision 1.7 1 /* $NetBSD: event.c,v 1.7 2001/06/07 17:52:52 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
51 #include <sys/param.h>
52 #include <sys/fcntl.h>
53 #include <sys/malloc.h>
54 #include <sys/proc.h>
55 #include <sys/systm.h>
56 #include <sys/vnode.h>
57 #include <sys/select.h>
58 #include <sys/poll.h>
59
60 #include <machine/vuid_event.h>
61 #include <dev/sun/event_var.h>
62
63 int (*ev_out32_hook) __P((struct firm_event *, int, struct uio *));
64
65 /*
66 * Initialize a firm_event queue.
67 */
68 void
69 ev_init(ev)
70 struct evvar *ev;
71 {
72
73 ev->ev_get = ev->ev_put = 0;
74 ev->ev_q = malloc((u_long)EV_QSIZE * sizeof(struct firm_event),
75 M_DEVBUF, M_WAITOK);
76 bzero((caddr_t)ev->ev_q, EV_QSIZE * sizeof(struct firm_event));
77 }
78
79 /*
80 * Tear down a firm_event queue.
81 */
82 void
83 ev_fini(ev)
84 struct evvar *ev;
85 {
86
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 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((caddr_t)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 if ((curproc->p_flag & P_32) && ev_out32_hook != NULL)
133 error = (*ev_out32_hook)(&ev->ev_q[ev->ev_get], cnt, uio);
134 else
135 error = uiomove((caddr_t)&ev->ev_q[ev->ev_get],
136 cnt * sizeof(struct firm_event), uio);
137 n -= cnt;
138 /*
139 * If we do not wrap to 0, used up all our space, or had an error,
140 * stop. Otherwise move from front of queue to put index, if there
141 * is anything there to move.
142 */
143 if ((ev->ev_get = (ev->ev_get + cnt) % EV_QSIZE) != 0 ||
144 n == 0 || error || (cnt = ev->ev_put) == 0)
145 return (error);
146 if (cnt > n)
147 cnt = n;
148 if ((curproc->p_flag & P_32) && ev_out32_hook != NULL)
149 error = (*ev_out32_hook)(&ev->ev_q[0], cnt, uio);
150 else
151 error = uiomove((caddr_t)&ev->ev_q[0],
152 cnt * sizeof(struct firm_event), uio);
153 ev->ev_get = cnt;
154 return (error);
155 }
156
157 int
158 ev_poll(ev, events, p)
159 struct evvar *ev;
160 int events;
161 struct proc *p;
162 {
163 int s = splev(), revents = 0;
164
165 if (events & (POLLIN | POLLRDNORM)) {
166 if (ev->ev_get == ev->ev_put)
167 selrecord(p, &ev->ev_sel);
168 else
169 revents |= events & (POLLIN | POLLRDNORM);
170 }
171 revents |= events & (POLLOUT | POLLWRNORM);
172
173 splx(s);
174 return (revents);
175 }
176