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