Home | History | Annotate | Line # | Download | only in sun
ms.c revision 1.20.6.2
      1 /*	$NetBSD: ms.c,v 1.20.6.2 2001/10/11 00:02:27 fvdl 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. All advertising materials mentioning features or use of this software
     25  *    must display the following acknowledgement:
     26  *	This product includes software developed by the University of
     27  *	California, Berkeley and its contributors.
     28  * 4. Neither the name of the University nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  *
     44  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
     45  */
     46 
     47 /*
     48  * Mouse driver (/dev/mouse)
     49  */
     50 
     51 /*
     52  * Zilog Z8530 Dual UART driver (mouse interface)
     53  *
     54  * This is the "slave" driver that will be attached to
     55  * the "zsc" driver for a Sun mouse.
     56  */
     57 
     58 #include <sys/param.h>
     59 #include <sys/systm.h>
     60 #include <sys/conf.h>
     61 #include <sys/device.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/kernel.h>
     64 #include <sys/proc.h>
     65 #include <sys/signal.h>
     66 #include <sys/signalvar.h>
     67 #include <sys/time.h>
     68 #include <sys/syslog.h>
     69 #include <sys/select.h>
     70 #include <sys/poll.h>
     71 #include <sys/vnode.h>
     72 
     73 #include <machine/vuid_event.h>
     74 
     75 #include <dev/ic/z8530reg.h>
     76 #include <machine/z8530var.h>
     77 #include <dev/sun/event_var.h>
     78 #include <dev/sun/msvar.h>
     79 
     80 #include "locators.h"
     81 
     82 cdev_decl(ms);	/* open, close, read, write, ioctl, stop, ... */
     83 
     84 extern struct cfdriver ms_cd;
     85 
     86 /****************************************************************
     87  *  Entry points for /dev/mouse
     88  *  (open,close,read,write,...)
     89  ****************************************************************/
     90 
     91 int
     92 msopen(devvp, flags, mode, p)
     93 	struct vnode *devvp;
     94 	int flags, mode;
     95 	struct proc *p;
     96 {
     97 	dev_t dev;
     98 	struct ms_softc *ms;
     99 	int unit;
    100 
    101 	dev = vdev_rdev(devvp);
    102 	unit = minor(dev);
    103 	if (unit >= ms_cd.cd_ndevs)
    104 		return (ENXIO);
    105 	ms = ms_cd.cd_devs[unit];
    106 	if (ms == NULL)
    107 		return (ENXIO);
    108 
    109 	/* This is an exclusive open device. */
    110 	if (ms->ms_events.ev_io)
    111 		return (EBUSY);
    112 
    113 	vdev_setprivdata(devvp, ms);
    114 
    115 	if (ms->ms_deviopen) {
    116 		int err;
    117 		err = (*ms->ms_deviopen)((struct device *)ms, flags, devvp);
    118 		if (err)
    119 			return (err);
    120 	}
    121 	ms->ms_events.ev_io = p;
    122 	ev_init(&ms->ms_events);	/* may cause sleep */
    123 
    124 	ms->ms_ready = 1;		/* start accepting events */
    125 	return (0);
    126 }
    127 
    128 int
    129 msclose(devvp, flags, mode, p)
    130 	struct vnode *devvp;
    131 	int flags, mode;
    132 	struct proc *p;
    133 {
    134 	struct ms_softc *ms;
    135 
    136 	ms = vdev_privdata(devvp);
    137 	ms->ms_ready = 0;		/* stop accepting events */
    138 	ev_fini(&ms->ms_events);
    139 
    140 	ms->ms_events.ev_io = NULL;
    141 	if (ms->ms_deviclose) {
    142 		int err;
    143 		err = (*ms->ms_deviclose)((struct device *)ms, flags, devvp);
    144 		if (err)
    145 			return (err);
    146 	}
    147 	return (0);
    148 }
    149 
    150 int
    151 msread(devvp, uio, flags)
    152 	struct vnode *devvp;
    153 	struct uio *uio;
    154 	int flags;
    155 {
    156 	struct ms_softc *ms;
    157 
    158 	ms = vdev_privdata(devvp);
    159 	return (ev_read(&ms->ms_events, uio, flags));
    160 }
    161 
    162 /* this routine should not exist, but is convenient to write here for now */
    163 int
    164 mswrite(devvp, uio, flags)
    165 	struct vnode *devvp;
    166 	struct uio *uio;
    167 	int flags;
    168 {
    169 
    170 	return (EOPNOTSUPP);
    171 }
    172 
    173 int
    174 msioctl(devvp, cmd, data, flag, p)
    175 	struct vnode *devvp;
    176 	u_long cmd;
    177 	caddr_t data;
    178 	int flag;
    179 	struct proc *p;
    180 {
    181 	struct ms_softc *ms;
    182 
    183 	ms = vdev_privdata(devvp);
    184 
    185 	switch (cmd) {
    186 
    187 	case FIONBIO:		/* we will remove this someday (soon???) */
    188 		return (0);
    189 
    190 	case FIOASYNC:
    191 		ms->ms_events.ev_async = *(int *)data != 0;
    192 		return (0);
    193 
    194 	case TIOCSPGRP:
    195 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
    196 			return (EPERM);
    197 		return (0);
    198 
    199 	case VUIDGFORMAT:
    200 		/* we only do firm_events */
    201 		*(int *)data = VUID_FIRM_EVENT;
    202 		return (0);
    203 
    204 	case VUIDSFORMAT:
    205 		if (*(int *)data != VUID_FIRM_EVENT)
    206 			return (EINVAL);
    207 		return (0);
    208 	}
    209 	return (ENOTTY);
    210 }
    211 
    212 int
    213 mspoll(devvp, events, p)
    214 	struct vnode *devvp;
    215 	int events;
    216 	struct proc *p;
    217 {
    218 	struct ms_softc *ms;
    219 
    220 	ms = vdev_privdata(devvp);
    221 	return (ev_poll(&ms->ms_events, events, p));
    222 }
    223 
    224 
    225 /****************************************************************
    226  * Middle layer (translator)
    227  ****************************************************************/
    228 
    229 /*
    230  * Called by our ms_softint() routine on input.
    231  */
    232 void
    233 ms_input(ms, c)
    234 	struct ms_softc *ms;
    235 	int c;
    236 {
    237 	struct firm_event *fe;
    238 	int mb, ub, d, get, put, any;
    239 	static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
    240 	static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
    241 
    242 	/*
    243 	 * Discard input if not ready.  Drop sync on parity or framing
    244 	 * error; gain sync on button byte.
    245 	 */
    246 	if (ms->ms_ready == 0)
    247 		return;
    248 	if (c == -1) {
    249 		ms->ms_byteno = -1;
    250 		return;
    251 	}
    252 	if ((c & ~0x0f) == 0x80) {	/* if in 0x80..0x8f */
    253 		if (c & 8) {
    254 			ms->ms_byteno = 1;	/* short form (3 bytes) */
    255 		} else {
    256 			ms->ms_byteno = 0;	/* long form (5 bytes) */
    257 		}
    258 	}
    259 
    260 	/*
    261 	 * Run the decode loop, adding to the current information.
    262 	 * We add, rather than replace, deltas, so that if the event queue
    263 	 * fills, we accumulate data for when it opens up again.
    264 	 */
    265 	switch (ms->ms_byteno) {
    266 
    267 	case -1:
    268 		return;
    269 
    270 	case 0:
    271 		/* buttons (long form) */
    272 		ms->ms_byteno = 2;
    273 		ms->ms_mb = (~c) & 0x7;
    274 		return;
    275 
    276 	case 1:
    277 		/* buttons (short form) */
    278 		ms->ms_byteno = 4;
    279 		ms->ms_mb = (~c) & 0x7;
    280 		return;
    281 
    282 	case 2:
    283 		/* first delta-x */
    284 		ms->ms_byteno = 3;
    285 		ms->ms_dx += (char)c;
    286 		return;
    287 
    288 	case 3:
    289 		/* first delta-y */
    290 		ms->ms_byteno = 4;
    291 		ms->ms_dy += (char)c;
    292 		return;
    293 
    294 	case 4:
    295 		/* second delta-x */
    296 		ms->ms_byteno = 5;
    297 		ms->ms_dx += (char)c;
    298 		return;
    299 
    300 	case 5:
    301 		/* second delta-y */
    302 		ms->ms_byteno = -1;	/* wait for button-byte again */
    303 		ms->ms_dy += (char)c;
    304 		break;
    305 
    306 	default:
    307 		panic("ms_rint");
    308 		/* NOTREACHED */
    309 	}
    310 
    311 	/*
    312 	 * We have at least one event (mouse button, delta-X, or
    313 	 * delta-Y; possibly all three, and possibly three separate
    314 	 * button events).  Deliver these events until we are out
    315 	 * of changes or out of room.  As events get delivered,
    316 	 * mark them `unchanged'.
    317 	 */
    318 	any = 0;
    319 	get = ms->ms_events.ev_get;
    320 	put = ms->ms_events.ev_put;
    321 	fe = &ms->ms_events.ev_q[put];
    322 
    323 	/* NEXT prepares to put the next event, backing off if necessary */
    324 #define	NEXT \
    325 	if ((++put) % EV_QSIZE == get) { \
    326 		put--; \
    327 		goto out; \
    328 	}
    329 	/* ADVANCE completes the `put' of the event */
    330 #define	ADVANCE \
    331 	fe++; \
    332 	if (put >= EV_QSIZE) { \
    333 		put = 0; \
    334 		fe = &ms->ms_events.ev_q[0]; \
    335 	} \
    336 	any = 1
    337 
    338 	mb = ms->ms_mb;
    339 	ub = ms->ms_ub;
    340 	while ((d = mb ^ ub) != 0) {
    341 		/*
    342 		 * Mouse button change.  Convert up to three changes
    343 		 * to the `first' change, and drop it into the event queue.
    344 		 */
    345 		NEXT;
    346 		d = to_one[d - 1];		/* from 1..7 to {1,2,4} */
    347 		fe->id = to_id[d - 1];		/* from {1,2,4} to ID */
    348 		fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
    349 		fe->time = time;
    350 		ADVANCE;
    351 		ub ^= d;
    352 	}
    353 	if (ms->ms_dx) {
    354 		NEXT;
    355 		fe->id = LOC_X_DELTA;
    356 		fe->value = ms->ms_dx;
    357 		fe->time = time;
    358 		ADVANCE;
    359 		ms->ms_dx = 0;
    360 	}
    361 	if (ms->ms_dy) {
    362 		NEXT;
    363 		fe->id = LOC_Y_DELTA;
    364 		fe->value = ms->ms_dy;
    365 		fe->time = time;
    366 		ADVANCE;
    367 		ms->ms_dy = 0;
    368 	}
    369 out:
    370 	if (any) {
    371 		ms->ms_ub = ub;
    372 		ms->ms_events.ev_put = put;
    373 		EV_WAKEUP(&ms->ms_events);
    374 	}
    375 }
    376