xref: /netbsd/sys/dev/wscons/wsevent.c (revision bf9ec67e)
1 /* $NetBSD: wsevent.c,v 1.10 2002/01/12 16:41:02 tsutsui 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.10 2002/01/12 16:41:02 tsutsui 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