Home | History | Annotate | Line # | Download | only in dev
ms.c revision 1.16
      1 /*	$NetBSD: ms.c,v 1.16 2000/03/23 06:33:11 thorpej 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/callout.h>
     63 #include <sys/tty.h>
     64 #include <sys/signalvar.h>
     65 
     66 #include <amiga/dev/event_var.h>
     67 #include <amiga/dev/vuid_event.h>
     68 
     69 #include <amiga/amiga/custom.h>
     70 #include <amiga/amiga/cia.h>
     71 #include <amiga/amiga/device.h>
     72 
     73 #include <sys/conf.h>
     74 #include <machine/conf.h>
     75 
     76 void msattach __P((struct device *, struct device *, void *));
     77 int msmatch __P((struct device *, struct cfdata *, void *));
     78 
     79 void msintr __P((void *));
     80 void ms_enable __P((dev_t));
     81 void ms_disable __P((dev_t));
     82 
     83 struct ms_softc {
     84 	struct device sc_dev;
     85 
     86 	struct callout sc_intr_ch;
     87 
     88 	u_char	ms_horc;	   /* horizontal counter on last scan */
     89   	u_char	ms_verc;	   /* vertical counter on last scan */
     90 	char	ms_mb;		   /* mouse button state */
     91 	char	ms_ub;		   /* user button state */
     92 	int	ms_dx;		   /* delta-x */
     93 	int	ms_dy;		   /* delta-y */
     94 	volatile int ms_ready;	   /* event queue is ready */
     95 	struct	evvar ms_events;   /* event queue state */
     96 };
     97 
     98 struct cfattach ms_ca = {
     99 	sizeof(struct ms_softc), msmatch, msattach
    100 };
    101 
    102 extern struct cfdriver ms_cd;
    103 
    104 int
    105 msmatch(pdp, cfp, auxp)
    106 	struct device *pdp;
    107 	struct cfdata *cfp;
    108 	void *auxp;
    109 {
    110 
    111 	if (matchname((char *)auxp, "ms") &&
    112 	    cfp->cf_unit >= 0 && cfp->cf_unit <= 1) /* only two units */
    113 		return 1;
    114 
    115 	return 0;
    116 }
    117 
    118 void
    119 msattach(pdp, dp, auxp)
    120 	struct device *pdp, *dp;
    121 	void *auxp;
    122 {
    123 	struct ms_softc *sc = (void *) dp;
    124 
    125 	printf("\n");
    126 	callout_init(&sc->sc_intr_ch);
    127 }
    128 
    129 /*
    130  * Amiga mice are hooked up to one of the two "game" ports, where
    131  * the main mouse is usually on the first port, and port 2 can
    132  * be used by a joystick. Nevertheless, we support two mouse
    133  * devices, /dev/mouse0 and /dev/mouse1 (with a link of /dev/mouse to
    134  * the device that represents the port of the mouse in use).
    135  */
    136 
    137 /*
    138  * enable scanner, called when someone opens the device.
    139  * Assume caller already validated range of dev.
    140  */
    141 void
    142 ms_enable(dev)
    143 	dev_t dev;
    144 {
    145 	struct ms_softc *ms;
    146 
    147 	ms = (struct ms_softc *)getsoftc(ms_cd, minor(dev));
    148 
    149 	/*
    150 	 * use this as flag to the "interrupt" to tell it when to
    151 	 * shut off (when it's reset to 0).
    152 	 */
    153 	ms->ms_ready = 1;
    154 
    155 	callout_reset(&ms->sc_intr_ch, 2, msintr, ms);
    156 }
    157 
    158 /*
    159  * disable scanner. Just set ms_ready to 0, and after the next
    160  * timeout taken, no further timeouts will be initiated.
    161  */
    162 void
    163 ms_disable(dev)
    164 	dev_t dev;
    165 {
    166 	struct ms_softc *ms;
    167 	int s;
    168 
    169 	ms = (struct ms_softc *)getsoftc(ms_cd, minor(dev));
    170 	s = splhigh ();
    171 	ms->ms_ready = 0;
    172 	/*
    173 	 * sync with the interrupt
    174 	 */
    175 	tsleep(ms, PZERO - 1, "mouse-disable", 0);
    176 	splx(s);
    177 }
    178 
    179 
    180 /*
    181  * we're emulating a mousesystems serial mouse here..
    182  */
    183 void
    184 msintr(arg)
    185 	void *arg;
    186 {
    187 	static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
    188 	static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
    189 	struct ms_softc *ms = arg;
    190 	struct firm_event *fe;
    191 	int mb, ub, d, get, put, any, unit;
    192 	u_char pra, *horc, *verc;
    193 	u_short pot, count;
    194 	short dx, dy;
    195 
    196 	unit = ms->sc_dev.dv_unit;
    197 
    198 	horc = ((u_char *) &count) + 1;
    199 	verc = (u_char *) &count;
    200 
    201 	/*
    202 	 * first read the three buttons.
    203 	 */
    204 	pot  = custom.potgor;
    205 	pra  = ciaa.pra;
    206 	pot >>= unit == 0 ? 8 : 12;	/* contains right and middle button */
    207 	pra >>= unit == 0 ? 6 : 7;	/* contains left button */
    208 	mb = (pot & 4) / 4 + (pot & 1) * 2 + (pra & 1) * 4;
    209 	mb ^= 0x07;
    210 
    211 	/*
    212 	 * read current values of counter registers
    213 	 */
    214 	if (unit == 0)
    215 		count = custom.joy0dat;
    216 	else
    217 		count = custom.joy1dat;
    218 
    219 	/*
    220 	 * take care of wraparound
    221 	 */
    222 	dx = *horc - ms->ms_horc;
    223 	if (dx < -127)
    224 		dx += 255;
    225 	else if (dx > 127)
    226 		dx -= 255;
    227 	dy = *verc - ms->ms_verc;
    228 	if (dy < -127)
    229 		dy += 255;
    230 	else if (dy > 127)
    231 		dy -= 255;
    232 
    233 	/*
    234 	 * remember current values for next scan
    235 	 */
    236 	ms->ms_horc = *horc;
    237 	ms->ms_verc = *verc;
    238 
    239 	ms->ms_dx = dx;
    240 	ms->ms_dy = dy;
    241 	ms->ms_mb = mb;
    242 
    243 	if (dx || dy || ms->ms_ub != ms->ms_mb) {
    244 		/*
    245 		 * We have at least one event (mouse button, delta-X, or
    246 		 * delta-Y; possibly all three, and possibly three separate
    247 		 * button events).  Deliver these events until we are out of
    248 		 * changes or out of room.  As events get delivered, mark them
    249 		 * `unchanged'.
    250 		 */
    251 		any = 0;
    252 		get = ms->ms_events.ev_get;
    253 		put = ms->ms_events.ev_put;
    254 		fe = &ms->ms_events.ev_q[put];
    255 
    256 		mb = ms->ms_mb;
    257 		ub = ms->ms_ub;
    258 		while ((d = mb ^ ub) != 0) {
    259 			/*
    260 			 * Mouse button change.  Convert up to three changes
    261 			 * to the `first' change, and drop it into the event
    262 			 * queue.
    263 			 */
    264 			if ((++put) % EV_QSIZE == get) {
    265 				put--;
    266 				goto out;
    267 			}
    268 
    269 			d = to_one[d - 1];	/* from 1..7 to {1,2,4} */
    270 			fe->id = to_id[d - 1];	/* from {1,2,4} to ID */
    271 			fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
    272 			fe->time = time;
    273 			fe++;
    274 
    275 			if (put >= EV_QSIZE) {
    276 				put = 0;
    277 				fe = &ms->ms_events.ev_q[0];
    278 			}
    279 			any = 1;
    280 
    281 			ub ^= d;
    282 		}
    283 		if (ms->ms_dx) {
    284 			if ((++put) % EV_QSIZE == get) {
    285 				put--;
    286 				goto out;
    287 			}
    288 
    289 			fe->id = LOC_X_DELTA;
    290 			fe->value = ms->ms_dx;
    291 			fe->time = time;
    292 			fe++;
    293 
    294 			if (put >= EV_QSIZE) {
    295 				put = 0;
    296 				fe = &ms->ms_events.ev_q[0];
    297 			}
    298 			any = 1;
    299 
    300 			ms->ms_dx = 0;
    301 		}
    302 		if (ms->ms_dy) {
    303 			if ((++put) % EV_QSIZE == get) {
    304 				put--;
    305 				goto out;
    306 			}
    307 
    308 			fe->id = LOC_Y_DELTA;
    309 			fe->value = ms->ms_dy;
    310 			fe->time = time;
    311 			fe++;
    312 
    313 			if (put >= EV_QSIZE) {
    314 				put = 0;
    315 				fe = &ms->ms_events.ev_q[0];
    316 			}
    317 			any = 1;
    318 
    319 			ms->ms_dy = 0;
    320 		}
    321 out:
    322 		if (any) {
    323 			ms->ms_ub = ub;
    324 			ms->ms_events.ev_put = put;
    325 			EV_WAKEUP(&ms->ms_events);
    326 		}
    327 	}
    328 
    329 	/*
    330 	 * reschedule handler, or if terminating,
    331 	 * handshake with ms_disable
    332 	 */
    333 	if (ms->ms_ready)
    334 		callout_reset(&ms->sc_intr_ch, 2, msintr, ms);
    335 	else
    336 		wakeup(ms);
    337 }
    338 
    339 int
    340 msopen(dev, flags, mode, p)
    341 	dev_t dev;
    342 	int flags, mode;
    343 	struct proc *p;
    344 {
    345 	struct ms_softc *ms;
    346 	int unit;
    347 
    348 	unit = minor(dev);
    349 	ms = (struct ms_softc *)getsoftc(ms_cd, unit);
    350 
    351 	if (ms == NULL)
    352 		return(EXDEV);
    353 
    354 	if (ms->ms_events.ev_io)
    355 		return(EBUSY);
    356 
    357 	ms->ms_events.ev_io = p;
    358 	ev_init(&ms->ms_events);	/* may cause sleep */
    359 	ms_enable(dev);
    360 	return(0);
    361 }
    362 
    363 int
    364 msclose(dev, flags, mode, p)
    365 	dev_t dev;
    366 	int flags, mode;
    367 	struct proc *p;
    368 {
    369 	int unit;
    370 	struct ms_softc *ms;
    371 
    372 	unit = minor (dev);
    373 	ms = (struct ms_softc *)getsoftc(ms_cd, unit);
    374 
    375 	ms_disable(dev);
    376 	ev_fini(&ms->ms_events);
    377 	ms->ms_events.ev_io = NULL;
    378 	return(0);
    379 }
    380 
    381 int
    382 msread(dev, uio, flags)
    383 	dev_t dev;
    384 	struct uio *uio;
    385 	int flags;
    386 {
    387 	struct ms_softc *ms;
    388 
    389 	ms = (struct ms_softc *)getsoftc(ms_cd, minor(dev));
    390 
    391 	return(ev_read(&ms->ms_events, uio, flags));
    392 }
    393 
    394 int
    395 msioctl(dev, cmd, data, flag, p)
    396 	dev_t dev;
    397 	u_long cmd;
    398 	register caddr_t data;
    399 	int flag;
    400 	struct proc *p;
    401 {
    402 	struct ms_softc *ms;
    403 	int unit;
    404 
    405 	unit = minor(dev);
    406 	ms = (struct ms_softc *)getsoftc(ms_cd, unit);
    407 
    408 	switch (cmd) {
    409 	case FIONBIO:		/* we will remove this someday (soon???) */
    410 		return(0);
    411 	case FIOASYNC:
    412 		ms->ms_events.ev_async = *(int *)data != 0;
    413 		return(0);
    414 	case TIOCSPGRP:
    415 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
    416 			return(EPERM);
    417 		return(0);
    418 	case VUIDGFORMAT:	/* we only do firm_events */
    419 		*(int *)data = VUID_FIRM_EVENT;
    420 		return(0);
    421 	case VUIDSFORMAT:
    422 		if (*(int *)data != VUID_FIRM_EVENT)
    423 			return(EINVAL);
    424 		return(0);
    425 	}
    426 	return(ENOTTY);
    427 }
    428 
    429 int
    430 mspoll(dev, events, p)
    431 	dev_t dev;
    432 	int events;
    433 	struct proc *p;
    434 {
    435 	struct ms_softc *ms;
    436 
    437 	ms = (struct ms_softc *)getsoftc(ms_cd, minor(dev));
    438 
    439 	return(ev_poll(&ms->ms_events, events, p));
    440 }
    441