xref: /openbsd/sys/dev/wscons/wseventvar.h (revision ff576b13)
1 /* $OpenBSD: wseventvar.h,v 1.14 2025/01/21 20:13:19 mvs Exp $ */
2 /* $NetBSD: wseventvar.h,v 1.1 1998/03/22 14:24:03 drochner 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_var.h	8.1 (Berkeley) 6/11/93
72  */
73 
74 #include <sys/event.h>
75 #include <sys/sigio.h>
76 #include <sys/signalvar.h>
77 
78 /*
79  * Internal "wscons_event" queue interface for the keyboard and mouse drivers.
80  * The drivers are expected not to place events in the queue above spltty(),
81  * i.e., are expected to run off serial ports or similar devices.
82  */
83 
84 /*
85  * Locks used to protect data
86  *	I	immutable
87  *	m	ws_mtx
88  */
89 
90 /*
91  * XXXSMP: Non WSEVENT_MPSAFE wseventvar structures rely on kernel lock
92  */
93 
94 /* WSEVENT_QSIZE should be a power of two so that `%' is fast */
95 #define	WSEVENT_QSIZE	256	/* may need tuning; this uses 2k */
96 
97 struct wseventvar {
98 	u_int	ws_get;			/* [m] get (read) index (modified
99 					    synchronously) */
100 	volatile u_int ws_put;		/* [m] put (write) index (modified by
101 					    interrupt) */
102 	int	ws_flags;		/* [I] flags, see below*/
103 	struct mutex ws_mtx;
104 	struct klist ws_klist;		/* [m] list of knotes */
105 	struct sigio_ref ws_sigio;	/* async I/O registration */
106 	int	ws_wanted;		/* [m] wake up on input ready */
107 	int	ws_async;		/* [m] send SIGIO on input ready */
108 	struct wscons_event *ws_q;	/* [m] circular buffer (queue) of
109 					    events */
110 };
111 
112 #define WSEVENT_MPSAFE		0x0001
113 
114 static inline void
wsevent_wakeup(struct wseventvar * ev)115 wsevent_wakeup(struct wseventvar *ev)
116 {
117 	int dosigio = 0;
118 
119 	knote(&ev->ws_klist, 0);
120 
121 	mtx_enter(&ev->ws_mtx);
122 	if (ev->ws_wanted) {
123 		ev->ws_wanted = 0;
124 		wakeup(ev);
125 	}
126 	if (ev->ws_async)
127 		dosigio = 1;
128 	mtx_leave(&ev->ws_mtx);
129 
130 	if (dosigio)
131 		pgsigio(&ev->ws_sigio, SIGIO, 0);
132 }
133 
134 #define	WSEVENT_WAKEUP(ev) do { wsevent_wakeup(ev); } while (0)
135 
136 int	wsevent_init_flags(struct wseventvar *, int);
137 void	wsevent_fini(struct wseventvar *);
138 int	wsevent_read(struct wseventvar *, struct uio *, int);
139 int	wsevent_kqfilter(struct wseventvar *, struct knote *);
140 
141 static inline int
wsevent_init(struct wseventvar * ev)142 wsevent_init(struct wseventvar *ev)
143 {
144 	return wsevent_init_flags(ev, 0);
145 }
146 
147 /*
148  * PWSEVENT is set just above PSOCK, which is just above TTIPRI, on the
149  * theory that mouse and keyboard `user' input should be quick.
150  */
151 #define	PWSEVENT	23
152