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