Home | History | Annotate | Line # | Download | only in sun
ms.c revision 1.20
      1 /*	$NetBSD: ms.c,v 1.20 2000/09/27 10:31:42 abs 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 
     72 #include <machine/vuid_event.h>
     73 
     74 #include <dev/ic/z8530reg.h>
     75 #include <machine/z8530var.h>
     76 #include <dev/sun/event_var.h>
     77 #include <dev/sun/msvar.h>
     78 
     79 #include "locators.h"
     80 
     81 cdev_decl(ms);	/* open, close, read, write, ioctl, stop, ... */
     82 
     83 extern struct cfdriver ms_cd;
     84 
     85 /****************************************************************
     86  *  Entry points for /dev/mouse
     87  *  (open,close,read,write,...)
     88  ****************************************************************/
     89 
     90 int
     91 msopen(dev, flags, mode, p)
     92 	dev_t dev;
     93 	int flags, mode;
     94 	struct proc *p;
     95 {
     96 	struct ms_softc *ms;
     97 	int unit;
     98 
     99 	unit = minor(dev);
    100 	if (unit >= ms_cd.cd_ndevs)
    101 		return (ENXIO);
    102 	ms = ms_cd.cd_devs[unit];
    103 	if (ms == NULL)
    104 		return (ENXIO);
    105 
    106 	/* This is an exclusive open device. */
    107 	if (ms->ms_events.ev_io)
    108 		return (EBUSY);
    109 
    110 	if (ms->ms_deviopen) {
    111 		int err;
    112 		err = (*ms->ms_deviopen)((struct device *)ms, flags);
    113 		if (err)
    114 			return (err);
    115 	}
    116 	ms->ms_events.ev_io = p;
    117 	ev_init(&ms->ms_events);	/* may cause sleep */
    118 
    119 	ms->ms_ready = 1;		/* start accepting events */
    120 	return (0);
    121 }
    122 
    123 int
    124 msclose(dev, flags, mode, p)
    125 	dev_t dev;
    126 	int flags, mode;
    127 	struct proc *p;
    128 {
    129 	struct ms_softc *ms;
    130 
    131 	ms = ms_cd.cd_devs[minor(dev)];
    132 	ms->ms_ready = 0;		/* stop accepting events */
    133 	ev_fini(&ms->ms_events);
    134 
    135 	ms->ms_events.ev_io = NULL;
    136 	if (ms->ms_deviclose) {
    137 		int err;
    138 		err = (*ms->ms_deviclose)((struct device *)ms, flags);
    139 		if (err)
    140 			return (err);
    141 	}
    142 	return (0);
    143 }
    144 
    145 int
    146 msread(dev, uio, flags)
    147 	dev_t dev;
    148 	struct uio *uio;
    149 	int flags;
    150 {
    151 	struct ms_softc *ms;
    152 
    153 	ms = ms_cd.cd_devs[minor(dev)];
    154 	return (ev_read(&ms->ms_events, uio, flags));
    155 }
    156 
    157 /* this routine should not exist, but is convenient to write here for now */
    158 int
    159 mswrite(dev, uio, flags)
    160 	dev_t dev;
    161 	struct uio *uio;
    162 	int flags;
    163 {
    164 
    165 	return (EOPNOTSUPP);
    166 }
    167 
    168 int
    169 msioctl(dev, cmd, data, flag, p)
    170 	dev_t dev;
    171 	u_long cmd;
    172 	caddr_t data;
    173 	int flag;
    174 	struct proc *p;
    175 {
    176 	struct ms_softc *ms;
    177 
    178 	ms = ms_cd.cd_devs[minor(dev)];
    179 
    180 	switch (cmd) {
    181 
    182 	case FIONBIO:		/* we will remove this someday (soon???) */
    183 		return (0);
    184 
    185 	case FIOASYNC:
    186 		ms->ms_events.ev_async = *(int *)data != 0;
    187 		return (0);
    188 
    189 	case TIOCSPGRP:
    190 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
    191 			return (EPERM);
    192 		return (0);
    193 
    194 	case VUIDGFORMAT:
    195 		/* we only do firm_events */
    196 		*(int *)data = VUID_FIRM_EVENT;
    197 		return (0);
    198 
    199 	case VUIDSFORMAT:
    200 		if (*(int *)data != VUID_FIRM_EVENT)
    201 			return (EINVAL);
    202 		return (0);
    203 	}
    204 	return (ENOTTY);
    205 }
    206 
    207 int
    208 mspoll(dev, events, p)
    209 	dev_t dev;
    210 	int events;
    211 	struct proc *p;
    212 {
    213 	struct ms_softc *ms;
    214 
    215 	ms = ms_cd.cd_devs[minor(dev)];
    216 	return (ev_poll(&ms->ms_events, events, p));
    217 }
    218 
    219 
    220 /****************************************************************
    221  * Middle layer (translator)
    222  ****************************************************************/
    223 
    224 /*
    225  * Called by our ms_softint() routine on input.
    226  */
    227 void
    228 ms_input(ms, c)
    229 	struct ms_softc *ms;
    230 	int c;
    231 {
    232 	struct firm_event *fe;
    233 	int mb, ub, d, get, put, any;
    234 	static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
    235 	static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
    236 
    237 	/*
    238 	 * Discard input if not ready.  Drop sync on parity or framing
    239 	 * error; gain sync on button byte.
    240 	 */
    241 	if (ms->ms_ready == 0)
    242 		return;
    243 	if (c == -1) {
    244 		ms->ms_byteno = -1;
    245 		return;
    246 	}
    247 	if ((c & ~0x0f) == 0x80) {	/* if in 0x80..0x8f */
    248 		if (c & 8) {
    249 			ms->ms_byteno = 1;	/* short form (3 bytes) */
    250 		} else {
    251 			ms->ms_byteno = 0;	/* long form (5 bytes) */
    252 		}
    253 	}
    254 
    255 	/*
    256 	 * Run the decode loop, adding to the current information.
    257 	 * We add, rather than replace, deltas, so that if the event queue
    258 	 * fills, we accumulate data for when it opens up again.
    259 	 */
    260 	switch (ms->ms_byteno) {
    261 
    262 	case -1:
    263 		return;
    264 
    265 	case 0:
    266 		/* buttons (long form) */
    267 		ms->ms_byteno = 2;
    268 		ms->ms_mb = (~c) & 0x7;
    269 		return;
    270 
    271 	case 1:
    272 		/* buttons (short form) */
    273 		ms->ms_byteno = 4;
    274 		ms->ms_mb = (~c) & 0x7;
    275 		return;
    276 
    277 	case 2:
    278 		/* first delta-x */
    279 		ms->ms_byteno = 3;
    280 		ms->ms_dx += (char)c;
    281 		return;
    282 
    283 	case 3:
    284 		/* first delta-y */
    285 		ms->ms_byteno = 4;
    286 		ms->ms_dy += (char)c;
    287 		return;
    288 
    289 	case 4:
    290 		/* second delta-x */
    291 		ms->ms_byteno = 5;
    292 		ms->ms_dx += (char)c;
    293 		return;
    294 
    295 	case 5:
    296 		/* second delta-y */
    297 		ms->ms_byteno = -1;	/* wait for button-byte again */
    298 		ms->ms_dy += (char)c;
    299 		break;
    300 
    301 	default:
    302 		panic("ms_rint");
    303 		/* NOTREACHED */
    304 	}
    305 
    306 	/*
    307 	 * We have at least one event (mouse button, delta-X, or
    308 	 * delta-Y; possibly all three, and possibly three separate
    309 	 * button events).  Deliver these events until we are out
    310 	 * of changes or out of room.  As events get delivered,
    311 	 * mark them `unchanged'.
    312 	 */
    313 	any = 0;
    314 	get = ms->ms_events.ev_get;
    315 	put = ms->ms_events.ev_put;
    316 	fe = &ms->ms_events.ev_q[put];
    317 
    318 	/* NEXT prepares to put the next event, backing off if necessary */
    319 #define	NEXT \
    320 	if ((++put) % EV_QSIZE == get) { \
    321 		put--; \
    322 		goto out; \
    323 	}
    324 	/* ADVANCE completes the `put' of the event */
    325 #define	ADVANCE \
    326 	fe++; \
    327 	if (put >= EV_QSIZE) { \
    328 		put = 0; \
    329 		fe = &ms->ms_events.ev_q[0]; \
    330 	} \
    331 	any = 1
    332 
    333 	mb = ms->ms_mb;
    334 	ub = ms->ms_ub;
    335 	while ((d = mb ^ ub) != 0) {
    336 		/*
    337 		 * Mouse button change.  Convert up to three changes
    338 		 * to the `first' change, and drop it into the event queue.
    339 		 */
    340 		NEXT;
    341 		d = to_one[d - 1];		/* from 1..7 to {1,2,4} */
    342 		fe->id = to_id[d - 1];		/* from {1,2,4} to ID */
    343 		fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
    344 		fe->time = time;
    345 		ADVANCE;
    346 		ub ^= d;
    347 	}
    348 	if (ms->ms_dx) {
    349 		NEXT;
    350 		fe->id = LOC_X_DELTA;
    351 		fe->value = ms->ms_dx;
    352 		fe->time = time;
    353 		ADVANCE;
    354 		ms->ms_dx = 0;
    355 	}
    356 	if (ms->ms_dy) {
    357 		NEXT;
    358 		fe->id = LOC_Y_DELTA;
    359 		fe->value = ms->ms_dy;
    360 		fe->time = time;
    361 		ADVANCE;
    362 		ms->ms_dy = 0;
    363 	}
    364 out:
    365 	if (any) {
    366 		ms->ms_ub = ub;
    367 		ms->ms_events.ev_put = put;
    368 		EV_WAKEUP(&ms->ms_events);
    369 	}
    370 }
    371