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