Home | History | Annotate | Line # | Download | only in dev
ms.c revision 1.10.8.1
      1 /*	$NetBSD: ms.c,v 1.10.8.1 2001/09/09 19:12:35 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Leo Weppelman.
      5  * All rights reserved.
      6  *
      7  * based on:
      8  *
      9  * Copyright (c) 1992, 1993
     10  *	The Regents of the University of California.  All rights reserved.
     11  *
     12  * This software was developed by the Computer Systems Engineering group
     13  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     14  * contributed to Berkeley.
     15  *
     16  * All advertising materials mentioning features or use of this software
     17  * must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Lawrence Berkeley Laboratory.
     20  *
     21  * Redistribution and use in source and binary forms, with or without
     22  * modification, are permitted provided that the following conditions
     23  * are met:
     24  * 1. Redistributions of source code must retain the above copyright
     25  *    notice, this list of conditions and the following disclaimer.
     26  * 2. Redistributions in binary form must reproduce the above copyright
     27  *    notice, this list of conditions and the following disclaimer in the
     28  *    documentation and/or other materials provided with the distribution.
     29  * 3. All advertising materials mentioning features or use of this software
     30  *    must display the following acknowledgement:
     31  *	This product includes software developed by the University of
     32  *	California, Berkeley and its contributors.
     33  * 4. Neither the name of the University nor the names of its contributors
     34  *    may be used to endorse or promote products derived from this software
     35  *    without specific prior written permission.
     36  *
     37  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     38  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     40  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     42  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     43  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     45  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     46  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     47  * SUCH DAMAGE.
     48  *
     49  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
     50  *
     51  * Header: ms.c,v 1.5 92/11/26 01:28:47 torek Exp  (LBL)
     52  */
     53 
     54 /*
     55  * Mouse driver.
     56  */
     57 
     58 #include <sys/param.h>
     59 #include <sys/conf.h>
     60 #include <sys/ioctl.h>
     61 #include <sys/kernel.h>
     62 #include <sys/proc.h>
     63 #include <sys/systm.h>
     64 #include <sys/callout.h>
     65 #include <sys/tty.h>
     66 #include <sys/signalvar.h>
     67 
     68 #include <machine/msioctl.h>
     69 #include <atari/dev/event_var.h>
     70 #include <atari/dev/vuid_event.h>
     71 #include <atari/dev/kbdvar.h>
     72 #include <atari/dev/msvar.h>
     73 
     74 #include "mouse.h"
     75 #if NMOUSE > 0
     76 
     77 /* there's really no more physical ports on an atari. */
     78 #if NMOUSE > 1
     79 #undef NMOUSE
     80 #define NMOUSE 1
     81 #endif
     82 
     83 typedef void	(*FPV) __P((void *));
     84 
     85 static struct ms_softc	ms_softc[NMOUSE];
     86 
     87 cdev_decl(ms);
     88 
     89 static	void	ms_3b_delay __P((struct ms_softc *));
     90 
     91 int
     92 mouseattach(cnt)
     93 	int cnt;
     94 {
     95 	printf("1 mouse configured\n");
     96 	ms_softc[0].ms_emul3b = 1;
     97 	callout_init(&ms_softc[0].ms_delay_ch);
     98 	return(NMOUSE);
     99 }
    100 
    101 static void
    102 ms_3b_delay(ms)
    103 struct ms_softc	*ms;
    104 {
    105 	REL_MOUSE	rel_ms;
    106 
    107 	rel_ms.id = TIMEOUT_ID;
    108 	rel_ms.dx = rel_ms.dy = 0;
    109 	mouse_soft(&rel_ms, sizeof(rel_ms), KBD_TIMEO_PKG);
    110 }
    111 /*
    112  * Note that we are called from the keyboard software interrupt!
    113  */
    114 void
    115 mouse_soft(rel_ms, size, type)
    116 REL_MOUSE	*rel_ms;
    117 int		size, type;
    118 {
    119 	struct ms_softc		*ms = &ms_softc[0];
    120 	struct firm_event	*fe, *fe2;
    121 	REL_MOUSE		fake_mouse;
    122 	int			get, put;
    123 	int			sps;
    124 	u_char			mbut, bmask;
    125 	int			flush_buttons;
    126 
    127 	if (ms->ms_events.ev_io == NULL)
    128 		return;
    129 
    130 	switch (type) {
    131 	    case KBD_JOY1_PKG:
    132 		/*
    133 		 * Ignore if in emulation mode
    134 		 */
    135 		if (ms->ms_emul3b)
    136 			return;
    137 
    138 		/*
    139 		 * There are some mice that have their middle button
    140 		 * wired to the 'up' bit of joystick 1....
    141 		 * Simulate a mouse packet with dx = dy = 0, the middle
    142 		 * button state set by UP and the other buttons unchanged.
    143 		 * Flush all button changes.
    144 		 */
    145 		flush_buttons = 1;
    146 		fake_mouse.id = (rel_ms->dx & 1 ? 4 : 0) | (ms->ms_buttons & 3);
    147 		fake_mouse.dx = fake_mouse.dy = 0;
    148 		rel_ms = &fake_mouse;
    149 		break;
    150 	    case KBD_TIMEO_PKG:
    151 		/*
    152 		 * Timeout package. No button changes and no movement.
    153 		 * Flush all button changes.
    154 		 */
    155 		flush_buttons = 1;
    156 		fake_mouse.id = ms->ms_buttons;
    157 		fake_mouse.dx = fake_mouse.dy = 0;
    158 		rel_ms = &fake_mouse;
    159 		break;
    160 	    case KBD_RMS_PKG:
    161 		/*
    162 		 * Normal mouse package. Always copy the middle button
    163 		 * status. The emulation code decides if button changes
    164 		 * must be flushed.
    165 		 */
    166 		rel_ms->id = (ms->ms_buttons & 4) | (rel_ms->id & 3);
    167 		flush_buttons = (ms->ms_emul3b) ? 0 : 1;
    168 		break;
    169 	    default:
    170 		return;
    171 	}
    172 
    173 	sps = splev();
    174 	get = ms->ms_events.ev_get;
    175 	put = ms->ms_events.ev_put;
    176 	fe  = &ms->ms_events.ev_q[put];
    177 
    178 	if ((type != KBD_TIMEO_PKG) && ms->ms_emul3b && ms->ms_bq_idx)
    179 		callout_stop(&ms->ms_delay_ch);
    180 
    181 	/*
    182 	 * Button states are encoded in the lower 3 bits of 'id'
    183 	 */
    184 	if (!(mbut = (rel_ms->id ^ ms->ms_buttons)) && (put != get)) {
    185 		/*
    186 		 * Compact dx/dy messages. Always generate an event when
    187 		 * a button is pressed or the event queue is empty.
    188 		 */
    189 		ms->ms_dx += rel_ms->dx;
    190 		ms->ms_dy += rel_ms->dy;
    191 		goto out;
    192 	}
    193 	rel_ms->dx += ms->ms_dx;
    194 	rel_ms->dy += ms->ms_dy;
    195 	ms->ms_dx = ms->ms_dy = 0;
    196 
    197 	/*
    198 	 * Output location events _before_ button events ie. make sure
    199 	 * the button is pressed at the correct location.
    200 	 */
    201 	if (rel_ms->dx) {
    202 		if ((++put) % EV_QSIZE == get) {
    203 			put--;
    204 			goto out;
    205 		}
    206 		fe->id    = LOC_X_DELTA;
    207 		fe->value = rel_ms->dx;
    208 		fe->time  = time;
    209 		if (put >= EV_QSIZE) {
    210 			put = 0;
    211 			fe  = &ms->ms_events.ev_q[0];
    212 		}
    213 		else fe++;
    214 	}
    215 	if (rel_ms->dy) {
    216 		if ((++put) % EV_QSIZE == get) {
    217 			put--;
    218 			goto out;
    219 		}
    220 		fe->id    = LOC_Y_DELTA;
    221 		fe->value = rel_ms->dy;
    222 		fe->time  = time;
    223 		if (put >= EV_QSIZE) {
    224 			put = 0;
    225 			fe  = &ms->ms_events.ev_q[0];
    226 		}
    227 		else fe++;
    228 	}
    229 	if (mbut && (type != KBD_TIMEO_PKG)) {
    230 		for (bmask = 1; bmask < 0x08; bmask <<= 1) {
    231 			if (!(mbut & bmask))
    232 				continue;
    233 			fe2 = &ms->ms_bq[ms->ms_bq_idx++];
    234 			if (bmask == 1)
    235 				fe2->id = MS_RIGHT;
    236 			else if (bmask == 2)
    237 				fe2->id = MS_LEFT;
    238 			else fe2->id = MS_MIDDLE;
    239 			fe2->value = rel_ms->id & bmask ? VKEY_DOWN : VKEY_UP;
    240 			fe2->time  = time;
    241 		}
    242 	}
    243 
    244 	/*
    245 	 * Handle 3rd button emulation.
    246 	 */
    247 	if (ms->ms_emul3b && ms->ms_bq_idx && (type != KBD_TIMEO_PKG)) {
    248 		/*
    249 		 * If the middle button is pressed, any change to
    250 		 * one of the other buttons releases all.
    251 		 */
    252 		if ((ms->ms_buttons & 4) && (mbut & 3)) {
    253 			ms->ms_bq[0].id = MS_MIDDLE;
    254 			ms->ms_bq_idx   = 1;
    255 			rel_ms->id      = 0;
    256 			flush_buttons   = 1;
    257 			goto out;
    258 		}
    259 	    	if (ms->ms_bq_idx == 2) {
    260 			if (ms->ms_bq[0].value == ms->ms_bq[1].value) {
    261 				/* Must be 2 button presses! */
    262 				ms->ms_bq[0].id = MS_MIDDLE;
    263 				ms->ms_bq_idx   = 1;
    264 				rel_ms->id      = 7;
    265 			}
    266 		}
    267 		else if (ms->ms_bq[0].value == VKEY_DOWN) {
    268 			callout_reset(&ms->ms_delay_ch, 10,
    269 			    (FPV)ms_3b_delay, (void *)ms);
    270 			goto out;
    271 		}
    272 		flush_buttons   = 1;
    273 	}
    274 out:
    275 	if (flush_buttons) {
    276 		int	i;
    277 
    278 		for (i = 0; i < ms->ms_bq_idx; i++) {
    279 			if ((++put) % EV_QSIZE == get) {
    280 				ms->ms_bq_idx = 0;
    281 				put--;
    282 				goto out;
    283 			}
    284 			*fe = ms->ms_bq[i];
    285 			if (put >= EV_QSIZE) {
    286 				put = 0;
    287 				fe  = &ms->ms_events.ev_q[0];
    288 			}
    289 			else fe++;
    290 		}
    291 		ms->ms_bq_idx = 0;
    292 	}
    293 	ms->ms_events.ev_put = put;
    294 	ms->ms_buttons       = rel_ms->id;
    295 	splx(sps);
    296 	EV_WAKEUP(&ms->ms_events);
    297 }
    298 
    299 int
    300 msopen(dev, flags, mode, p)
    301 dev_t		dev;
    302 int		flags, mode;
    303 struct proc	*p;
    304 {
    305 	u_char		report_ms_joy[] = { 0x14, 0x08 };
    306 	struct ms_softc	*ms;
    307 	int		unit;
    308 
    309 	unit = minor(dev);
    310 	ms   = &ms_softc[unit];
    311 
    312 	if (unit >= NMOUSE)
    313 		return(EXDEV);
    314 
    315 	if (ms->ms_events.ev_io)
    316 		return(EBUSY);
    317 
    318 	ms->ms_events.ev_io = p;
    319 	ms->ms_dx = ms->ms_dy = 0;
    320 	ms->ms_buttons = 0;
    321 	ms->ms_bq[0].id = ms->ms_bq[1].id = 0;
    322 	ms->ms_bq_idx = 0;
    323 	ev_init(&ms->ms_events);	/* may cause sleep */
    324 
    325 	/*
    326 	 * Enable mouse reporting.
    327 	 */
    328 	kbd_write(report_ms_joy, sizeof(report_ms_joy));
    329 	return(0);
    330 }
    331 
    332 int
    333 msclose(dev, flags, mode, p)
    334 dev_t		dev;
    335 int		flags, mode;
    336 struct proc	*p;
    337 {
    338 	u_char		disable_ms_joy[] = { 0x12, 0x1a };
    339 	int		unit;
    340 	struct ms_softc	*ms;
    341 
    342 	unit = minor (dev);
    343 	ms   = &ms_softc[unit];
    344 
    345 	/*
    346 	 * Turn off mouse interrogation.
    347 	 */
    348 	kbd_write(disable_ms_joy, sizeof(disable_ms_joy));
    349 	ev_fini(&ms->ms_events);
    350 	ms->ms_events.ev_io = NULL;
    351 	return(0);
    352 }
    353 
    354 int
    355 msread(dev, uio, flags)
    356 dev_t		dev;
    357 struct uio	*uio;
    358 int		flags;
    359 {
    360 	struct ms_softc *ms;
    361 
    362 	ms = &ms_softc[minor(dev)];
    363 	return(ev_read(&ms->ms_events, uio, flags));
    364 }
    365 
    366 int
    367 msioctl(dev, cmd, data, flag, p)
    368 dev_t			dev;
    369 u_long			cmd;
    370 register caddr_t 	data;
    371 int			flag;
    372 struct proc		*p;
    373 {
    374 	struct ms_softc *ms;
    375 	int		unit;
    376 
    377 	unit = minor(dev);
    378 	ms = &ms_softc[unit];
    379 
    380 	switch (cmd) {
    381 	case  MIOCS3B_EMUL:
    382 		ms->ms_emul3b = (*(int *)data != 0) ? 1 : 0;
    383 		return (0);
    384 	case  MIOCG3B_EMUL:
    385 		*(int *)data = ms->ms_emul3b;
    386 		return (0);
    387 	case FIONBIO:		/* we will remove this someday (soon???) */
    388 		return(0);
    389 	case FIOASYNC:
    390 		ms->ms_events.ev_async = *(int *)data != 0;
    391 		return(0);
    392 	case TIOCSPGRP:
    393 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
    394 			return(EPERM);
    395 		return(0);
    396 	case VUIDGFORMAT:	/* we only do firm_events */
    397 		*(int *)data = VUID_FIRM_EVENT;
    398 		return(0);
    399 	case VUIDSFORMAT:
    400 		if (*(int *)data != VUID_FIRM_EVENT)
    401 			return(EINVAL);
    402 		return(0);
    403 	}
    404 	return(ENOTTY);
    405 }
    406 
    407 int
    408 mspoll(dev, events, p)
    409 dev_t		dev;
    410 int		events;
    411 struct proc	*p;
    412 {
    413 	struct ms_softc *ms;
    414 
    415 	ms = &ms_softc[minor(dev)];
    416 	return(ev_poll(&ms->ms_events, events, p));
    417 }
    418 
    419 int
    420 mskqfilter(dev_t dev, struct knote *kn)
    421 {
    422 	struct ms_softc *ms;
    423 
    424 	ms = &ms_softc[minor(dev)];
    425 	return (ev_kqfilter(&ms->ms_events, kn));
    426 }
    427 #endif /* NMOUSE > 0 */
    428