event.c revision 1.1 1 /*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * All advertising materials mentioning features or use of this software
10 * must display the following acknowledgement:
11 * This product includes software developed by the University of
12 * California, Lawrence Berkeley Laboratory.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @(#)event.c 8.1 (Berkeley) 6/11/93
43 *
44 * from: Header: event.c,v 1.5 92/11/26 01:10:44 torek Exp (LBL)
45 * $Id: event.c,v 1.1 1994/01/26 21:05:41 mw Exp $
46 */
47
48 /*
49 * Internal `Firm_event' interface for the keyboard and mouse drivers.
50 */
51
52 #include "param.h"
53 #include "fcntl.h"
54 #include "malloc.h"
55 #include "proc.h"
56 #include "systm.h"
57 #include "vnode.h"
58
59 #include "vuid_event.h"
60 #include "event_var.h"
61
62 /*
63 * Initialize a firm_event queue.
64 */
65 void
66 ev_init(ev)
67 register struct evvar *ev;
68 {
69
70 ev->ev_get = ev->ev_put = 0;
71 ev->ev_q = malloc((u_long)EV_QSIZE * sizeof(struct firm_event),
72 M_DEVBUF, M_WAITOK);
73 bzero((caddr_t)ev->ev_q, EV_QSIZE * sizeof(struct firm_event));
74 }
75
76 /*
77 * Tear down a firm_event queue.
78 */
79 void
80 ev_fini(ev)
81 register struct evvar *ev;
82 {
83
84 free(ev->ev_q, M_DEVBUF);
85 }
86
87 /*
88 * User-level interface: read, select.
89 * (User cannot write an event queue.)
90 */
91 int
92 ev_read(ev, uio, flags)
93 register struct evvar *ev;
94 struct uio *uio;
95 int flags;
96 {
97 int s, n, cnt, error;
98
99 /*
100 * Make sure we can return at least 1.
101 */
102 if (uio->uio_resid < sizeof(struct firm_event))
103 return (EMSGSIZE); /* ??? */
104 s = splev();
105 while (ev->ev_get == ev->ev_put) {
106 if (flags & IO_NDELAY) {
107 splx(s);
108 return (EWOULDBLOCK);
109 }
110 ev->ev_wanted = 1;
111 error = tsleep((caddr_t)ev, PEVENT | PCATCH, "firm_event", 0);
112 if (error) {
113 splx(s);
114 return (error);
115 }
116 }
117 /*
118 * Move firm_events from tail end of queue (there is at least one
119 * there).
120 */
121 if (ev->ev_put < ev->ev_get)
122 cnt = EV_QSIZE - ev->ev_get; /* events in [get..QSIZE) */
123 else
124 cnt = ev->ev_put - ev->ev_get; /* events in [get..put) */
125 splx(s);
126 n = howmany(uio->uio_resid, sizeof(struct firm_event));
127 if (cnt > n)
128 cnt = n;
129 error = uiomove((caddr_t)&ev->ev_q[ev->ev_get],
130 cnt * sizeof(struct firm_event), uio);
131 n -= cnt;
132 /*
133 * If we do not wrap to 0, used up all our space, or had an error,
134 * stop. Otherwise move from front of queue to put index, if there
135 * is anything there to move.
136 */
137 if ((ev->ev_get = (ev->ev_get + cnt) % EV_QSIZE) != 0 ||
138 n == 0 || error || (cnt = ev->ev_put) == 0)
139 return (error);
140 if (cnt > n)
141 cnt = n;
142 error = uiomove((caddr_t)&ev->ev_q[0],
143 cnt * sizeof(struct firm_event), uio);
144 ev->ev_get = cnt;
145 return (error);
146 }
147
148 int
149 ev_select(ev, rw, p)
150 register struct evvar *ev;
151 int rw;
152 struct proc *p;
153 {
154 int s = splev();
155
156 switch (rw) {
157
158 case FREAD:
159 /* succeed if there is something to read */
160 if (ev->ev_get != ev->ev_put) {
161 splx(s);
162 return (1);
163 }
164 selrecord(p, &ev->ev_sel);
165 break;
166
167 case FWRITE:
168 return (1); /* always fails => never blocks */
169 }
170 splx(s);
171 return (0);
172 }
173