Home | History | Annotate | Line # | Download | only in dev
ms.c revision 1.1
      1  1.1  mw /*
      2  1.1  mw  * based on:
      3  1.1  mw  *
      4  1.1  mw  * Copyright (c) 1992, 1993
      5  1.1  mw  *	The Regents of the University of California.  All rights reserved.
      6  1.1  mw  *
      7  1.1  mw  * This software was developed by the Computer Systems Engineering group
      8  1.1  mw  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  1.1  mw  * contributed to Berkeley.
     10  1.1  mw  *
     11  1.1  mw  * All advertising materials mentioning features or use of this software
     12  1.1  mw  * must display the following acknowledgement:
     13  1.1  mw  *	This product includes software developed by the University of
     14  1.1  mw  *	California, Lawrence Berkeley Laboratory.
     15  1.1  mw  *
     16  1.1  mw  * Redistribution and use in source and binary forms, with or without
     17  1.1  mw  * modification, are permitted provided that the following conditions
     18  1.1  mw  * are met:
     19  1.1  mw  * 1. Redistributions of source code must retain the above copyright
     20  1.1  mw  *    notice, this list of conditions and the following disclaimer.
     21  1.1  mw  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1  mw  *    notice, this list of conditions and the following disclaimer in the
     23  1.1  mw  *    documentation and/or other materials provided with the distribution.
     24  1.1  mw  * 3. All advertising materials mentioning features or use of this software
     25  1.1  mw  *    must display the following acknowledgement:
     26  1.1  mw  *	This product includes software developed by the University of
     27  1.1  mw  *	California, Berkeley and its contributors.
     28  1.1  mw  * 4. Neither the name of the University nor the names of its contributors
     29  1.1  mw  *    may be used to endorse or promote products derived from this software
     30  1.1  mw  *    without specific prior written permission.
     31  1.1  mw  *
     32  1.1  mw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  1.1  mw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  1.1  mw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  1.1  mw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  1.1  mw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  1.1  mw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  1.1  mw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  1.1  mw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  1.1  mw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  1.1  mw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  1.1  mw  * SUCH DAMAGE.
     43  1.1  mw  *
     44  1.1  mw  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
     45  1.1  mw  *
     46  1.1  mw  * from: Header: ms.c,v 1.5 92/11/26 01:28:47 torek Exp  (LBL)
     47  1.1  mw  * $Id: ms.c,v 1.1 1994/01/26 21:06:05 mw Exp $
     48  1.1  mw  */
     49  1.1  mw 
     50  1.1  mw /*
     51  1.1  mw  * Mouse driver.
     52  1.1  mw  */
     53  1.1  mw 
     54  1.1  mw #include "param.h"
     55  1.1  mw #include "conf.h"
     56  1.1  mw #include "ioctl.h"
     57  1.1  mw #include "kernel.h"
     58  1.1  mw #include "proc.h"
     59  1.1  mw #include "syslog.h"
     60  1.1  mw #include "systm.h"
     61  1.1  mw #include "tty.h"
     62  1.1  mw 
     63  1.1  mw #include "event_var.h"
     64  1.1  mw #include "vuid_event.h"
     65  1.1  mw 
     66  1.1  mw #include "../amiga/custom.h"
     67  1.1  mw #include "../amiga/cia.h"
     68  1.1  mw 
     69  1.1  mw #include "mouse.h"
     70  1.1  mw #if NMOUSE > 0
     71  1.1  mw 
     72  1.1  mw /* there's really no more physical ports on an amiga.. */
     73  1.1  mw #if NMOUSE > 2
     74  1.1  mw #undef NMOUSE
     75  1.1  mw #define NMOUSE 2
     76  1.1  mw #endif
     77  1.1  mw 
     78  1.1  mw void msintr (int unit);
     79  1.1  mw 
     80  1.1  mw /* Amiga mice are hooked up to one of the two "game" ports, where
     81  1.1  mw    the main mouse is usually on the first port, and port 2 can
     82  1.1  mw    be used by a joystick. Nevertheless, we support two mouse
     83  1.1  mw    devices, /dev/mouse0 and /dev/mouse1 (with a link of /dev/mouse to
     84  1.1  mw    the device that represents the port of the mouse in use). */
     85  1.1  mw 
     86  1.1  mw struct ms_softc {
     87  1.1  mw 	u_char	ms_horc;	   /* horizontal counter on last scan */
     88  1.1  mw   	u_char	ms_verc;	   /* vertical counter on last scan */
     89  1.1  mw 	char	ms_mb;		   /* mouse button state */
     90  1.1  mw 	char	ms_ub;		   /* user button state */
     91  1.1  mw 	int	ms_dx;		   /* delta-x */
     92  1.1  mw 	int	ms_dy;		   /* delta-y */
     93  1.1  mw 	volatile int ms_ready;	   /* event queue is ready */
     94  1.1  mw 	struct	evvar ms_events;   /* event queue state */
     95  1.1  mw } ms_softc[NMOUSE];
     96  1.1  mw 
     97  1.1  mw 
     98  1.1  mw /* enable scanner, called when someone opens the device.
     99  1.1  mw    Assume caller already validated range of dev. */
    100  1.1  mw void
    101  1.1  mw ms_enable (dev_t dev)
    102  1.1  mw {
    103  1.1  mw   int unit = minor (dev);
    104  1.1  mw   struct ms_softc *ms = &ms_softc[unit];
    105  1.1  mw 
    106  1.1  mw   /* use this as flag to the "interrupt" to tell it when to
    107  1.1  mw      shut off (when it's reset to 0). */
    108  1.1  mw   ms->ms_ready = 1;
    109  1.1  mw 
    110  1.1  mw   timeout ((timeout_t) msintr, (caddr_t) unit, 2);
    111  1.1  mw }
    112  1.1  mw 
    113  1.1  mw /* disable scanner. Just set ms_ready to 0, and after the next
    114  1.1  mw    timeout taken, no further timeouts will be initiated. */
    115  1.1  mw void
    116  1.1  mw ms_disable (dev_t dev)
    117  1.1  mw {
    118  1.1  mw   struct ms_softc *ms = &ms_softc[minor (dev)];
    119  1.1  mw   int s = splhigh ();
    120  1.1  mw 
    121  1.1  mw   ms->ms_ready = 0;
    122  1.1  mw   /* sync with the interrupt */
    123  1.1  mw   tsleep ((caddr_t) ms, PZERO - 1, "mouse-disable", 0);
    124  1.1  mw   splx (s);
    125  1.1  mw }
    126  1.1  mw 
    127  1.1  mw 
    128  1.1  mw void
    129  1.1  mw msintr (int unit)
    130  1.1  mw {
    131  1.1  mw   struct ms_softc *ms = &ms_softc[unit];
    132  1.1  mw   register struct firm_event *fe;
    133  1.1  mw   register int mb, ub, d, get, put, any;
    134  1.1  mw   static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
    135  1.1  mw   static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
    136  1.1  mw   u_short pot;
    137  1.1  mw   u_char  pra;
    138  1.1  mw   u_short count;
    139  1.1  mw   u_char  *horc = ((u_char *) &count) + 1;
    140  1.1  mw   u_char  *verc = (u_char *) &count;
    141  1.1  mw   short	  dx, dy;
    142  1.1  mw 
    143  1.1  mw   /* BTW: we're emulating a mousesystems serial mouse here.. */
    144  1.1  mw 
    145  1.1  mw   /* first read the three buttons. */
    146  1.1  mw   pot  = custom.potgor;
    147  1.1  mw   pra  = ciaa.pra;
    148  1.1  mw   pot  >>= unit == 0 ? 8 : 12;	/* contains right and middle button */
    149  1.1  mw   pra  >>= unit == 0 ? 6 : 7;	/* contains left button */
    150  1.1  mw   mb = (pot & 4) / 4 + (pot & 1) * 2 + (pra & 1) * 4;
    151  1.1  mw   mb ^= 0x07;
    152  1.1  mw 
    153  1.1  mw   /* read current values of counter registers */
    154  1.1  mw   count = unit == 0 ? custom.joy0dat : custom.joy1dat;
    155  1.1  mw 
    156  1.1  mw   /* take care of wraparound */
    157  1.1  mw   dx = *horc - ms->ms_horc;
    158  1.1  mw   if (dx < -127)
    159  1.1  mw     dx += 255;
    160  1.1  mw   else if (dx > 127)
    161  1.1  mw     dx -= 255;
    162  1.1  mw   dy = *verc - ms->ms_verc;
    163  1.1  mw   if (dy < -127)
    164  1.1  mw     dy += 255;
    165  1.1  mw   else if (dy > 127)
    166  1.1  mw     dy -= 255;
    167  1.1  mw 
    168  1.1  mw   /* remember current values for next scan */
    169  1.1  mw   ms->ms_horc = *horc;
    170  1.1  mw   ms->ms_verc = *verc;
    171  1.1  mw 
    172  1.1  mw   ms->ms_dx = dx;
    173  1.1  mw   ms->ms_dy = dy;
    174  1.1  mw   ms->ms_mb = mb;
    175  1.1  mw 
    176  1.1  mw   if (dx || dy || ms->ms_ub != ms->ms_mb)
    177  1.1  mw     {
    178  1.1  mw 
    179  1.1  mw       /* We have at least one event (mouse button, delta-X, or
    180  1.1  mw 	 delta-Y; possibly all three, and possibly three separate
    181  1.1  mw 	 button events).  Deliver these events until we are out
    182  1.1  mw 	 of changes or out of room.  As events get delivered,
    183  1.1  mw 	 mark them `unchanged'. */
    184  1.1  mw 
    185  1.1  mw       any = 0;
    186  1.1  mw       get = ms->ms_events.ev_get;
    187  1.1  mw       put = ms->ms_events.ev_put;
    188  1.1  mw       fe = &ms->ms_events.ev_q[put];
    189  1.1  mw 
    190  1.1  mw       /* NEXT prepares to put the next event, backing off if necessary */
    191  1.1  mw #define	NEXT \
    192  1.1  mw       if ((++put) % EV_QSIZE == get)  \
    193  1.1  mw 	{ \
    194  1.1  mw           put--; \
    195  1.1  mw 	  goto out; \
    196  1.1  mw 	}
    197  1.1  mw       /* ADVANCE completes the `put' of the event */
    198  1.1  mw #define	ADVANCE \
    199  1.1  mw       fe++; \
    200  1.1  mw       if (put >= EV_QSIZE) \
    201  1.1  mw 	{ \
    202  1.1  mw 	  put = 0; \
    203  1.1  mw 	  fe = &ms->ms_events.ev_q[0]; \
    204  1.1  mw 	} \
    205  1.1  mw       any = 1
    206  1.1  mw 
    207  1.1  mw       mb = ms->ms_mb;
    208  1.1  mw       ub = ms->ms_ub;
    209  1.1  mw       while ((d = mb ^ ub) != 0)
    210  1.1  mw 	{
    211  1.1  mw 	  /* Mouse button change.  Convert up to three changes
    212  1.1  mw 	     to the `first' change, and drop it into the event queue. */
    213  1.1  mw 
    214  1.1  mw 	  NEXT;
    215  1.1  mw 	  d = to_one[d - 1];		/* from 1..7 to {1,2,4} */
    216  1.1  mw 	  fe->id = to_id[d - 1];		/* from {1,2,4} to ID */
    217  1.1  mw 	  fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
    218  1.1  mw 	  fe->time = time;
    219  1.1  mw 	  ADVANCE;
    220  1.1  mw 	  ub ^= d;
    221  1.1  mw 	}
    222  1.1  mw       if (ms->ms_dx)
    223  1.1  mw 	{
    224  1.1  mw 	  NEXT;
    225  1.1  mw 	  fe->id = LOC_X_DELTA;
    226  1.1  mw 	  fe->value = ms->ms_dx;
    227  1.1  mw 	  fe->time = time;
    228  1.1  mw 	  ADVANCE;
    229  1.1  mw 	  ms->ms_dx = 0;
    230  1.1  mw 	}
    231  1.1  mw       if (ms->ms_dy)
    232  1.1  mw 	{
    233  1.1  mw 	  NEXT;
    234  1.1  mw 	  fe->id = LOC_Y_DELTA;
    235  1.1  mw 	  fe->value = ms->ms_dy;
    236  1.1  mw 	  fe->time = time;
    237  1.1  mw 	  ADVANCE;
    238  1.1  mw 	  ms->ms_dy = 0;
    239  1.1  mw 	}
    240  1.1  mw out:
    241  1.1  mw       if (any)
    242  1.1  mw 	{
    243  1.1  mw 	  ms->ms_ub = ub;
    244  1.1  mw 	  ms->ms_events.ev_put = put;
    245  1.1  mw 	  EV_WAKEUP(&ms->ms_events);
    246  1.1  mw 	}
    247  1.1  mw     }
    248  1.1  mw 
    249  1.1  mw   /* reschedule handler, or if terminating, handshake with ms_disable */
    250  1.1  mw   if (ms->ms_ready)
    251  1.1  mw     timeout ((timeout_t) msintr, (caddr_t) unit, 2);
    252  1.1  mw   else
    253  1.1  mw     wakeup ((caddr_t) ms);
    254  1.1  mw 
    255  1.1  mw }
    256  1.1  mw 
    257  1.1  mw int
    258  1.1  mw msopen (dev, flags, mode, p)
    259  1.1  mw      dev_t dev;
    260  1.1  mw      int flags, mode;
    261  1.1  mw      struct proc *p;
    262  1.1  mw {
    263  1.1  mw   int unit = minor (dev);
    264  1.1  mw   struct ms_softc *ms = &ms_softc[unit];
    265  1.1  mw   int s, error;
    266  1.1  mw 
    267  1.1  mw   if (unit >= NMOUSE)
    268  1.1  mw     return EXDEV;
    269  1.1  mw 
    270  1.1  mw   if (ms->ms_events.ev_io)
    271  1.1  mw     return EBUSY;
    272  1.1  mw 
    273  1.1  mw   ms->ms_events.ev_io = p;
    274  1.1  mw   ev_init (&ms->ms_events);	/* may cause sleep */
    275  1.1  mw   ms_enable (dev);
    276  1.1  mw   return 0;
    277  1.1  mw }
    278  1.1  mw 
    279  1.1  mw int
    280  1.1  mw msclose (dev, flags, mode, p)
    281  1.1  mw      dev_t dev;
    282  1.1  mw      int flags, mode;
    283  1.1  mw      struct proc *p;
    284  1.1  mw {
    285  1.1  mw   int unit = minor (dev);
    286  1.1  mw   struct ms_softc *ms = &ms_softc[unit];
    287  1.1  mw 
    288  1.1  mw   ms_disable (dev);
    289  1.1  mw   ev_fini (&ms->ms_events);
    290  1.1  mw   ms->ms_events.ev_io = NULL;
    291  1.1  mw   return 0;
    292  1.1  mw }
    293  1.1  mw 
    294  1.1  mw int
    295  1.1  mw msread (dev, uio, flags)
    296  1.1  mw      dev_t dev;
    297  1.1  mw      struct uio *uio;
    298  1.1  mw      int flags;
    299  1.1  mw {
    300  1.1  mw   int unit = minor (dev);
    301  1.1  mw   struct ms_softc *ms = &ms_softc[unit];
    302  1.1  mw 
    303  1.1  mw   return ev_read (&ms->ms_events, uio, flags);
    304  1.1  mw }
    305  1.1  mw 
    306  1.1  mw /* this routine should not exist, but is convenient to write here for now */
    307  1.1  mw int
    308  1.1  mw mswrite (dev, uio, flags)
    309  1.1  mw      dev_t dev;
    310  1.1  mw      struct uio *uio;
    311  1.1  mw      int flags;
    312  1.1  mw {
    313  1.1  mw   return EOPNOTSUPP;
    314  1.1  mw }
    315  1.1  mw 
    316  1.1  mw int
    317  1.1  mw msioctl (dev, cmd, data, flag, p)
    318  1.1  mw      dev_t dev;
    319  1.1  mw      int cmd;
    320  1.1  mw      register caddr_t data;
    321  1.1  mw      int flag;
    322  1.1  mw      struct proc *p;
    323  1.1  mw {
    324  1.1  mw   int unit = minor (dev);
    325  1.1  mw   struct ms_softc *ms = &ms_softc[unit];
    326  1.1  mw 
    327  1.1  mw   switch (cmd)
    328  1.1  mw     {
    329  1.1  mw     case FIONBIO:		/* we will remove this someday (soon???) */
    330  1.1  mw       return 0;
    331  1.1  mw 
    332  1.1  mw     case FIOASYNC:
    333  1.1  mw       ms->ms_events.ev_async = *(int *)data != 0;
    334  1.1  mw       return 0;
    335  1.1  mw 
    336  1.1  mw     case TIOCSPGRP:
    337  1.1  mw       if (*(int *)data != ms->ms_events.ev_io->p_pgid)
    338  1.1  mw 	return EPERM;
    339  1.1  mw       return 0;
    340  1.1  mw 
    341  1.1  mw     case VUIDGFORMAT:
    342  1.1  mw       /* we only do firm_events */
    343  1.1  mw       *(int *)data = VUID_FIRM_EVENT;
    344  1.1  mw       return 0;
    345  1.1  mw 
    346  1.1  mw     case VUIDSFORMAT:
    347  1.1  mw       if (*(int *)data != VUID_FIRM_EVENT)
    348  1.1  mw 	return EINVAL;
    349  1.1  mw       return 0;
    350  1.1  mw     }
    351  1.1  mw 
    352  1.1  mw   return ENOTTY;
    353  1.1  mw }
    354  1.1  mw 
    355  1.1  mw int
    356  1.1  mw msselect (dev, rw, p)
    357  1.1  mw      dev_t dev;
    358  1.1  mw      int rw;
    359  1.1  mw      struct proc *p;
    360  1.1  mw {
    361  1.1  mw   int unit = minor (dev);
    362  1.1  mw   struct ms_softc *ms = &ms_softc[unit];
    363  1.1  mw 
    364  1.1  mw   return ev_select (&ms->ms_events, rw, p);
    365  1.1  mw }
    366  1.1  mw 
    367  1.1  mw void
    368  1.1  mw mouseattach() {}
    369  1.1  mw #endif /* NMOUSE > 0 */
    370