Home | History | Annotate | Line # | Download | only in dev
ms.c revision 1.13
      1 /*	$NetBSD: ms.c,v 1.13 1996/10/13 03:07:26 christos Exp $	*/
      2 
      3 /*
      4  * based on:
      5  *
      6  * Copyright (c) 1992, 1993
      7  *	The Regents of the University of California.  All rights reserved.
      8  *
      9  * This software was developed by the Computer Systems Engineering group
     10  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     11  * contributed to Berkeley.
     12  *
     13  * All advertising materials mentioning features or use of this software
     14  * must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Lawrence Berkeley Laboratory.
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions
     20  * are met:
     21  * 1. Redistributions of source code must retain the above copyright
     22  *    notice, this list of conditions and the following disclaimer.
     23  * 2. Redistributions in binary form must reproduce the above copyright
     24  *    notice, this list of conditions and the following disclaimer in the
     25  *    documentation and/or other materials provided with the distribution.
     26  * 3. All advertising materials mentioning features or use of this software
     27  *    must display the following acknowledgement:
     28  *	This product includes software developed by the University of
     29  *	California, Berkeley and its contributors.
     30  * 4. Neither the name of the University nor the names of its contributors
     31  *    may be used to endorse or promote products derived from this software
     32  *    without specific prior written permission.
     33  *
     34  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     44  * SUCH DAMAGE.
     45  *
     46  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
     47  *
     48  * Header: ms.c,v 1.5 92/11/26 01:28:47 torek Exp  (LBL)
     49  */
     50 
     51 /*
     52  * Mouse driver.
     53  */
     54 
     55 #include <sys/param.h>
     56 #include <sys/device.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/kernel.h>
     59 #include <sys/proc.h>
     60 #include <sys/syslog.h>
     61 #include <sys/systm.h>
     62 #include <sys/tty.h>
     63 #include <sys/signalvar.h>
     64 
     65 #include <amiga/dev/event_var.h>
     66 #include <amiga/dev/vuid_event.h>
     67 
     68 #include <amiga/amiga/custom.h>
     69 #include <amiga/amiga/cia.h>
     70 #include <amiga/amiga/device.h>
     71 
     72 #include <sys/conf.h>
     73 #include <machine/conf.h>
     74 
     75 void msattach __P((struct device *, struct device *, void *));
     76 int msmatch __P((struct device *, void *, void *));
     77 
     78 void msintr __P((void *));
     79 void ms_enable __P((dev_t));
     80 void ms_disable __P((dev_t));
     81 
     82 struct ms_softc {
     83 	struct device sc_dev;
     84 
     85 	u_char	ms_horc;	   /* horizontal counter on last scan */
     86   	u_char	ms_verc;	   /* vertical counter on last scan */
     87 	char	ms_mb;		   /* mouse button state */
     88 	char	ms_ub;		   /* user button state */
     89 	int	ms_dx;		   /* delta-x */
     90 	int	ms_dy;		   /* delta-y */
     91 	volatile int ms_ready;	   /* event queue is ready */
     92 	struct	evvar ms_events;   /* event queue state */
     93 };
     94 
     95 struct cfattach ms_ca = {
     96 	sizeof(struct ms_softc), msmatch, msattach
     97 };
     98 
     99 struct cfdriver ms_cd = {
    100 	NULL, "ms", DV_DULL, NULL, 0
    101 };
    102 
    103 int
    104 msmatch(pdp, match, auxp)
    105 	struct device *pdp;
    106 	void *match, *auxp;
    107 {
    108 	struct cfdata *cfp = (struct cfdata *)match;
    109 
    110 	if (matchname((char *)auxp, "ms") &&
    111 	    cfp->cf_unit >= 0 && cfp->cf_unit <= 1) /* only two units */
    112 		return 1;
    113 
    114 	return 0;
    115 }
    116 
    117 void
    118 msattach(pdp, dp, auxp)
    119 	struct device *pdp, *dp;
    120 	void *auxp;
    121 {
    122 	printf("\n");
    123 }
    124 
    125 /*
    126  * Amiga mice are hooked up to one of the two "game" ports, where
    127  * the main mouse is usually on the first port, and port 2 can
    128  * be used by a joystick. Nevertheless, we support two mouse
    129  * devices, /dev/mouse0 and /dev/mouse1 (with a link of /dev/mouse to
    130  * the device that represents the port of the mouse in use).
    131  */
    132 
    133 /*
    134  * enable scanner, called when someone opens the device.
    135  * Assume caller already validated range of dev.
    136  */
    137 void
    138 ms_enable(dev)
    139 	dev_t dev;
    140 {
    141 	struct ms_softc *ms;
    142 
    143 	ms = (struct ms_softc *)getsoftc(ms_cd, minor(dev));
    144 
    145 	/*
    146 	 * use this as flag to the "interrupt" to tell it when to
    147 	 * shut off (when it's reset to 0).
    148 	 */
    149 	ms->ms_ready = 1;
    150 
    151 	timeout(msintr, (void *)minor(dev), 2);
    152 }
    153 
    154 /*
    155  * disable scanner. Just set ms_ready to 0, and after the next
    156  * timeout taken, no further timeouts will be initiated.
    157  */
    158 void
    159 ms_disable(dev)
    160 	dev_t dev;
    161 {
    162 	struct ms_softc *ms;
    163 	int s;
    164 
    165 	ms = (struct ms_softc *)getsoftc(ms_cd, minor(dev));
    166 	s = splhigh ();
    167 	ms->ms_ready = 0;
    168 	/*
    169 	 * sync with the interrupt
    170 	 */
    171 	tsleep(ms, PZERO - 1, "mouse-disable", 0);
    172 	splx(s);
    173 }
    174 
    175 
    176 /*
    177  * we're emulating a mousesystems serial mouse here..
    178  */
    179 void
    180 msintr(arg)
    181 	void *arg;
    182 {
    183 	static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
    184 	static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
    185 	struct ms_softc *ms;
    186 	struct firm_event *fe;
    187 	int mb, ub, d, get, put, any, unit;
    188 	u_char pra, *horc, *verc;
    189 	u_short pot, count;
    190 	short dx, dy;
    191 
    192 	unit = (int)arg;
    193 	ms = (struct ms_softc *)getsoftc(ms_cd, unit);
    194 
    195 	horc = ((u_char *) &count) + 1;
    196 	verc = (u_char *) &count;
    197 
    198 	/*
    199 	 * first read the three buttons.
    200 	 */
    201 	pot  = custom.potgor;
    202 	pra  = ciaa.pra;
    203 	pot >>= unit == 0 ? 8 : 12;	/* contains right and middle button */
    204 	pra >>= unit == 0 ? 6 : 7;	/* contains left button */
    205 	mb = (pot & 4) / 4 + (pot & 1) * 2 + (pra & 1) * 4;
    206 	mb ^= 0x07;
    207 
    208 	/*
    209 	 * read current values of counter registers
    210 	 */
    211 	if (unit == 0)
    212 		count = custom.joy0dat;
    213 	else
    214 		count = custom.joy1dat;
    215 
    216 	/*
    217 	 * take care of wraparound
    218 	 */
    219 	dx = *horc - ms->ms_horc;
    220 	if (dx < -127)
    221 		dx += 255;
    222 	else if (dx > 127)
    223 		dx -= 255;
    224 	dy = *verc - ms->ms_verc;
    225 	if (dy < -127)
    226 		dy += 255;
    227 	else if (dy > 127)
    228 		dy -= 255;
    229 
    230 	/*
    231 	 * remember current values for next scan
    232 	 */
    233 	ms->ms_horc = *horc;
    234 	ms->ms_verc = *verc;
    235 
    236 	ms->ms_dx = dx;
    237 	ms->ms_dy = dy;
    238 	ms->ms_mb = mb;
    239 
    240 	if (dx || dy || ms->ms_ub != ms->ms_mb) {
    241 		/*
    242 		 * We have at least one event (mouse button, delta-X, or
    243 		 * delta-Y; possibly all three, and possibly three separate
    244 		 * button events).  Deliver these events until we are out of
    245 		 * changes or out of room.  As events get delivered, mark them
    246 		 * `unchanged'.
    247 		 */
    248 		any = 0;
    249 		get = ms->ms_events.ev_get;
    250 		put = ms->ms_events.ev_put;
    251 		fe = &ms->ms_events.ev_q[put];
    252 
    253 		mb = ms->ms_mb;
    254 		ub = ms->ms_ub;
    255 		while ((d = mb ^ ub) != 0) {
    256 			/*
    257 			 * Mouse button change.  Convert up to three changes
    258 			 * to the `first' change, and drop it into the event
    259 			 * queue.
    260 			 */
    261 			if ((++put) % EV_QSIZE == get) {
    262 				put--;
    263 				goto out;
    264 			}
    265 
    266 			d = to_one[d - 1];	/* from 1..7 to {1,2,4} */
    267 			fe->id = to_id[d - 1];	/* from {1,2,4} to ID */
    268 			fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
    269 			fe->time = time;
    270 			fe++;
    271 
    272 			if (put >= EV_QSIZE) {
    273 				put = 0;
    274 				fe = &ms->ms_events.ev_q[0];
    275 			}
    276 			any = 1;
    277 
    278 			ub ^= d;
    279 		}
    280 		if (ms->ms_dx) {
    281 			if ((++put) % EV_QSIZE == get) {
    282 				put--;
    283 				goto out;
    284 			}
    285 
    286 			fe->id = LOC_X_DELTA;
    287 			fe->value = ms->ms_dx;
    288 			fe->time = time;
    289 			fe++;
    290 
    291 			if (put >= EV_QSIZE) {
    292 				put = 0;
    293 				fe = &ms->ms_events.ev_q[0];
    294 			}
    295 			any = 1;
    296 
    297 			ms->ms_dx = 0;
    298 		}
    299 		if (ms->ms_dy) {
    300 			if ((++put) % EV_QSIZE == get) {
    301 				put--;
    302 				goto out;
    303 			}
    304 
    305 			fe->id = LOC_Y_DELTA;
    306 			fe->value = ms->ms_dy;
    307 			fe->time = time;
    308 			fe++;
    309 
    310 			if (put >= EV_QSIZE) {
    311 				put = 0;
    312 				fe = &ms->ms_events.ev_q[0];
    313 			}
    314 			any = 1;
    315 
    316 			ms->ms_dy = 0;
    317 		}
    318 out:
    319 		if (any) {
    320 			ms->ms_ub = ub;
    321 			ms->ms_events.ev_put = put;
    322 			EV_WAKEUP(&ms->ms_events);
    323 		}
    324 	}
    325 
    326 	/*
    327 	 * reschedule handler, or if terminating,
    328 	 * handshake with ms_disable
    329 	 */
    330 	if (ms->ms_ready)
    331 		timeout(msintr, (void *)unit, 2);
    332 	else
    333 		wakeup(ms);
    334 }
    335 
    336 int
    337 msopen(dev, flags, mode, p)
    338 	dev_t dev;
    339 	int flags, mode;
    340 	struct proc *p;
    341 {
    342 	struct ms_softc *ms;
    343 	int unit;
    344 
    345 	unit = minor(dev);
    346 	ms = (struct ms_softc *)getsoftc(ms_cd, unit);
    347 
    348 	if (ms == NULL)
    349 		return(EXDEV);
    350 
    351 	if (ms->ms_events.ev_io)
    352 		return(EBUSY);
    353 
    354 	ms->ms_events.ev_io = p;
    355 	ev_init(&ms->ms_events);	/* may cause sleep */
    356 	ms_enable(dev);
    357 	return(0);
    358 }
    359 
    360 int
    361 msclose(dev, flags, mode, p)
    362 	dev_t dev;
    363 	int flags, mode;
    364 	struct proc *p;
    365 {
    366 	int unit;
    367 	struct ms_softc *ms;
    368 
    369 	unit = minor (dev);
    370 	ms = (struct ms_softc *)getsoftc(ms_cd, unit);
    371 
    372 	ms_disable(dev);
    373 	ev_fini(&ms->ms_events);
    374 	ms->ms_events.ev_io = NULL;
    375 	return(0);
    376 }
    377 
    378 int
    379 msread(dev, uio, flags)
    380 	dev_t dev;
    381 	struct uio *uio;
    382 	int flags;
    383 {
    384 	struct ms_softc *ms;
    385 
    386 	ms = (struct ms_softc *)getsoftc(ms_cd, minor(dev));
    387 
    388 	return(ev_read(&ms->ms_events, uio, flags));
    389 }
    390 
    391 int
    392 msioctl(dev, cmd, data, flag, p)
    393 	dev_t dev;
    394 	u_long cmd;
    395 	register caddr_t data;
    396 	int flag;
    397 	struct proc *p;
    398 {
    399 	struct ms_softc *ms;
    400 	int unit;
    401 
    402 	unit = minor(dev);
    403 	ms = (struct ms_softc *)getsoftc(ms_cd, unit);
    404 
    405 	switch (cmd) {
    406 	case FIONBIO:		/* we will remove this someday (soon???) */
    407 		return(0);
    408 	case FIOASYNC:
    409 		ms->ms_events.ev_async = *(int *)data != 0;
    410 		return(0);
    411 	case TIOCSPGRP:
    412 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
    413 			return(EPERM);
    414 		return(0);
    415 	case VUIDGFORMAT:	/* we only do firm_events */
    416 		*(int *)data = VUID_FIRM_EVENT;
    417 		return(0);
    418 	case VUIDSFORMAT:
    419 		if (*(int *)data != VUID_FIRM_EVENT)
    420 			return(EINVAL);
    421 		return(0);
    422 	}
    423 	return(ENOTTY);
    424 }
    425 
    426 int
    427 mspoll(dev, events, p)
    428 	dev_t dev;
    429 	int events;
    430 	struct proc *p;
    431 {
    432 	struct ms_softc *ms;
    433 
    434 	ms = (struct ms_softc *)getsoftc(ms_cd, minor(dev));
    435 
    436 	return(ev_poll(&ms->ms_events, events, p));
    437 }
    438