| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: event.c,v 1.19 2021/09/26 16:36:19 thorpej Exp $ */ | | 1 | /* $NetBSD: event.c,v 1.20 2022/05/26 14:30:36 tsutsui Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1992, 1993 | | 4 | * Copyright (c) 1992, 1993 |
5 | * The Regents of the University of California. All rights reserved. | | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | | 6 | * |
7 | * This software was developed by the Computer Systems Engineering group | | 7 | * This software was developed by the Computer Systems Engineering group |
8 | * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and | | 8 | * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and |
9 | * contributed to Berkeley. | | 9 | * contributed to Berkeley. |
10 | * | | 10 | * |
11 | * All advertising materials mentioning features or use of this software | | 11 | * All advertising materials mentioning features or use of this software |
12 | * must display the following acknowledgement: | | 12 | * must display the following acknowledgement: |
13 | * This product includes software developed by the University of | | 13 | * This product includes software developed by the University of |
14 | * California, Lawrence Berkeley Laboratory. | | 14 | * California, Lawrence Berkeley Laboratory. |
| @@ -35,27 +35,27 @@ | | | @@ -35,27 +35,27 @@ |
35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
38 | * SUCH DAMAGE. | | 38 | * SUCH DAMAGE. |
39 | * | | 39 | * |
40 | * @(#)event.c 8.1 (Berkeley) 6/11/93 | | 40 | * @(#)event.c 8.1 (Berkeley) 6/11/93 |
41 | */ | | 41 | */ |
42 | | | 42 | |
43 | /* | | 43 | /* |
44 | * Internal `Firm_event' interface for the keyboard and mouse drivers. | | 44 | * Internal `Firm_event' interface for the keyboard and mouse drivers. |
45 | */ | | 45 | */ |
46 | | | 46 | |
47 | #include <sys/cdefs.h> | | 47 | #include <sys/cdefs.h> |
48 | __KERNEL_RCSID(0, "$NetBSD: event.c,v 1.19 2021/09/26 16:36:19 thorpej Exp $"); | | 48 | __KERNEL_RCSID(0, "$NetBSD: event.c,v 1.20 2022/05/26 14:30:36 tsutsui Exp $"); |
49 | | | 49 | |
50 | #include <sys/param.h> | | 50 | #include <sys/param.h> |
51 | #include <sys/fcntl.h> | | 51 | #include <sys/fcntl.h> |
52 | #include <sys/kmem.h> | | 52 | #include <sys/kmem.h> |
53 | #include <sys/proc.h> | | 53 | #include <sys/proc.h> |
54 | #include <sys/systm.h> | | 54 | #include <sys/systm.h> |
55 | #include <sys/vnode.h> | | 55 | #include <sys/vnode.h> |
56 | #include <sys/select.h> | | 56 | #include <sys/select.h> |
57 | #include <sys/poll.h> | | 57 | #include <sys/poll.h> |
58 | #include <sys/mutex.h> | | 58 | #include <sys/mutex.h> |
59 | #include <sys/condvar.h> | | 59 | #include <sys/condvar.h> |
60 | | | 60 | |
61 | #include <machine/vuid_event.h> | | 61 | #include <machine/vuid_event.h> |
| @@ -91,87 +91,87 @@ ev_fini(struct evvar *ev) | | | @@ -91,87 +91,87 @@ ev_fini(struct evvar *ev) |
91 | /* | | 91 | /* |
92 | * User-level interface: read, select. | | 92 | * User-level interface: read, select. |
93 | * (User cannot write an event queue.) | | 93 | * (User cannot write an event queue.) |
94 | */ | | 94 | */ |
95 | int | | 95 | int |
96 | ev_read(struct evvar *ev, struct uio *uio, int flags) | | 96 | ev_read(struct evvar *ev, struct uio *uio, int flags) |
97 | { | | 97 | { |
98 | int n, cnt, put, error; | | 98 | int n, cnt, put, error; |
99 | | | 99 | |
100 | /* | | 100 | /* |
101 | * Make sure we can return at least 1. | | 101 | * Make sure we can return at least 1. |
102 | */ | | 102 | */ |
103 | if (uio->uio_resid < sizeof(struct firm_event)) | | 103 | if (uio->uio_resid < sizeof(struct firm_event)) |
104 | return (EMSGSIZE); /* ??? */ | | 104 | return EMSGSIZE; /* ??? */ |
105 | mutex_enter(ev->ev_lock); | | 105 | mutex_enter(ev->ev_lock); |
106 | while (ev->ev_get == ev->ev_put) { | | 106 | while (ev->ev_get == ev->ev_put) { |
107 | if (flags & IO_NDELAY) { | | 107 | if (flags & IO_NDELAY) { |
108 | mutex_exit(ev->ev_lock); | | 108 | mutex_exit(ev->ev_lock); |
109 | return (EWOULDBLOCK); | | 109 | return EWOULDBLOCK; |
110 | } | | 110 | } |
111 | ev->ev_wanted = true; | | 111 | ev->ev_wanted = true; |
112 | error = cv_wait_sig(&ev->ev_cv, ev->ev_lock); | | 112 | error = cv_wait_sig(&ev->ev_cv, ev->ev_lock); |
113 | if (error != 0) { | | 113 | if (error != 0) { |
114 | mutex_exit(ev->ev_lock); | | 114 | mutex_exit(ev->ev_lock); |
115 | return (error); | | 115 | return error; |
116 | } | | 116 | } |
117 | } | | 117 | } |
118 | /* | | 118 | /* |
119 | * Move firm_events from tail end of queue (there is at least one | | 119 | * Move firm_events from tail end of queue (there is at least one |
120 | * there). | | 120 | * there). |
121 | */ | | 121 | */ |
122 | if (ev->ev_put < ev->ev_get) | | 122 | if (ev->ev_put < ev->ev_get) |
123 | cnt = EV_QSIZE - ev->ev_get; /* events in [get..QSIZE) */ | | 123 | cnt = EV_QSIZE - ev->ev_get; /* events in [get..QSIZE) */ |
124 | else | | 124 | else |
125 | cnt = ev->ev_put - ev->ev_get; /* events in [get..put) */ | | 125 | cnt = ev->ev_put - ev->ev_get; /* events in [get..put) */ |
126 | put = ev->ev_put; | | 126 | put = ev->ev_put; |
127 | mutex_exit(ev->ev_lock); | | 127 | mutex_exit(ev->ev_lock); |
128 | n = howmany(uio->uio_resid, sizeof(struct firm_event)); | | 128 | n = howmany(uio->uio_resid, sizeof(struct firm_event)); |
129 | if (cnt > n) | | 129 | if (cnt > n) |
130 | cnt = n; | | 130 | cnt = n; |
131 | error = uiomove((void *)&ev->ev_q[ev->ev_get], | | 131 | error = uiomove((void *)&ev->ev_q[ev->ev_get], |
132 | cnt * sizeof(struct firm_event), uio); | | 132 | cnt * sizeof(struct firm_event), uio); |
133 | n -= cnt; | | 133 | n -= cnt; |
134 | /* | | 134 | /* |
135 | * If we do not wrap to 0, used up all our space, or had an error, | | 135 | * If we do not wrap to 0, used up all our space, or had an error, |
136 | * stop. Otherwise move from front of queue to put index, if there | | 136 | * stop. Otherwise move from front of queue to put index, if there |
137 | * is anything there to move. | | 137 | * is anything there to move. |
138 | */ | | 138 | */ |
139 | if ((ev->ev_get = (ev->ev_get + cnt) % EV_QSIZE) != 0 || | | 139 | if ((ev->ev_get = (ev->ev_get + cnt) % EV_QSIZE) != 0 || |
140 | n == 0 || error || (cnt = put) == 0) | | 140 | n == 0 || error || (cnt = put) == 0) |
141 | return (error); | | 141 | return error; |
142 | if (cnt > n) | | 142 | if (cnt > n) |
143 | cnt = n; | | 143 | cnt = n; |
144 | error = uiomove((void *)&ev->ev_q[0], | | 144 | error = uiomove((void *)&ev->ev_q[0], |
145 | cnt * sizeof(struct firm_event), uio); | | 145 | cnt * sizeof(struct firm_event), uio); |
146 | ev->ev_get = cnt; | | 146 | ev->ev_get = cnt; |
147 | return (error); | | 147 | return error; |
148 | } | | 148 | } |
149 | | | 149 | |
150 | int | | 150 | int |
151 | ev_poll(struct evvar *ev, int events, struct lwp *l) | | 151 | ev_poll(struct evvar *ev, int events, struct lwp *l) |
152 | { | | 152 | { |
153 | int revents = 0; | | 153 | int revents = 0; |
154 | | | 154 | |
155 | mutex_enter(ev->ev_lock); | | 155 | mutex_enter(ev->ev_lock); |
156 | if (events & (POLLIN | POLLRDNORM)) { | | 156 | if (events & (POLLIN | POLLRDNORM)) { |
157 | if (ev->ev_get == ev->ev_put) | | 157 | if (ev->ev_get == ev->ev_put) |
158 | selrecord(l, &ev->ev_sel); | | 158 | selrecord(l, &ev->ev_sel); |
159 | else | | 159 | else |
160 | revents |= events & (POLLIN | POLLRDNORM); | | 160 | revents |= events & (POLLIN | POLLRDNORM); |
161 | } | | 161 | } |
162 | revents |= events & (POLLOUT | POLLWRNORM); | | 162 | revents |= events & (POLLOUT | POLLWRNORM); |
163 | mutex_exit(ev->ev_lock); | | 163 | mutex_exit(ev->ev_lock); |
164 | return (revents); | | 164 | return revents; |
165 | } | | 165 | } |
166 | | | 166 | |
167 | void | | 167 | void |
168 | ev_wakeup(struct evvar *ev) | | 168 | ev_wakeup(struct evvar *ev) |
169 | { | | 169 | { |
170 | | | 170 | |
171 | mutex_enter(ev->ev_lock); | | 171 | mutex_enter(ev->ev_lock); |
172 | selnotify(&ev->ev_sel, 0, 0); | | 172 | selnotify(&ev->ev_sel, 0, 0); |
173 | if (ev->ev_wanted) { | | 173 | if (ev->ev_wanted) { |
174 | ev->ev_wanted = false; | | 174 | ev->ev_wanted = false; |
175 | cv_signal(&ev->ev_cv); | | 175 | cv_signal(&ev->ev_cv); |
176 | } | | 176 | } |
177 | mutex_exit(ev->ev_lock); | | 177 | mutex_exit(ev->ev_lock); |
| @@ -223,24 +223,24 @@ static const struct filterops ev_filtops | | | @@ -223,24 +223,24 @@ static const struct filterops ev_filtops |
223 | .f_event = filt_evread, | | 223 | .f_event = filt_evread, |
224 | }; | | 224 | }; |
225 | | | 225 | |
226 | int | | 226 | int |
227 | ev_kqfilter(struct evvar *ev, struct knote *kn) | | 227 | ev_kqfilter(struct evvar *ev, struct knote *kn) |
228 | { | | 228 | { |
229 | | | 229 | |
230 | switch (kn->kn_filter) { | | 230 | switch (kn->kn_filter) { |
231 | case EVFILT_READ: | | 231 | case EVFILT_READ: |
232 | kn->kn_fop = &ev_filtops; | | 232 | kn->kn_fop = &ev_filtops; |
233 | break; | | 233 | break; |
234 | | | 234 | |
235 | default: | | 235 | default: |
236 | return (EINVAL); | | 236 | return EINVAL; |
237 | } | | 237 | } |
238 | | | 238 | |
239 | kn->kn_hook = ev; | | 239 | kn->kn_hook = ev; |
240 | | | 240 | |
241 | mutex_enter(ev->ev_lock); | | 241 | mutex_enter(ev->ev_lock); |
242 | selrecord_knote(&ev->ev_sel, kn); | | 242 | selrecord_knote(&ev->ev_sel, kn); |
243 | mutex_exit(ev->ev_lock); | | 243 | mutex_exit(ev->ev_lock); |
244 | | | 244 | |
245 | return (0); | | 245 | return 0; |
246 | } | | 246 | } |