1 /* $OpenBSD: wsevent.c,v 1.5 2005/11/21 18:16:45 millert Exp $ */
2 /* $NetBSD: wsevent.c,v 1.16 2003/08/07 16:31:29 agc Exp $ */
3
4 /*
5 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christopher G. Demetriou
18 * for the NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35
36 /*
37 * Copyright (c) 1992, 1993
38 * The Regents of the University of California. All rights reserved.
39 *
40 * This software was developed by the Computer Systems Engineering group
41 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
42 * contributed to Berkeley.
43 *
44 * All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Lawrence Berkeley Laboratory.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 *
73 * @(#)event.c 8.1 (Berkeley) 6/11/93
74 */
75
76 /*
77 * Internal "wscons_event" queue interface for the keyboard and mouse drivers.
78 */
79
80 #include <sys/param.h>
81 #include <sys/fcntl.h>
82 #include <sys/malloc.h>
83 #include <sys/proc.h>
84 #include <sys/systm.h>
85 #include <sys/vnode.h>
86 #include <sys/selinfo.h>
87 #include <sys/poll.h>
88
89 #include <dev/wscons/wsconsio.h>
90 #include <dev/wscons/wseventvar.h>
91
92 /*
93 * Initialize a wscons_event queue.
94 */
95 void
96 wsevent_init(struct wseventvar *ev)
97 {
98
99 if (ev->q != NULL) {
100 #ifdef DIAGNOSTIC
101 printf("wsevent_init: already initialized\n");
102 #endif
103 return;
104 }
105 ev->get = ev->put = 0;
106 ev->q = malloc((u_long)WSEVENT_QSIZE * sizeof(struct wscons_event),
107 M_DEVBUF, M_WAITOK);
108 bzero((caddr_t)ev->q, WSEVENT_QSIZE * sizeof(struct wscons_event));
109 }
110
111 /*
112 * Tear down a wscons_event queue.
113 */
114 void
115 wsevent_fini(struct wseventvar *ev)
116 {
117 if (ev->q == NULL) {
118 #ifdef DIAGNOSTIC
119 printf("wsevent_fini: already invoked\n");
120 #endif
121 return;
122 }
123 free(ev->q, M_DEVBUF);
124 ev->q = NULL;
125 }
126
127 /*
128 * User-level interface: read, poll.
129 * (User cannot write an event queue.)
130 */
131 int
132 wsevent_read(struct wseventvar *ev, struct uio *uio, int flags)
133 {
134 int s, n, cnt, error;
135
136 /*
137 * Make sure we can return at least 1.
138 */
139 if (uio->uio_resid < sizeof(struct wscons_event))
140 return (EMSGSIZE); /* ??? */
141 s = splwsevent();
142 while (ev->get == ev->put) {
143 if (flags & IO_NDELAY) {
144 splx(s);
145 return (EWOULDBLOCK);
146 }
147 ev->wanted = 1;
148 error = tsleep(ev, PWSEVENT | PCATCH,
149 "wsevent_read", 0);
150 if (error) {
151 splx(s);
152 return (error);
153 }
154 }
155 /*
156 * Move wscons_event from tail end of queue (there is at least one
157 * there).
158 */
159 if (ev->put < ev->get)
160 cnt = WSEVENT_QSIZE - ev->get; /* events in [get..QSIZE) */
161 else
162 cnt = ev->put - ev->get; /* events in [get..put) */
163 splx(s);
164 n = howmany(uio->uio_resid, sizeof(struct wscons_event));
165 if (cnt > n)
166 cnt = n;
167 error = uiomove((caddr_t)&ev->q[ev->get],
168 cnt * sizeof(struct wscons_event), uio);
169 n -= cnt;
170 /*
171 * If we do not wrap to 0, used up all our space, or had an error,
172 * stop. Otherwise move from front of queue to put index, if there
173 * is anything there to move.
174 */
175 if ((ev->get = (ev->get + cnt) % WSEVENT_QSIZE) != 0 ||
176 n == 0 || error || (cnt = ev->put) == 0)
177 return (error);
178 if (cnt > n)
179 cnt = n;
180 error = uiomove((caddr_t)&ev->q[0],
181 cnt * sizeof(struct wscons_event), uio);
182 ev->get = cnt;
183 return (error);
184 }
185
186 int
187 wsevent_poll(struct wseventvar *ev, int events, struct proc *p)
188 {
189 int revents = 0;
190 int s = splwsevent();
191
192 if (events & (POLLIN | POLLRDNORM)) {
193 if (ev->get != ev->put)
194 revents |= events & (POLLIN | POLLRDNORM);
195 else
196 selrecord(p, &ev->sel);
197 }
198
199 splx(s);
200 return (revents);
201 }