wsevent.c revision 1.12 1 /* $NetBSD: wsevent.c,v 1.12 2002/11/26 18:49:50 christos Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1992, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This software was developed by the Computer Systems Engineering group
38 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
39 * contributed to Berkeley.
40 *
41 * All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Lawrence Berkeley Laboratory.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
73 *
74 * @(#)event.c 8.1 (Berkeley) 6/11/93
75 */
76
77 /*
78 * Internal "wscons_event" queue interface for the keyboard and mouse drivers.
79 */
80
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: wsevent.c,v 1.12 2002/11/26 18:49:50 christos Exp $");
83
84 #include <sys/param.h>
85 #include <sys/fcntl.h>
86 #include <sys/malloc.h>
87 #include <sys/proc.h>
88 #include <sys/systm.h>
89 #include <sys/vnode.h>
90 #include <sys/select.h>
91 #include <sys/poll.h>
92
93 #include <dev/wscons/wsconsio.h>
94 #include <dev/wscons/wseventvar.h>
95
96 /*
97 * Initialize a wscons_event queue.
98 */
99 void
100 wsevent_init(struct wseventvar *ev)
101 {
102
103 #ifdef DIAGNOSTIC
104 if (ev->q != NULL) {
105 printf("wsevent_init: already init\n");
106 return;
107 }
108 #endif
109 ev->get = ev->put = 0;
110 ev->q = malloc((u_long)WSEVENT_QSIZE * sizeof(struct wscons_event),
111 M_DEVBUF, M_WAITOK|M_ZERO);
112 }
113
114 /*
115 * Tear down a wscons_event queue.
116 */
117 void
118 wsevent_fini(struct wseventvar *ev)
119 {
120
121 free(ev->q, M_DEVBUF);
122 ev->q = NULL;
123 }
124
125 /*
126 * User-level interface: read, poll.
127 * (User cannot write an event queue.)
128 */
129 int
130 wsevent_read(struct wseventvar *ev, struct uio *uio, int flags)
131 {
132 int s, n, cnt, error;
133
134 /*
135 * Make sure we can return at least 1.
136 */
137 if (uio->uio_resid < sizeof(struct wscons_event))
138 return (EMSGSIZE); /* ??? */
139 s = splwsevent();
140 while (ev->get == ev->put) {
141 if (flags & IO_NDELAY) {
142 splx(s);
143 return (EWOULDBLOCK);
144 }
145 ev->wanted = 1;
146 error = tsleep(ev, PWSEVENT | PCATCH,
147 "wsevent_read", 0);
148 if (error) {
149 splx(s);
150 return (error);
151 }
152 }
153 /*
154 * Move wscons_event from tail end of queue (there is at least one
155 * there).
156 */
157 if (ev->put < ev->get)
158 cnt = WSEVENT_QSIZE - ev->get; /* events in [get..QSIZE) */
159 else
160 cnt = ev->put - ev->get; /* events in [get..put) */
161 splx(s);
162 n = howmany(uio->uio_resid, sizeof(struct wscons_event));
163 if (cnt > n)
164 cnt = n;
165 error = uiomove(&ev->q[ev->get],
166 cnt * sizeof(struct wscons_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->get = (ev->get + cnt) % WSEVENT_QSIZE) != 0 ||
174 n == 0 || error || (cnt = ev->put) == 0)
175 return (error);
176 if (cnt > n)
177 cnt = n;
178 error = uiomove(&ev->q[0],
179 cnt * sizeof(struct wscons_event), uio);
180 ev->get = cnt;
181 return (error);
182 }
183
184 int
185 wsevent_poll(struct wseventvar *ev, int events, struct proc *p)
186 {
187 int revents = 0;
188 int s = splwsevent();
189
190 if (events & (POLLIN | POLLRDNORM)) {
191 if (ev->get != ev->put)
192 revents |= events & (POLLIN | POLLRDNORM);
193 else
194 selrecord(p, &ev->sel);
195 }
196
197 splx(s);
198 return (revents);
199 }
200
201 static void
202 filt_wseventrdetach(struct knote *kn)
203 {
204 struct wseventvar *ev = kn->kn_hook;
205 int s;
206
207 s = splwsevent();
208 SLIST_REMOVE(&ev->sel.sel_klist, kn, knote, kn_selnext);
209 splx(s);
210 }
211
212 static int
213 filt_wseventread(struct knote *kn, long hint)
214 {
215 struct wseventvar *ev = kn->kn_hook;
216
217 if (ev->get == ev->put)
218 return (0);
219
220 if (ev->get < ev->put)
221 kn->kn_data = ev->put - ev->get;
222 else
223 kn->kn_data = (WSEVENT_QSIZE - ev->get) +
224 ev->put;
225
226 kn->kn_data *= sizeof(struct wscons_event);
227
228 return (1);
229 }
230
231 static const struct filterops wsevent_filtops =
232 { 1, NULL, filt_wseventrdetach, filt_wseventread };
233
234 int
235 wsevent_kqfilter(struct wseventvar *ev, struct knote *kn)
236 {
237 struct klist *klist;
238 int s;
239
240 switch (kn->kn_filter) {
241 case EVFILT_READ:
242 klist = &ev->sel.sel_klist;
243 kn->kn_fop = &wsevent_filtops;
244 break;
245
246 default:
247 return (1);
248 }
249
250 kn->kn_hook = ev;
251
252 s = splwsevent();
253 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
254 splx(s);
255
256 return (0);
257 }
258