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