Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: event.c,v 1.20 2022/05/26 14:30:36 tsutsui Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratory.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     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
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)event.c	8.1 (Berkeley) 6/11/93
     41  */
     42 
     43 /*
     44  * Internal `Firm_event' interface for the keyboard and mouse drivers.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: event.c,v 1.20 2022/05/26 14:30:36 tsutsui Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/fcntl.h>
     52 #include <sys/kmem.h>
     53 #include <sys/proc.h>
     54 #include <sys/systm.h>
     55 #include <sys/vnode.h>
     56 #include <sys/select.h>
     57 #include <sys/poll.h>
     58 #include <sys/mutex.h>
     59 #include <sys/condvar.h>
     60 
     61 #include <machine/vuid_event.h>
     62 #include <x68k/dev/event_var.h>
     63 
     64 /*
     65  * Initialize a firm_event queue.
     66  */
     67 void
     68 ev_init(struct evvar *ev, const char *name, kmutex_t *mtx)
     69 {
     70 
     71 	ev->ev_get = ev->ev_put = 0;
     72 	ev->ev_q = kmem_zalloc((size_t)EV_QSIZE * sizeof(struct firm_event),
     73 	    KM_SLEEP);
     74 	selinit(&ev->ev_sel);
     75 	ev->ev_lock = mtx;
     76 	cv_init(&ev->ev_cv, name);
     77 }
     78 
     79 /*
     80  * Tear down a firm_event queue.
     81  */
     82 void
     83 ev_fini(struct evvar *ev)
     84 {
     85 
     86 	cv_destroy(&ev->ev_cv);
     87 	seldestroy(&ev->ev_sel);
     88 	kmem_free(ev->ev_q, (size_t)EV_QSIZE * sizeof(struct firm_event));
     89 }
     90 
     91 /*
     92  * User-level interface: read, select.
     93  * (User cannot write an event queue.)
     94  */
     95 int
     96 ev_read(struct evvar *ev, struct uio *uio, int flags)
     97 {
     98 	int n, cnt, put, error;
     99 
    100 	/*
    101 	 * Make sure we can return at least 1.
    102 	 */
    103 	if (uio->uio_resid < sizeof(struct firm_event))
    104 		return EMSGSIZE;	/* ??? */
    105 	mutex_enter(ev->ev_lock);
    106 	while (ev->ev_get == ev->ev_put) {
    107 		if (flags & IO_NDELAY) {
    108 			mutex_exit(ev->ev_lock);
    109 			return EWOULDBLOCK;
    110 		}
    111 		ev->ev_wanted = true;
    112 		error = cv_wait_sig(&ev->ev_cv, ev->ev_lock);
    113 		if (error != 0) {
    114 			mutex_exit(ev->ev_lock);
    115 			return error;
    116 		}
    117 	}
    118 	/*
    119 	 * Move firm_events from tail end of queue (there is at least one
    120 	 * there).
    121 	 */
    122 	if (ev->ev_put < ev->ev_get)
    123 		cnt = EV_QSIZE - ev->ev_get;	/* events in [get..QSIZE) */
    124 	else
    125 		cnt = ev->ev_put - ev->ev_get;	/* events in [get..put) */
    126 	put = ev->ev_put;
    127 	mutex_exit(ev->ev_lock);
    128 	n = howmany(uio->uio_resid, sizeof(struct firm_event));
    129 	if (cnt > n)
    130 		cnt = n;
    131 	error = uiomove((void *)&ev->ev_q[ev->ev_get],
    132 	    cnt * sizeof(struct firm_event), uio);
    133 	n -= cnt;
    134 	/*
    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
    137 	 * is anything there to move.
    138 	 */
    139 	if ((ev->ev_get = (ev->ev_get + cnt) % EV_QSIZE) != 0 ||
    140 	    n == 0 || error || (cnt = put) == 0)
    141 		return error;
    142 	if (cnt > n)
    143 		cnt = n;
    144 	error = uiomove((void *)&ev->ev_q[0],
    145 	    cnt * sizeof(struct firm_event), uio);
    146 	ev->ev_get = cnt;
    147 	return error;
    148 }
    149 
    150 int
    151 ev_poll(struct evvar *ev, int events, struct lwp *l)
    152 {
    153 	int revents = 0;
    154 
    155 	mutex_enter(ev->ev_lock);
    156 	if (events & (POLLIN | POLLRDNORM)) {
    157 		if (ev->ev_get == ev->ev_put)
    158 			selrecord(l, &ev->ev_sel);
    159 		else
    160 			revents |= events & (POLLIN | POLLRDNORM);
    161 	}
    162 	revents |= events & (POLLOUT | POLLWRNORM);
    163 	mutex_exit(ev->ev_lock);
    164 	return revents;
    165 }
    166 
    167 void
    168 ev_wakeup(struct evvar *ev)
    169 {
    170 
    171 	mutex_enter(ev->ev_lock);
    172 	selnotify(&ev->ev_sel, 0, 0);
    173 	if (ev->ev_wanted) {
    174 		ev->ev_wanted = false;
    175 		cv_signal(&ev->ev_cv);
    176 	}
    177 	mutex_exit(ev->ev_lock);
    178 
    179 	if (ev->ev_async) {
    180 		mutex_enter(&proc_lock);
    181 		psignal(ev->ev_io, SIGIO);
    182 		mutex_exit(&proc_lock);
    183 	}
    184 }
    185 
    186 static void
    187 filt_evrdetach(struct knote *kn)
    188 {
    189 	struct evvar *ev = kn->kn_hook;
    190 
    191 	mutex_enter(ev->ev_lock);
    192 	selremove_knote(&ev->ev_sel, kn);
    193 	mutex_exit(ev->ev_lock);
    194 }
    195 
    196 static int
    197 filt_evread(struct knote *kn, long hint)
    198 {
    199 	struct evvar *ev = kn->kn_hook;
    200 	int rv = 1;
    201 
    202 	mutex_enter(ev->ev_lock);
    203 	if (ev->ev_get == ev->ev_put) {
    204 		rv = 0;
    205 	} else {
    206 		if (ev->ev_get < ev->ev_put)
    207 			kn->kn_data = ev->ev_put - ev->ev_get;
    208 		else
    209 			kn->kn_data = (EV_QSIZE - ev->ev_get) +
    210 			    ev->ev_put;
    211 
    212 		kn->kn_data *= sizeof(struct firm_event);
    213 	}
    214 	mutex_exit(ev->ev_lock);
    215 
    216 	return rv;
    217 }
    218 
    219 static const struct filterops ev_filtops = {
    220 	.f_flags = FILTEROP_ISFD,
    221 	.f_attach = NULL,
    222 	.f_detach = filt_evrdetach,
    223 	.f_event = filt_evread,
    224 };
    225 
    226 int
    227 ev_kqfilter(struct evvar *ev, struct knote *kn)
    228 {
    229 
    230 	switch (kn->kn_filter) {
    231 	case EVFILT_READ:
    232 		kn->kn_fop = &ev_filtops;
    233 		break;
    234 
    235 	default:
    236 		return EINVAL;
    237 	}
    238 
    239 	kn->kn_hook = ev;
    240 
    241 	mutex_enter(ev->ev_lock);
    242 	selrecord_knote(&ev->ev_sel, kn);
    243 	mutex_exit(ev->ev_lock);
    244 
    245 	return 0;
    246 }
    247