Home | History | Annotate | Line # | Download | only in wscons
wsevent.c revision 1.4
      1 /* $NetBSD: wsevent.c,v 1.4 2000/01/05 11:19:37 drochner 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 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: wsevent.c,v 1.4 2000/01/05 11:19:37 drochner Exp $");
     35 
     36 /*
     37  * Copyright (c) 1992, 1993
     38  *	The Regents of the University of California.  All rights reserved.
     39  *
     40  * This software was developed by the Computer Systems Engineering group
     41  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     42  * contributed to Berkeley.
     43  *
     44  * All advertising materials mentioning features or use of this software
     45  * must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Lawrence Berkeley Laboratory.
     48  *
     49  * Redistribution and use in source and binary forms, with or without
     50  * modification, are permitted provided that the following conditions
     51  * are met:
     52  * 1. Redistributions of source code must retain the above copyright
     53  *    notice, this list of conditions and the following disclaimer.
     54  * 2. Redistributions in binary form must reproduce the above copyright
     55  *    notice, this list of conditions and the following disclaimer in the
     56  *    documentation and/or other materials provided with the distribution.
     57  * 3. All advertising materials mentioning features or use of this software
     58  *    must display the following acknowledgement:
     59  *	This product includes software developed by the University of
     60  *	California, Berkeley and its contributors.
     61  * 4. Neither the name of the University nor the names of its contributors
     62  *    may be used to endorse or promote products derived from this software
     63  *    without specific prior written permission.
     64  *
     65  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     66  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     67  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     68  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     69  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     70  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     71  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     72  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     73  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     74  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     75  * SUCH DAMAGE.
     76  *
     77  *	@(#)event.c	8.1 (Berkeley) 6/11/93
     78  */
     79 
     80 /*
     81  * Internal "wscons_event" queue interface for the keyboard and mouse drivers.
     82  */
     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(ev)
    101 	register struct wseventvar *ev;
    102 {
    103 
    104 	ev->get = ev->put = 0;
    105 	ev->q = malloc((u_long)WSEVENT_QSIZE * sizeof(struct wscons_event),
    106 	    M_DEVBUF, M_WAITOK);
    107 	memset((caddr_t)ev->q, 0, WSEVENT_QSIZE * sizeof(struct wscons_event));
    108 }
    109 
    110 /*
    111  * Tear down a wscons_event queue.
    112  */
    113 void
    114 wsevent_fini(ev)
    115 	register struct wseventvar *ev;
    116 {
    117 
    118 	free(ev->q, M_DEVBUF);
    119 }
    120 
    121 /*
    122  * User-level interface: read, poll.
    123  * (User cannot write an event queue.)
    124  */
    125 int
    126 wsevent_read(ev, uio, flags)
    127 	register struct wseventvar *ev;
    128 	struct uio *uio;
    129 	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((caddr_t)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 
    183 int
    184 wsevent_poll(ev, events, p)
    185 	register struct wseventvar *ev;
    186 	int events;
    187 	struct proc *p;
    188 {
    189 	int revents = 0;
    190 	int s = splwsevent();
    191 
    192         if (events & (POLLIN | POLLRDNORM)) {
    193 		if (ev->get != ev->put)
    194 			revents |= events & (POLLIN | POLLRDNORM);
    195 		else
    196 			selrecord(p, &ev->sel);
    197 	}
    198 
    199 	splx(s);
    200 	return (revents);
    201 }
    202