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