Home | History | Annotate | Line # | Download | only in dev
ms.c revision 1.4
      1  1.4  oki /*	$NetBSD: ms.c,v 1.4 1997/10/12 06:42:18 oki Exp $ */
      2  1.1  oki 
      3  1.1  oki /*
      4  1.1  oki  * Copyright (c) 1992, 1993
      5  1.1  oki  *	The Regents of the University of California.  All rights reserved.
      6  1.1  oki  *
      7  1.1  oki  * This software was developed by the Computer Systems Engineering group
      8  1.1  oki  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  1.1  oki  * contributed to Berkeley.
     10  1.1  oki  *
     11  1.1  oki  * All advertising materials mentioning features or use of this software
     12  1.1  oki  * must display the following acknowledgement:
     13  1.1  oki  *	This product includes software developed by the University of
     14  1.1  oki  *	California, Lawrence Berkeley Laboratory.
     15  1.1  oki  *
     16  1.1  oki  * Redistribution and use in source and binary forms, with or without
     17  1.1  oki  * modification, are permitted provided that the following conditions
     18  1.1  oki  * are met:
     19  1.1  oki  * 1. Redistributions of source code must retain the above copyright
     20  1.1  oki  *    notice, this list of conditions and the following disclaimer.
     21  1.1  oki  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1  oki  *    notice, this list of conditions and the following disclaimer in the
     23  1.1  oki  *    documentation and/or other materials provided with the distribution.
     24  1.1  oki  * 3. All advertising materials mentioning features or use of this software
     25  1.1  oki  *    must display the following acknowledgement:
     26  1.1  oki  *	This product includes software developed by the University of
     27  1.1  oki  *	California, Berkeley and its contributors.
     28  1.1  oki  * 4. Neither the name of the University nor the names of its contributors
     29  1.1  oki  *    may be used to endorse or promote products derived from this software
     30  1.1  oki  *    without specific prior written permission.
     31  1.1  oki  *
     32  1.1  oki  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  1.1  oki  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  1.1  oki  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  1.1  oki  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  1.1  oki  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  1.1  oki  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  1.1  oki  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  1.1  oki  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  1.1  oki  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  1.1  oki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  1.1  oki  * SUCH DAMAGE.
     43  1.1  oki  *
     44  1.1  oki  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
     45  1.1  oki  */
     46  1.1  oki 
     47  1.1  oki /*
     48  1.1  oki  * Mouse driver.
     49  1.1  oki  */
     50  1.1  oki 
     51  1.1  oki #include <sys/param.h>
     52  1.1  oki #include <sys/conf.h>
     53  1.1  oki #include <sys/ioctl.h>
     54  1.1  oki #include <sys/kernel.h>
     55  1.1  oki #include <sys/proc.h>
     56  1.1  oki #include <sys/syslog.h>
     57  1.1  oki #include <sys/systm.h>
     58  1.1  oki #include <sys/tty.h>
     59  1.1  oki 
     60  1.1  oki #include <x68k/dev/event_var.h>
     61  1.1  oki #include <machine/vuid_event.h>
     62  1.1  oki 
     63  1.1  oki #include <x68k/x68k/iodevice.h>
     64  1.1  oki 
     65  1.1  oki /*
     66  1.1  oki  * Mouse state.  A SHARP X1/X680x0 mouse is a fairly simple device,
     67  1.1  oki  * producing three-byte blobs of the form:
     68  1.1  oki  *
     69  1.1  oki  *	b dx dy
     70  1.1  oki  *
     71  1.1  oki  * where b is the button state, encoded as 0x80|(buttons)---there are
     72  1.1  oki  * two buttons (2=left, 1=right)---and dx,dy are X and Y delta values.
     73  1.1  oki  */
     74  1.1  oki struct ms_softc {
     75  1.1  oki 	short	ms_byteno;		/* input byte number, for decode */
     76  1.1  oki 	char	ms_mb;			/* mouse button state */
     77  1.1  oki 	char	ms_ub;			/* user button state */
     78  1.1  oki 	int	ms_dx;			/* delta-x */
     79  1.1  oki 	int	ms_dy;			/* delta-y */
     80  1.1  oki 	struct	tty *ms_mouse;		/* downlink for output to mouse */
     81  1.1  oki 	void	(*ms_open) __P((struct tty *));	/* enable dataflow */
     82  1.1  oki 	void	(*ms_close) __P((struct tty *));/* disable dataflow */
     83  1.1  oki 	volatile int ms_ready;		/* event queue is ready */
     84  1.1  oki 	struct	evvar ms_events;	/* event queue state */
     85  1.1  oki } ms_softc;
     86  1.1  oki 
     87  1.4  oki cdev_decl(ms);
     88  1.4  oki 
     89  1.4  oki void ms_serial __P((struct tty *, void(*)(struct tty *),
     90  1.4  oki 		    void(*)(struct tty *)));
     91  1.4  oki void ms_modem __P((int));
     92  1.4  oki void ms_rint __P((int));
     93  1.4  oki void mouseattach __P((void));
     94  1.4  oki 
     95  1.1  oki /*
     96  1.1  oki  * Attach the mouse serial (down-link) interface.
     97  1.1  oki  * Do we need to set it to 4800 baud, 8 bits?
     98  1.1  oki  * Test by power cycling and not booting Human68k before BSD?
     99  1.1  oki  */
    100  1.1  oki void
    101  1.1  oki ms_serial(tp, iopen, iclose)
    102  1.1  oki 	struct tty *tp;
    103  1.4  oki 	void (*iopen) __P((struct tty *));
    104  1.4  oki 	void (*iclose) __P((struct tty *));
    105  1.1  oki {
    106  1.1  oki 
    107  1.1  oki 	ms_softc.ms_mouse = tp;
    108  1.1  oki 	ms_softc.ms_open = iopen;
    109  1.1  oki 	ms_softc.ms_close = iclose;
    110  1.1  oki }
    111  1.1  oki 
    112  1.1  oki void
    113  1.1  oki ms_modem(onoff)
    114  1.1  oki 	register int onoff;
    115  1.1  oki {
    116  1.1  oki 	static int oonoff;
    117  1.1  oki 
    118  1.1  oki 	if (ms_softc.ms_ready == 1) {
    119  1.1  oki 		if (ms_softc.ms_byteno == -1)
    120  1.1  oki 			ms_softc.ms_byteno = onoff = 0;
    121  1.1  oki 		if (oonoff != onoff) {
    122  1.1  oki 			zs_msmodem(onoff);
    123  1.1  oki 			oonoff = onoff;
    124  1.1  oki 		}
    125  1.1  oki 	}
    126  1.1  oki }
    127  1.1  oki 
    128  1.1  oki void
    129  1.1  oki ms_rint(c)
    130  1.1  oki 	register int c;
    131  1.1  oki {
    132  1.1  oki 	register struct firm_event *fe;
    133  1.1  oki 	register struct ms_softc *ms = &ms_softc;
    134  1.1  oki 	register int mb, ub, d, get, put, any;
    135  1.1  oki 	static const char to_one[] = { 1, 2, 3 };
    136  1.1  oki 	static const int to_id[] = { MS_LEFT, MS_RIGHT, MS_MIDDLE };
    137  1.1  oki 
    138  1.1  oki 	/*
    139  1.1  oki 	 * Discard input if not ready.  Drop sync on parity or framing
    140  1.1  oki 	 * error; gain sync on button byte.
    141  1.1  oki 	 */
    142  1.1  oki 	if (ms->ms_ready == 0)
    143  1.1  oki 		return;
    144  1.1  oki 	if (c & (TTY_FE|TTY_PE)) {
    145  1.1  oki 		log(LOG_WARNING,
    146  1.1  oki 		    "mouse input parity or framing error (0x%x)\n", c);
    147  1.1  oki 		ms->ms_byteno = -1;
    148  1.1  oki 		return;
    149  1.1  oki 	}
    150  1.1  oki 
    151  1.1  oki 	/*
    152  1.1  oki 	 * Run the decode loop, adding to the current information.
    153  1.1  oki 	 * We add, rather than replace, deltas, so that if the event queue
    154  1.1  oki 	 * fills, we accumulate data for when it opens up again.
    155  1.1  oki 	 */
    156  1.1  oki 	switch (ms->ms_byteno) {
    157  1.1  oki 
    158  1.1  oki 	case -1:
    159  1.1  oki 		return;
    160  1.1  oki 
    161  1.1  oki 	case 0:
    162  1.1  oki 		/* buttons */
    163  1.1  oki 		ms->ms_byteno = 1;
    164  1.1  oki 		ms->ms_mb = c & 0x7;
    165  1.1  oki 		return;
    166  1.1  oki 
    167  1.1  oki 	case 1:
    168  1.1  oki 		/* delta-x */
    169  1.1  oki 		ms->ms_byteno = 2;
    170  1.1  oki 		ms->ms_dx += (char)c;
    171  1.1  oki 		return;
    172  1.1  oki 
    173  1.1  oki 	case 2:
    174  1.1  oki 		/* delta-y */
    175  1.1  oki 		ms->ms_byteno = -1	/* wait for button-byte again */;
    176  1.1  oki 		ms->ms_dy += (char)c;
    177  1.1  oki 		break;
    178  1.1  oki 
    179  1.1  oki 	default:
    180  1.1  oki 		panic("ms_rint");
    181  1.1  oki 		/* NOTREACHED */
    182  1.1  oki 	}
    183  1.1  oki 
    184  1.1  oki 	/*
    185  1.1  oki 	 * We have at least one event (mouse button, delta-X, or
    186  1.1  oki 	 * delta-Y; possibly all three, and possibly three separate
    187  1.1  oki 	 * button events).  Deliver these events until we are out
    188  1.1  oki 	 * of changes or out of room.  As events get delivered,
    189  1.1  oki 	 * mark them `unchanged'.
    190  1.1  oki 	 */
    191  1.1  oki 	any = 0;
    192  1.1  oki 	get = ms->ms_events.ev_get;
    193  1.1  oki 	put = ms->ms_events.ev_put;
    194  1.1  oki 	fe = &ms->ms_events.ev_q[put];
    195  1.1  oki 
    196  1.1  oki 	/* NEXT prepares to put the next event, backing off if necessary */
    197  1.1  oki #define	NEXT \
    198  1.1  oki 	if ((++put) % EV_QSIZE == get) { \
    199  1.1  oki 		put--; \
    200  1.1  oki 		goto out; \
    201  1.1  oki 	}
    202  1.1  oki 	/* ADVANCE completes the `put' of the event */
    203  1.1  oki #define	ADVANCE \
    204  1.1  oki 	fe++; \
    205  1.1  oki 	if (put >= EV_QSIZE) { \
    206  1.1  oki 		put = 0; \
    207  1.1  oki 		fe = &ms->ms_events.ev_q[0]; \
    208  1.1  oki 	} \
    209  1.1  oki 	any = 1
    210  1.1  oki 
    211  1.1  oki 	mb = ms->ms_mb;
    212  1.1  oki 	ub = ms->ms_ub;
    213  1.1  oki 	while ((d = mb ^ ub) != 0) {
    214  1.1  oki 		/*
    215  1.1  oki 		 * Mouse button change.  Convert up to three changes
    216  1.1  oki 		 * to the `first' change, and drop it into the event queue.
    217  1.1  oki 		 */
    218  1.1  oki 		NEXT;
    219  1.1  oki 		d = to_one[d - 1];		/* from 1..7 to {1,2,4} */
    220  1.1  oki 		fe->id = to_id[d - 1];		/* from {1,2,4} to ID */
    221  1.1  oki 		fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
    222  1.1  oki 		fe->time = time;
    223  1.1  oki 		ADVANCE;
    224  1.1  oki 		ub ^= d;
    225  1.1  oki 	}
    226  1.1  oki 	if (ms->ms_dx) {
    227  1.1  oki 		NEXT;
    228  1.1  oki 		fe->id = LOC_X_DELTA;
    229  1.1  oki 		fe->value = ms->ms_dx;
    230  1.1  oki 		fe->time = time;
    231  1.1  oki 		ADVANCE;
    232  1.1  oki 		ms->ms_dx = 0;
    233  1.1  oki 	}
    234  1.1  oki 	if (ms->ms_dy) {
    235  1.1  oki 		NEXT;
    236  1.1  oki 		fe->id = LOC_Y_DELTA;
    237  1.1  oki 		fe->value = -ms->ms_dy; /* XXX? */
    238  1.1  oki 		fe->time = time;
    239  1.1  oki 		ADVANCE;
    240  1.1  oki 		ms->ms_dy = 0;
    241  1.1  oki 	}
    242  1.1  oki out:
    243  1.1  oki 	if (any) {
    244  1.1  oki 		ms->ms_ub = ub;
    245  1.1  oki 		ms->ms_events.ev_put = put;
    246  1.1  oki 		EV_WAKEUP(&ms->ms_events);
    247  1.1  oki 	}
    248  1.1  oki }
    249  1.1  oki 
    250  1.1  oki int
    251  1.1  oki msopen(dev, flags, mode, p)
    252  1.1  oki 	dev_t dev;
    253  1.1  oki 	int flags, mode;
    254  1.1  oki 	struct proc *p;
    255  1.1  oki {
    256  1.1  oki 	if (ms_softc.ms_events.ev_io)
    257  1.1  oki 		return (EBUSY);
    258  1.1  oki 	ms_softc.ms_events.ev_io = p;
    259  1.1  oki 	ev_init(&ms_softc.ms_events);	/* may cause sleep */
    260  1.1  oki 	ms_softc.ms_ready = 1;		/* start accepting events */
    261  1.1  oki 	(*ms_softc.ms_open)(ms_softc.ms_mouse);
    262  1.1  oki 	return (0);
    263  1.1  oki }
    264  1.1  oki int
    265  1.1  oki msclose(dev, flags, mode, p)
    266  1.1  oki 	dev_t dev;
    267  1.1  oki 	int flags, mode;
    268  1.1  oki 	struct proc *p;
    269  1.1  oki {
    270  1.1  oki 
    271  1.1  oki 	ms_modem(0);
    272  1.1  oki 	ms_softc.ms_ready = 0;		/* stop accepting events */
    273  1.1  oki 	ev_fini(&ms_softc.ms_events);
    274  1.1  oki 	(*ms_softc.ms_close)(ms_softc.ms_mouse);
    275  1.1  oki 	ms_softc.ms_events.ev_io = NULL;
    276  1.1  oki 	return (0);
    277  1.1  oki }
    278  1.1  oki 
    279  1.1  oki int
    280  1.1  oki msread(dev, uio, flags)
    281  1.1  oki 	dev_t dev;
    282  1.1  oki 	struct uio *uio;
    283  1.1  oki 	int flags;
    284  1.1  oki {
    285  1.1  oki 
    286  1.1  oki 	return (ev_read(&ms_softc.ms_events, uio, flags));
    287  1.1  oki }
    288  1.1  oki 
    289  1.1  oki /* this routine should not exist, but is convenient to write here for now */
    290  1.1  oki int
    291  1.1  oki mswrite(dev, uio, flags)
    292  1.1  oki 	dev_t dev;
    293  1.1  oki 	struct uio *uio;
    294  1.1  oki 	int flags;
    295  1.1  oki {
    296  1.1  oki 
    297  1.1  oki 	return (EOPNOTSUPP);
    298  1.1  oki }
    299  1.1  oki 
    300  1.1  oki int
    301  1.1  oki msioctl(dev, cmd, data, flag, p)
    302  1.1  oki 	dev_t dev;
    303  1.1  oki 	u_long cmd;
    304  1.1  oki 	register caddr_t data;
    305  1.1  oki 	int flag;
    306  1.1  oki 	struct proc *p;
    307  1.1  oki {
    308  1.1  oki 	switch (cmd) {
    309  1.1  oki 
    310  1.1  oki 	case FIONBIO:		/* we will remove this someday (soon???) */
    311  1.1  oki 		return (0);
    312  1.1  oki 
    313  1.1  oki 	case FIOASYNC:
    314  1.1  oki 		ms_softc.ms_events.ev_async = *(int *)data != 0;
    315  1.1  oki 		return (0);
    316  1.1  oki 
    317  1.1  oki 	case TIOCSPGRP:
    318  1.1  oki 		if (*(int *)data != ms_softc.ms_events.ev_io->p_pgid)
    319  1.1  oki 			return (EPERM);
    320  1.1  oki 		return (0);
    321  1.1  oki 
    322  1.1  oki 	case VUIDGFORMAT:
    323  1.1  oki 		/* we only do firm_events */
    324  1.1  oki 		*(int *)data = VUID_FIRM_EVENT;
    325  1.1  oki 		return (0);
    326  1.1  oki 
    327  1.1  oki 	case VUIDSFORMAT:
    328  1.1  oki 		if (*(int *)data != VUID_FIRM_EVENT)
    329  1.1  oki 			return (EINVAL);
    330  1.1  oki 		return (0);
    331  1.1  oki 	}
    332  1.1  oki 	return (ENOTTY);
    333  1.1  oki }
    334  1.1  oki 
    335  1.1  oki int
    336  1.3  oki mspoll(dev, events, p)
    337  1.1  oki 	dev_t dev;
    338  1.3  oki 	int events;
    339  1.1  oki 	struct proc *p;
    340  1.1  oki {
    341  1.1  oki 
    342  1.3  oki 	return (ev_poll(&ms_softc.ms_events, events, p));
    343  1.1  oki }
    344  1.1  oki 
    345  1.2  oki void
    346  1.1  oki mouseattach(){} /* XXX pseudo-device */
    347