Home | History | Annotate | Line # | Download | only in dev
ms.c revision 1.39.18.1
      1  1.39.18.1  pgoyette /*	$NetBSD: ms.c,v 1.39.18.1 2017/04/27 05:36:31 pgoyette Exp $ */
      2        1.5       cgd 
      3        1.1        mw /*
      4        1.1        mw  * based on:
      5        1.1        mw  *
      6        1.1        mw  * Copyright (c) 1992, 1993
      7        1.1        mw  *	The Regents of the University of California.  All rights reserved.
      8        1.1        mw  *
      9        1.1        mw  * This software was developed by the Computer Systems Engineering group
     10        1.1        mw  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     11        1.1        mw  * contributed to Berkeley.
     12        1.1        mw  *
     13        1.1        mw  * All advertising materials mentioning features or use of this software
     14        1.1        mw  * must display the following acknowledgement:
     15        1.1        mw  *	This product includes software developed by the University of
     16        1.1        mw  *	California, Lawrence Berkeley Laboratory.
     17        1.1        mw  *
     18        1.1        mw  * Redistribution and use in source and binary forms, with or without
     19        1.1        mw  * modification, are permitted provided that the following conditions
     20        1.1        mw  * are met:
     21        1.1        mw  * 1. Redistributions of source code must retain the above copyright
     22        1.1        mw  *    notice, this list of conditions and the following disclaimer.
     23        1.1        mw  * 2. Redistributions in binary form must reproduce the above copyright
     24        1.1        mw  *    notice, this list of conditions and the following disclaimer in the
     25        1.1        mw  *    documentation and/or other materials provided with the distribution.
     26       1.26       agc  * 3. Neither the name of the University nor the names of its contributors
     27        1.1        mw  *    may be used to endorse or promote products derived from this software
     28        1.1        mw  *    without specific prior written permission.
     29        1.1        mw  *
     30        1.1        mw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     31        1.1        mw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     32        1.1        mw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33        1.1        mw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     34        1.1        mw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     35        1.1        mw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     36        1.1        mw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     37        1.1        mw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     38        1.1        mw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     39        1.1        mw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     40        1.1        mw  * SUCH DAMAGE.
     41        1.1        mw  *
     42        1.1        mw  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
     43        1.1        mw  *
     44        1.5       cgd  * Header: ms.c,v 1.5 92/11/26 01:28:47 torek Exp  (LBL)
     45        1.1        mw  */
     46       1.20   aymeric 
     47       1.20   aymeric #include <sys/cdefs.h>
     48  1.39.18.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: ms.c,v 1.39.18.1 2017/04/27 05:36:31 pgoyette Exp $");
     49        1.1        mw 
     50        1.1        mw /*
     51        1.1        mw  * Mouse driver.
     52       1.28  jandberg  *
     53       1.28  jandberg  * wscons aware. Attaches two wsmouse devices, one for each port.
     54       1.28  jandberg  * Also still exports its own device entry points so it is possible
     55       1.28  jandberg  * to open this and read firm_events.
     56       1.28  jandberg  * The events go only to one place at a time:
     57       1.28  jandberg  * - When somebody has opened a ms device directly wsmouse cannot be activated.
     58       1.28  jandberg  *   (when wsmouse is opened it calls ms_enable to activate)
     59       1.28  jandberg  * - When feeding events to wsmouse open of ms device will fail.
     60        1.1        mw  */
     61        1.1        mw 
     62       1.28  jandberg #include "wsmouse.h"
     63       1.28  jandberg 
     64        1.2    chopps #include <sys/param.h>
     65        1.9        is #include <sys/device.h>
     66        1.2    chopps #include <sys/ioctl.h>
     67        1.2    chopps #include <sys/kernel.h>
     68        1.2    chopps #include <sys/proc.h>
     69        1.2    chopps #include <sys/syslog.h>
     70        1.2    chopps #include <sys/systm.h>
     71       1.16   thorpej #include <sys/callout.h>
     72        1.2    chopps #include <sys/tty.h>
     73        1.8     veego #include <sys/signalvar.h>
     74       1.21   gehenna #include <sys/conf.h>
     75        1.1        mw 
     76        1.2    chopps #include <amiga/dev/event_var.h>
     77        1.2    chopps #include <amiga/dev/vuid_event.h>
     78        1.1        mw 
     79        1.2    chopps #include <amiga/amiga/custom.h>
     80        1.2    chopps #include <amiga/amiga/cia.h>
     81        1.9        is #include <amiga/amiga/device.h>
     82        1.1        mw 
     83       1.28  jandberg #if NWSMOUSE > 0
     84       1.28  jandberg #include <dev/wscons/wsmousevar.h>
     85       1.28  jandberg #include <dev/wscons/wsconsio.h>
     86       1.28  jandberg #endif
     87       1.28  jandberg 
     88       1.37       chs void msattach(device_t, device_t, void *);
     89       1.37       chs int msmatch(device_t, cfdata_t, void *);
     90        1.1        mw 
     91       1.17    kleink /* per-port state */
     92       1.17    kleink struct ms_port {
     93       1.17    kleink 	int	ms_portno;	   /* which hardware port, for msintr() */
     94        1.9        is 
     95       1.17    kleink 	struct callout ms_intr_ch;
     96       1.16   thorpej 
     97        1.9        is 	u_char	ms_horc;	   /* horizontal counter on last scan */
     98        1.9        is   	u_char	ms_verc;	   /* vertical counter on last scan */
     99        1.9        is 	char	ms_mb;		   /* mouse button state */
    100        1.9        is 	char	ms_ub;		   /* user button state */
    101        1.9        is 	int	ms_dx;		   /* delta-x */
    102        1.9        is 	int	ms_dy;		   /* delta-y */
    103        1.9        is 	volatile int ms_ready;	   /* event queue is ready */
    104        1.9        is 	struct	evvar ms_events;   /* event queue state */
    105       1.28  jandberg #if NWSMOUSE > 0
    106       1.37       chs 	device_t ms_wsmousedev; /* wsmouse device */
    107       1.28  jandberg 	int     ms_wsenabled;      /* feeding events to wscons */
    108       1.28  jandberg #endif
    109        1.9        is };
    110        1.9        is 
    111       1.17    kleink #define	MS_NPORTS	2
    112       1.17    kleink 
    113       1.17    kleink struct ms_softc {
    114       1.17    kleink 	struct ms_port sc_ports[MS_NPORTS];
    115       1.17    kleink };
    116       1.17    kleink 
    117       1.37       chs CFATTACH_DECL_NEW(ms, sizeof(struct ms_softc),
    118       1.23   thorpej     msmatch, msattach, NULL, NULL);
    119        1.9        is 
    120       1.19   aymeric void msintr(void *);
    121       1.19   aymeric void ms_enable(struct ms_port *);
    122       1.19   aymeric void ms_disable(struct ms_port *);
    123       1.17    kleink 
    124       1.15   thorpej extern struct cfdriver ms_cd;
    125       1.21   gehenna 
    126       1.21   gehenna dev_type_open(msopen);
    127       1.21   gehenna dev_type_close(msclose);
    128       1.21   gehenna dev_type_read(msread);
    129       1.21   gehenna dev_type_ioctl(msioctl);
    130       1.21   gehenna dev_type_poll(mspoll);
    131       1.24  jdolecek dev_type_kqfilter(mskqfilter);
    132       1.21   gehenna 
    133       1.21   gehenna const struct cdevsw ms_cdevsw = {
    134       1.38  dholland 	.d_open = msopen,
    135       1.38  dholland 	.d_close = msclose,
    136       1.38  dholland 	.d_read = msread,
    137       1.38  dholland 	.d_write = nowrite,
    138       1.38  dholland 	.d_ioctl = msioctl,
    139       1.38  dholland 	.d_stop = nostop,
    140       1.38  dholland 	.d_tty = notty,
    141       1.38  dholland 	.d_poll = mspoll,
    142       1.38  dholland 	.d_mmap = nommap,
    143       1.38  dholland 	.d_kqfilter = mskqfilter,
    144       1.39  dholland 	.d_discard = nodiscard,
    145       1.38  dholland 	.d_flag = 0
    146       1.21   gehenna };
    147        1.1        mw 
    148       1.17    kleink #define	MS_UNIT(d)	((minor(d) & ~0x1) >> 1)
    149       1.17    kleink #define	MS_PORT(d)	(minor(d) & 0x1)
    150       1.17    kleink 
    151       1.28  jandberg #if NWSMOUSE > 0
    152       1.28  jandberg /*
    153       1.28  jandberg  * Callbacks for wscons.
    154       1.28  jandberg  */
    155       1.28  jandberg static int ms_wscons_enable(void *);
    156       1.31  christos static int ms_wscons_ioctl(void *, u_long, void *, int, struct lwp *);
    157       1.28  jandberg static void ms_wscons_disable(void *);
    158       1.28  jandberg 
    159       1.28  jandberg static struct wsmouse_accessops ms_wscons_accessops = {
    160       1.28  jandberg 	ms_wscons_enable,
    161       1.28  jandberg 	ms_wscons_ioctl,
    162       1.28  jandberg 	ms_wscons_disable
    163       1.28  jandberg };
    164       1.28  jandberg #endif
    165       1.28  jandberg 
    166  1.39.18.1  pgoyette /*
    167  1.39.18.1  pgoyette  * Given a dev_t, return a pointer to the port's hardware state.
    168  1.39.18.1  pgoyette  * Assumes the unit to be valid, so do *not* use this in msopen().
    169  1.39.18.1  pgoyette  */
    170  1.39.18.1  pgoyette static struct ms_port *
    171  1.39.18.1  pgoyette ms_dev2msport(dev_t dev)
    172  1.39.18.1  pgoyette {
    173  1.39.18.1  pgoyette 	device_t self;
    174  1.39.18.1  pgoyette 	struct ms_softc *sc;
    175  1.39.18.1  pgoyette 	struct ms_port *msp;
    176  1.39.18.1  pgoyette 
    177  1.39.18.1  pgoyette 	msp = NULL;
    178  1.39.18.1  pgoyette 
    179  1.39.18.1  pgoyette 	self = device_lookup_acquire(&ms_cd, MS_UNIT(dev));
    180  1.39.18.1  pgoyette 	if (self == NULL)
    181  1.39.18.1  pgoyette 		return msp;;
    182  1.39.18.1  pgoyette 	sc = device_private(self);
    183  1.39.18.1  pgoyette 	if (sc != NULL)
    184  1.39.18.1  pgoyette 		msp = sc->sc_ports[MS_PORT(dev)];
    185  1.39.18.1  pgoyette 
    186  1.39.18.1  pgoyette 	device_release(self);
    187  1.39.18.1  pgoyette 	return msp;
    188  1.39.18.1  pgoyette }
    189  1.39.18.1  pgoyette 
    190        1.3    chopps int
    191       1.37       chs msmatch(device_t parent, cfdata_t cf, void *aux)
    192        1.3    chopps {
    193       1.17    kleink 	static int ms_matched = 0;
    194        1.9        is 
    195       1.17    kleink 	/* Allow only one instance. */
    196       1.37       chs 	if (!matchname((char *)aux, "ms") || ms_matched)
    197       1.17    kleink 		return 0;
    198        1.9        is 
    199       1.17    kleink 	ms_matched = 1;
    200       1.17    kleink 	return 1;
    201        1.3    chopps }
    202        1.1        mw 
    203        1.9        is void
    204       1.37       chs msattach(device_t parent, device_t self, void *aux)
    205        1.9        is {
    206       1.28  jandberg #if NWSMOUSE > 0
    207       1.28  jandberg 	struct wsmousedev_attach_args waa;
    208       1.28  jandberg #endif
    209       1.37       chs 	struct ms_softc *sc = device_private(self);
    210       1.17    kleink 	int i;
    211       1.16   thorpej 
    212       1.13  christos 	printf("\n");
    213       1.17    kleink 	for (i = 0; i < MS_NPORTS; i++) {
    214       1.17    kleink 		sc->sc_ports[i].ms_portno = i;
    215       1.32        ad 		callout_init(&sc->sc_ports[i].ms_intr_ch, 0);
    216       1.28  jandberg #if NWSMOUSE > 0
    217       1.28  jandberg 		waa.accessops = &ms_wscons_accessops;
    218       1.28  jandberg 		waa.accesscookie = &sc->sc_ports[i];
    219       1.28  jandberg 
    220       1.28  jandberg 		sc->sc_ports[i].ms_wsenabled = 0;
    221       1.28  jandberg 		sc->sc_ports[i].ms_wsmousedev =
    222       1.37       chs 		    config_found(self, &waa, wsmousedevprint);
    223       1.28  jandberg #endif
    224       1.17    kleink 	}
    225        1.9        is }
    226        1.9        is 
    227        1.3    chopps /*
    228        1.3    chopps  * Amiga mice are hooked up to one of the two "game" ports, where
    229        1.3    chopps  * the main mouse is usually on the first port, and port 2 can
    230        1.3    chopps  * be used by a joystick. Nevertheless, we support two mouse
    231        1.3    chopps  * devices, /dev/mouse0 and /dev/mouse1 (with a link of /dev/mouse to
    232        1.3    chopps  * the device that represents the port of the mouse in use).
    233        1.3    chopps  */
    234        1.1        mw 
    235        1.3    chopps /*
    236       1.17    kleink  * enable scanner, called when someone opens the port.
    237        1.3    chopps  */
    238        1.1        mw void
    239       1.19   aymeric ms_enable(struct ms_port *ms)
    240        1.1        mw {
    241       1.10        is 
    242       1.19   aymeric 	/*
    243        1.3    chopps 	 * use this as flag to the "interrupt" to tell it when to
    244        1.3    chopps 	 * shut off (when it's reset to 0).
    245        1.3    chopps 	 */
    246        1.3    chopps 	ms->ms_ready = 1;
    247        1.1        mw 
    248       1.17    kleink 	callout_reset(&ms->ms_intr_ch, 2, msintr, ms);
    249        1.1        mw }
    250        1.1        mw 
    251        1.3    chopps /*
    252        1.3    chopps  * disable scanner. Just set ms_ready to 0, and after the next
    253        1.3    chopps  * timeout taken, no further timeouts will be initiated.
    254        1.3    chopps  */
    255        1.1        mw void
    256       1.19   aymeric ms_disable(struct ms_port *ms)
    257        1.1        mw {
    258        1.3    chopps 	int s;
    259        1.1        mw 
    260        1.3    chopps 	s = splhigh ();
    261        1.3    chopps 	ms->ms_ready = 0;
    262        1.3    chopps 	/*
    263        1.3    chopps 	 * sync with the interrupt
    264        1.3    chopps 	 */
    265        1.3    chopps 	tsleep(ms, PZERO - 1, "mouse-disable", 0);
    266        1.3    chopps 	splx(s);
    267        1.1        mw }
    268        1.1        mw 
    269        1.1        mw 
    270       1.19   aymeric /*
    271        1.3    chopps  * we're emulating a mousesystems serial mouse here..
    272        1.3    chopps  */
    273        1.1        mw void
    274       1.19   aymeric msintr(void *arg)
    275        1.1        mw {
    276        1.3    chopps 	static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
    277        1.3    chopps 	static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
    278       1.17    kleink 	struct ms_port *ms = arg;
    279        1.3    chopps 	struct firm_event *fe;
    280       1.17    kleink 	int mb, ub, d, get, put, any, port;
    281        1.3    chopps 	u_char pra, *horc, *verc;
    282        1.3    chopps 	u_short pot, count;
    283        1.3    chopps 	short dx, dy;
    284       1.19   aymeric 
    285       1.17    kleink 	port = ms->ms_portno;
    286       1.10        is 
    287        1.3    chopps 	horc = ((u_char *) &count) + 1;
    288        1.3    chopps 	verc = (u_char *) &count;
    289        1.3    chopps 
    290        1.3    chopps 	/*
    291        1.3    chopps 	 * first read the three buttons.
    292        1.3    chopps 	 */
    293        1.3    chopps 	pot  = custom.potgor;
    294        1.3    chopps 	pra  = ciaa.pra;
    295       1.17    kleink 	pot >>= port == 0 ? 8 : 12;	/* contains right and middle button */
    296       1.17    kleink 	pra >>= port == 0 ? 6 : 7;	/* contains left button */
    297        1.3    chopps 	mb = (pot & 4) / 4 + (pot & 1) * 2 + (pra & 1) * 4;
    298        1.3    chopps 	mb ^= 0x07;
    299        1.3    chopps 
    300        1.3    chopps 	/*
    301        1.3    chopps 	 * read current values of counter registers
    302        1.3    chopps 	 */
    303       1.17    kleink 	if (port == 0)
    304        1.3    chopps 		count = custom.joy0dat;
    305        1.3    chopps 	else
    306        1.3    chopps 		count = custom.joy1dat;
    307       1.19   aymeric 
    308        1.3    chopps 	/*
    309        1.3    chopps 	 * take care of wraparound
    310        1.3    chopps 	 */
    311        1.3    chopps 	dx = *horc - ms->ms_horc;
    312        1.3    chopps 	if (dx < -127)
    313        1.3    chopps 		dx += 255;
    314        1.3    chopps 	else if (dx > 127)
    315        1.3    chopps 		dx -= 255;
    316        1.3    chopps 	dy = *verc - ms->ms_verc;
    317        1.3    chopps 	if (dy < -127)
    318        1.3    chopps 		dy += 255;
    319        1.3    chopps 	else if (dy > 127)
    320        1.3    chopps 		dy -= 255;
    321        1.3    chopps 
    322        1.3    chopps 	/*
    323        1.3    chopps 	 * remember current values for next scan
    324        1.3    chopps 	 */
    325        1.3    chopps 	ms->ms_horc = *horc;
    326        1.3    chopps 	ms->ms_verc = *verc;
    327        1.3    chopps 
    328        1.3    chopps 	ms->ms_dx = dx;
    329        1.3    chopps 	ms->ms_dy = dy;
    330        1.3    chopps 	ms->ms_mb = mb;
    331       1.19   aymeric 
    332       1.28  jandberg #if NWSMOUSE > 0
    333       1.28  jandberg 	/*
    334       1.28  jandberg 	 * If we have attached wsmouse and we are not opened
    335       1.28  jandberg 	 * directly then pass events to wscons.
    336       1.28  jandberg 	 */
    337       1.28  jandberg 	if (ms->ms_wsmousedev && ms->ms_wsenabled)
    338       1.28  jandberg 	{
    339       1.28  jandberg 		int buttons = 0;
    340       1.28  jandberg 
    341       1.28  jandberg 		if (mb & 4)
    342       1.28  jandberg 			buttons |= 1;
    343       1.28  jandberg 		if (mb & 2)
    344       1.28  jandberg 			buttons |= 2;
    345       1.28  jandberg 		if (mb & 1)
    346       1.28  jandberg 			buttons |= 4;
    347       1.28  jandberg 
    348       1.28  jandberg 		wsmouse_input(ms->ms_wsmousedev,
    349       1.28  jandberg 			      buttons,
    350       1.30    plunky 			      dx, -dy, 0, 0,
    351       1.28  jandberg 			      WSMOUSE_INPUT_DELTA);
    352       1.28  jandberg 
    353       1.28  jandberg 	} else
    354       1.28  jandberg #endif
    355        1.3    chopps 	if (dx || dy || ms->ms_ub != ms->ms_mb) {
    356        1.3    chopps 		/*
    357        1.3    chopps 		 * We have at least one event (mouse button, delta-X, or
    358        1.3    chopps 		 * delta-Y; possibly all three, and possibly three separate
    359        1.3    chopps 		 * button events).  Deliver these events until we are out of
    360       1.19   aymeric 		 * changes or out of room.  As events get delivered, mark them
    361        1.3    chopps 		 * `unchanged'.
    362        1.3    chopps 		 */
    363        1.3    chopps 		any = 0;
    364        1.3    chopps 		get = ms->ms_events.ev_get;
    365        1.3    chopps 		put = ms->ms_events.ev_put;
    366        1.3    chopps 		fe = &ms->ms_events.ev_q[put];
    367        1.3    chopps 
    368        1.3    chopps 		mb = ms->ms_mb;
    369        1.3    chopps 		ub = ms->ms_ub;
    370        1.3    chopps 		while ((d = mb ^ ub) != 0) {
    371        1.3    chopps 			/*
    372        1.3    chopps 			 * Mouse button change.  Convert up to three changes
    373        1.3    chopps 			 * to the `first' change, and drop it into the event
    374        1.3    chopps 			 * queue.
    375        1.3    chopps 			 */
    376        1.3    chopps 			if ((++put) % EV_QSIZE == get) {
    377        1.3    chopps 				put--;
    378        1.3    chopps 				goto out;
    379        1.3    chopps 			}
    380        1.3    chopps 
    381        1.3    chopps 			d = to_one[d - 1];	/* from 1..7 to {1,2,4} */
    382        1.3    chopps 			fe->id = to_id[d - 1];	/* from {1,2,4} to ID */
    383        1.3    chopps 			fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
    384       1.35    mhitch 			firm_gettime(fe);
    385        1.3    chopps 			fe++;
    386        1.3    chopps 
    387        1.3    chopps 			if (put >= EV_QSIZE) {
    388        1.3    chopps 				put = 0;
    389        1.3    chopps 				fe = &ms->ms_events.ev_q[0];
    390        1.3    chopps 			}
    391        1.3    chopps 			any = 1;
    392        1.3    chopps 
    393        1.3    chopps 			ub ^= d;
    394        1.3    chopps 		}
    395        1.3    chopps 		if (ms->ms_dx) {
    396        1.3    chopps 			if ((++put) % EV_QSIZE == get) {
    397        1.3    chopps 				put--;
    398        1.3    chopps 				goto out;
    399        1.3    chopps 			}
    400        1.3    chopps 
    401        1.3    chopps 			fe->id = LOC_X_DELTA;
    402        1.3    chopps 			fe->value = ms->ms_dx;
    403       1.35    mhitch 			firm_gettime(fe);
    404        1.4    chopps 			fe++;
    405        1.3    chopps 
    406        1.3    chopps 			if (put >= EV_QSIZE) {
    407        1.3    chopps 				put = 0;
    408        1.3    chopps 				fe = &ms->ms_events.ev_q[0];
    409        1.3    chopps 			}
    410        1.3    chopps 			any = 1;
    411        1.3    chopps 
    412        1.3    chopps 			ms->ms_dx = 0;
    413        1.3    chopps 		}
    414        1.3    chopps 		if (ms->ms_dy) {
    415        1.3    chopps 			if ((++put) % EV_QSIZE == get) {
    416        1.3    chopps 				put--;
    417        1.3    chopps 				goto out;
    418        1.3    chopps 			}
    419        1.3    chopps 
    420        1.3    chopps 			fe->id = LOC_Y_DELTA;
    421        1.3    chopps 			fe->value = ms->ms_dy;
    422       1.35    mhitch 			firm_gettime(fe);
    423        1.4    chopps 			fe++;
    424        1.3    chopps 
    425        1.3    chopps 			if (put >= EV_QSIZE) {
    426        1.3    chopps 				put = 0;
    427        1.3    chopps 				fe = &ms->ms_events.ev_q[0];
    428        1.3    chopps 			}
    429        1.3    chopps 			any = 1;
    430        1.1        mw 
    431        1.3    chopps 			ms->ms_dy = 0;
    432        1.3    chopps 		}
    433        1.1        mw out:
    434        1.3    chopps 		if (any) {
    435        1.3    chopps 			ms->ms_ub = ub;
    436        1.3    chopps 			ms->ms_events.ev_put = put;
    437        1.3    chopps 			EV_WAKEUP(&ms->ms_events);
    438        1.3    chopps 		}
    439        1.1        mw 	}
    440        1.1        mw 
    441        1.3    chopps 	/*
    442        1.3    chopps 	 * reschedule handler, or if terminating,
    443        1.3    chopps 	 * handshake with ms_disable
    444        1.3    chopps 	 */
    445        1.3    chopps 	if (ms->ms_ready)
    446       1.17    kleink 		callout_reset(&ms->ms_intr_ch, 2, msintr, ms);
    447        1.3    chopps 	else
    448        1.3    chopps 		wakeup(ms);
    449        1.1        mw }
    450        1.1        mw 
    451        1.1        mw int
    452       1.29  christos msopen(dev_t dev, int flags, int mode, struct lwp *l)
    453        1.1        mw {
    454  1.39.18.1  pgoyette 	device_t self;
    455       1.17    kleink 	struct ms_softc *sc;
    456       1.17    kleink 	struct ms_port *ms;
    457       1.17    kleink 	int unit, port;
    458        1.3    chopps 
    459       1.17    kleink 	unit = MS_UNIT(dev);
    460        1.3    chopps 
    461  1.39.18.1  pgoyette 	self = device_lookup_acquire(&ms_cd, unit);
    462  1.39.18.1  pgoyette 	if (self == NULL)
    463  1.39.18.1  pgoyette 		return ENXIO;
    464  1.39.18.1  pgoyette 	sc = device_private(self);
    465  1.39.18.1  pgoyette 	if (sc == NULL) {
    466  1.39.18.1  pgoyette 		device_release(self);
    467        1.3    chopps 		return(EXDEV);
    468  1.39.18.1  pgoyette 	}
    469        1.3    chopps 
    470       1.17    kleink 	port = MS_PORT(dev);
    471       1.17    kleink 	ms = &sc->sc_ports[port];
    472       1.17    kleink 
    473  1.39.18.1  pgoyette 	if (ms->ms_events.ev_io) {
    474  1.39.18.1  pgoyette 		device_release(self);
    475        1.3    chopps 		return(EBUSY);
    476  1.39.18.1  pgoyette 	}
    477       1.18        is 
    478       1.28  jandberg #if NWSMOUSE > 0
    479       1.28  jandberg 	/* don't allow opening when sending events to wsmouse */
    480  1.39.18.1  pgoyette 	if (ms->ms_wsenabled) {
    481  1.39.18.1  pgoyette 		device_release(self);
    482       1.28  jandberg 		return EBUSY;
    483  1.39.18.1  pgoyette 	}
    484       1.28  jandberg #endif
    485       1.18        is 	/* initialize potgo bits for mouse mode */
    486       1.18        is 	custom.potgo = custom.potgor | (0xf00 << (port * 4));
    487        1.3    chopps 
    488       1.29  christos 	ms->ms_events.ev_io = l->l_proc;
    489        1.3    chopps 	ev_init(&ms->ms_events);	/* may cause sleep */
    490       1.17    kleink 	ms_enable(ms);
    491  1.39.18.1  pgoyette 
    492  1.39.18.1  pgoyette 	device_release(self);
    493        1.3    chopps 	return(0);
    494        1.1        mw }
    495        1.1        mw 
    496        1.1        mw int
    497       1.29  christos msclose(dev_t dev, int flags, int mode, struct lwp *l)
    498        1.1        mw {
    499       1.17    kleink 	struct ms_port *ms;
    500        1.1        mw 
    501  1.39.18.1  pgoyette 	ms = ms_dev2msport(dev);
    502        1.3    chopps 
    503       1.17    kleink 	ms_disable(ms);
    504        1.3    chopps 	ev_fini(&ms->ms_events);
    505        1.3    chopps 	ms->ms_events.ev_io = NULL;
    506        1.3    chopps 	return(0);
    507        1.1        mw }
    508        1.1        mw 
    509        1.1        mw int
    510       1.19   aymeric msread(dev_t dev, struct uio *uio, int flags)
    511        1.1        mw {
    512       1.17    kleink 	struct ms_port *ms;
    513        1.1        mw 
    514  1.39.18.1  pgoyette 	ms = ms_dev2msport(dev);
    515       1.10        is 
    516        1.3    chopps 	return(ev_read(&ms->ms_events, uio, flags));
    517        1.1        mw }
    518        1.1        mw 
    519        1.1        mw int
    520       1.31  christos msioctl(dev_t dev, u_long cmd, register void *data, int flag,
    521       1.29  christos         struct lwp *l)
    522        1.1        mw {
    523       1.17    kleink 	struct ms_port *ms;
    524        1.1        mw 
    525  1.39.18.1  pgoyette 	ms = ms_dev2msport(dev);
    526        1.1        mw 
    527        1.3    chopps 	switch (cmd) {
    528        1.3    chopps 	case FIONBIO:		/* we will remove this someday (soon???) */
    529        1.3    chopps 		return(0);
    530        1.3    chopps 	case FIOASYNC:
    531        1.3    chopps 		ms->ms_events.ev_async = *(int *)data != 0;
    532       1.27  jdolecek 		return(0);
    533       1.27  jdolecek 	case FIOSETOWN:
    534       1.27  jdolecek 		if (-*(int *)data != ms->ms_events.ev_io->p_pgid
    535       1.27  jdolecek 		    && *(int *)data != ms->ms_events.ev_io->p_pid)
    536       1.27  jdolecek 			return(EPERM);
    537        1.3    chopps 		return(0);
    538        1.3    chopps 	case TIOCSPGRP:
    539        1.3    chopps 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
    540        1.3    chopps 			return(EPERM);
    541        1.3    chopps 		return(0);
    542        1.3    chopps 	case VUIDGFORMAT:	/* we only do firm_events */
    543        1.3    chopps 		*(int *)data = VUID_FIRM_EVENT;
    544        1.3    chopps 		return(0);
    545        1.3    chopps 	case VUIDSFORMAT:
    546        1.3    chopps 		if (*(int *)data != VUID_FIRM_EVENT)
    547        1.3    chopps 			return(EINVAL);
    548        1.3    chopps 		return(0);
    549        1.3    chopps 	}
    550        1.3    chopps 	return(ENOTTY);
    551        1.1        mw }
    552        1.1        mw 
    553        1.1        mw int
    554       1.29  christos mspoll(dev_t dev, int events, struct lwp *l)
    555        1.1        mw {
    556       1.17    kleink 	struct ms_port *ms;
    557        1.1        mw 
    558  1.39.18.1  pgoyette 	ms = ms_dev2msport(dev);
    559       1.10        is 
    560       1.29  christos 	return(ev_poll(&ms->ms_events, events, l));
    561       1.24  jdolecek }
    562       1.24  jdolecek 
    563       1.24  jdolecek int
    564       1.36       dsl mskqfilter(dev_t dev, struct knote *kn)
    565       1.24  jdolecek {
    566       1.24  jdolecek 	struct ms_port *ms;
    567       1.24  jdolecek 
    568  1.39.18.1  pgoyette 	ms = ms_dev2msport(dev);
    569       1.24  jdolecek 
    570       1.24  jdolecek 	return (ev_kqfilter(&ms->ms_events, kn));
    571        1.1        mw }
    572       1.28  jandberg 
    573       1.28  jandberg #if NWSMOUSE > 0
    574       1.28  jandberg 
    575       1.28  jandberg static int
    576       1.31  christos ms_wscons_ioctl(void *cookie, u_long cmd, void *data, int flag,
    577       1.29  christos 		struct lwp *l)
    578       1.28  jandberg {
    579       1.28  jandberg 	switch(cmd) {
    580       1.28  jandberg 	case WSMOUSEIO_GTYPE:
    581       1.28  jandberg 		*(u_int*)data = WSMOUSE_TYPE_AMIGA;
    582       1.28  jandberg 		return (0);
    583       1.28  jandberg 	}
    584       1.28  jandberg 
    585       1.28  jandberg 	return -1;
    586       1.28  jandberg }
    587       1.28  jandberg 
    588       1.28  jandberg static int
    589       1.28  jandberg ms_wscons_enable(void *cookie)
    590       1.28  jandberg {
    591       1.28  jandberg 	struct ms_port *port = cookie;
    592       1.28  jandberg 
    593       1.28  jandberg 	/* somebody reading events from us directly? */
    594       1.28  jandberg 	if (port->ms_events.ev_io)
    595       1.28  jandberg 		return EBUSY;
    596       1.28  jandberg 
    597       1.28  jandberg 	port->ms_wsenabled = 1;
    598       1.28  jandberg 	ms_enable(port);
    599       1.28  jandberg 
    600       1.28  jandberg 	return 0;
    601       1.28  jandberg }
    602       1.28  jandberg 
    603       1.28  jandberg static void
    604       1.28  jandberg ms_wscons_disable(void *cookie)
    605       1.28  jandberg {
    606       1.28  jandberg 	struct ms_port *port = cookie;
    607       1.28  jandberg 
    608       1.28  jandberg 	if (port->ms_wsenabled)
    609       1.28  jandberg 		ms_disable(port);
    610       1.28  jandberg 	port->ms_wsenabled = 0;
    611       1.28  jandberg }
    612       1.28  jandberg 
    613       1.28  jandberg #endif
    614       1.28  jandberg 
    615