Home | History | Annotate | Line # | Download | only in iomd
qms.c revision 1.11.10.1
      1  1.11.10.1   garbled /*	$NetBSD: qms.c,v 1.11.10.1 2007/10/03 19:22:44 garbled Exp $	*/
      2        1.1   reinoud 
      3        1.7     bjh21 /*-
      4        1.7     bjh21  * Copyright (c) 2001 Reinoud Zandijk
      5        1.7     bjh21  * All rights reserved.
      6        1.7     bjh21  *
      7        1.7     bjh21  * This code is derived from software contributed to The NetBSD Foundation
      8        1.7     bjh21  * by Reinoud Zandijk
      9        1.1   reinoud  *
     10        1.1   reinoud  * Redistribution and use in source and binary forms, with or without
     11        1.1   reinoud  * modification, are permitted provided that the following conditions
     12        1.1   reinoud  * are met:
     13        1.1   reinoud  * 1. Redistributions of source code must retain the above copyright
     14        1.1   reinoud  *    notice, this list of conditions and the following disclaimer.
     15        1.1   reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1   reinoud  *    notice, this list of conditions and the following disclaimer in the
     17        1.1   reinoud  *    documentation and/or other materials provided with the distribution.
     18        1.1   reinoud  * 3. All advertising materials mentioning features or use of this software
     19        1.1   reinoud  *    must display the following acknowledgement:
     20        1.7     bjh21  *      This product includes software developed by the NetBSD
     21        1.7     bjh21  *      Foundation, Inc. and its contributors.
     22        1.7     bjh21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23        1.7     bjh21  *    contributors may be used to endorse or promote products derived
     24        1.7     bjh21  *    from this software without specific prior written permission.
     25        1.1   reinoud  *
     26        1.7     bjh21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27        1.7     bjh21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28        1.7     bjh21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29        1.7     bjh21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30        1.7     bjh21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31        1.7     bjh21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32        1.7     bjh21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33        1.7     bjh21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34        1.7     bjh21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35        1.7     bjh21  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36        1.7     bjh21  * POSSIBILITY OF SUCH DAMAGE.
     37        1.1   reinoud  */
     38        1.1   reinoud /*
     39        1.7     bjh21  * Quadrature mouse driver for the wscons as used in the IOMD
     40        1.1   reinoud  */
     41        1.5     lukem 
     42        1.7     bjh21 #include <sys/param.h>
     43        1.7     bjh21 
     44  1.11.10.1   garbled __KERNEL_RCSID(0, "$NetBSD: qms.c,v 1.11.10.1 2007/10/03 19:22:44 garbled Exp $");
     45        1.1   reinoud 
     46        1.7     bjh21 #include <sys/callout.h>
     47        1.7     bjh21 #include <sys/device.h>
     48        1.7     bjh21 #include <sys/errno.h>
     49        1.1   reinoud #include <sys/ioctl.h>
     50        1.7     bjh21 #include <sys/kernel.h>
     51        1.7     bjh21 #include <sys/malloc.h>
     52        1.7     bjh21 #include <sys/proc.h>
     53        1.1   reinoud #include <sys/tty.h>
     54        1.1   reinoud #include <sys/types.h>
     55        1.7     bjh21 #include <sys/syslog.h>
     56        1.7     bjh21 #include <sys/systm.h>
     57        1.7     bjh21 #include <sys/select.h>
     58        1.1   reinoud 
     59        1.1   reinoud #include <machine/bus.h>
     60        1.7     bjh21 #include <machine/intr.h>
     61        1.1   reinoud 
     62        1.7     bjh21 #include <arm/iomd/iomdvar.h>
     63        1.1   reinoud 
     64        1.7     bjh21 #include <dev/wscons/wsconsio.h>
     65        1.7     bjh21 #include <dev/wscons/wsmousevar.h>
     66        1.1   reinoud 
     67        1.7     bjh21 struct qms_softc {
     68        1.7     bjh21 	struct device  sc_dev;
     69        1.7     bjh21 	struct device *sc_wsmousedev;
     70        1.1   reinoud 
     71        1.7     bjh21 	bus_space_tag_t sc_iot;		/* bus tag */
     72        1.7     bjh21 	bus_space_handle_t sc_ioh;	/* bus handle for XY */
     73        1.7     bjh21 	bus_space_handle_t sc_butioh;	/* bus handle for buttons */
     74        1.2   gehenna 
     75        1.7     bjh21 	struct callout sc_callout;
     76        1.2   gehenna 
     77        1.7     bjh21 	u_int16_t lastx;
     78        1.7     bjh21 	u_int16_t lasty;
     79        1.7     bjh21 	int lastb;
     80        1.2   gehenna };
     81        1.1   reinoud 
     82        1.1   reinoud /* Offsets of hardware registers */
     83        1.7     bjh21 #define QMS_MOUSEX	0		/* 16 bits X register */
     84        1.7     bjh21 #define QMS_MOUSEY	1		/* 16 bits Y register */
     85        1.7     bjh21 #define QMS_BUTTONS	0 		/* mouse buttons in bits 4,5,6 */
     86        1.7     bjh21 
     87        1.7     bjh21 static int  qms_match(struct device *, struct cfdata *, void *);
     88        1.7     bjh21 static void qms_attach(struct device *, struct device *, void *);
     89        1.7     bjh21 
     90        1.7     bjh21 static int qms_enable(void *);
     91       1.11  christos static int qms_ioctl(void *, u_long, void *, int, struct lwp *);
     92        1.7     bjh21 static void qms_disable(void *cookie);
     93        1.7     bjh21 static void qms_intr(void *arg);
     94        1.1   reinoud 
     95        1.7     bjh21 CFATTACH_DECL(qms, sizeof(struct qms_softc),
     96        1.7     bjh21     qms_match, qms_attach, NULL, NULL);
     97        1.1   reinoud 
     98        1.7     bjh21 static struct wsmouse_accessops qms_accessops = {
     99        1.7     bjh21 	qms_enable, qms_ioctl, qms_disable
    100        1.7     bjh21 };
    101        1.1   reinoud 
    102        1.1   reinoud 
    103        1.7     bjh21 static int
    104        1.7     bjh21 qms_match(struct device *parent, struct cfdata *cf, void *aux)
    105        1.1   reinoud {
    106        1.7     bjh21 	struct qms_attach_args *qa = aux;
    107        1.1   reinoud 
    108        1.7     bjh21 	if (strcmp(qa->qa_name, "qms") == 0)
    109        1.9     bjh21 		return 1;
    110        1.1   reinoud 
    111        1.9     bjh21 	return 0;
    112        1.1   reinoud }
    113        1.1   reinoud 
    114        1.1   reinoud 
    115        1.7     bjh21 static void
    116        1.7     bjh21 qms_attach(struct device *parent, struct device *self, void *aux)
    117        1.1   reinoud {
    118        1.7     bjh21 	struct qms_softc *sc = (void *)self;
    119        1.7     bjh21 	struct qms_attach_args *qa = aux;
    120        1.7     bjh21 	struct wsmousedev_attach_args wsmouseargs;
    121        1.1   reinoud 
    122        1.7     bjh21 	sc->sc_iot = qa->qa_iot;
    123        1.7     bjh21 	sc->sc_ioh = qa->qa_ioh;
    124        1.7     bjh21 	sc->sc_butioh = qa->qa_ioh_but;
    125        1.1   reinoud 
    126        1.7     bjh21 	/* set up wsmouse attach arguments */
    127        1.7     bjh21 	wsmouseargs.accessops = &qms_accessops;
    128        1.7     bjh21 	wsmouseargs.accesscookie = sc;
    129        1.1   reinoud 
    130        1.7     bjh21 	printf("\n");
    131        1.1   reinoud 
    132        1.7     bjh21 	sc->sc_wsmousedev =
    133        1.7     bjh21 	    config_found(&sc->sc_dev, &wsmouseargs, wsmousedevprint);
    134        1.1   reinoud 
    135  1.11.10.1   garbled 	callout_init(&sc->sc_callout, 0);
    136        1.1   reinoud }
    137        1.1   reinoud 
    138        1.1   reinoud 
    139        1.7     bjh21 static int
    140        1.7     bjh21 qms_enable(void *cookie)
    141        1.1   reinoud {
    142        1.7     bjh21 	struct qms_softc *sc = cookie;
    143        1.7     bjh21 	int b;
    144        1.1   reinoud 
    145        1.7     bjh21 	/* We don't want the mouse to warp on open. */
    146        1.7     bjh21 	sc->lastx = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
    147        1.7     bjh21 	sc->lasty = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
    148        1.7     bjh21 	b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
    149        1.1   reinoud 
    150        1.7     bjh21 	/* patch up the buttons */
    151        1.1   reinoud 	b >>= 4;
    152        1.7     bjh21 	sc->lastb = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
    153        1.1   reinoud 
    154        1.7     bjh21 	callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
    155        1.7     bjh21 	return 0;
    156        1.1   reinoud }
    157        1.1   reinoud 
    158        1.1   reinoud 
    159        1.7     bjh21 static void
    160        1.7     bjh21 qms_disable(void *cookie)
    161        1.1   reinoud {
    162        1.7     bjh21 	struct qms_softc *sc = cookie;
    163        1.1   reinoud 
    164        1.7     bjh21 	callout_stop(&sc->sc_callout);
    165        1.1   reinoud }
    166        1.1   reinoud 
    167        1.1   reinoud 
    168        1.7     bjh21 static int
    169       1.11  christos qms_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
    170        1.1   reinoud {
    171        1.1   reinoud 
    172        1.7     bjh21 	switch (cmd) {
    173        1.7     bjh21 	case WSMOUSEIO_GTYPE:
    174        1.7     bjh21 		*(int *)data = WSMOUSE_TYPE_ARCHIMEDES;
    175        1.7     bjh21 		return 0;
    176        1.1   reinoud 	}
    177        1.1   reinoud 
    178        1.7     bjh21 	return EPASSTHROUGH;
    179        1.1   reinoud }
    180        1.3  jdolecek 
    181        1.7     bjh21 
    182        1.3  jdolecek static void
    183        1.7     bjh21 qms_intr(void *arg)
    184        1.3  jdolecek {
    185        1.7     bjh21 	struct qms_softc *sc = arg;
    186        1.7     bjh21 	int b;
    187        1.7     bjh21 	u_int16_t x, y;
    188        1.7     bjh21 	int16_t dx, dy;
    189        1.7     bjh21 
    190        1.7     bjh21 	x = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
    191        1.7     bjh21 	y = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
    192        1.7     bjh21 	b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
    193        1.3  jdolecek 
    194        1.7     bjh21 	/* patch up the buttons */
    195        1.7     bjh21 	b >>= 4;
    196        1.7     bjh21 	b = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
    197        1.3  jdolecek 
    198        1.7     bjh21 	if ((x != sc->lastx) || (y != sc->lasty) || (b != sc->lastb)) {
    199        1.7     bjh21 		/* This assumes that int16_t is two's complement. */
    200        1.7     bjh21 		dx = x - sc->lastx;
    201        1.7     bjh21 		dy = y - sc->lasty;
    202       1.10    plunky 		wsmouse_input(sc->sc_wsmousedev,
    203       1.10    plunky 				b,
    204       1.10    plunky 				dx, dy, 0, 0,
    205       1.10    plunky 				WSMOUSE_INPUT_DELTA);
    206        1.3  jdolecek 
    207        1.7     bjh21 		/* save old values */
    208        1.7     bjh21 		sc->lastx = x;
    209        1.7     bjh21 		sc->lasty = y;
    210        1.7     bjh21 		sc->lastb = b;
    211        1.9     bjh21 	}
    212        1.7     bjh21 	callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
    213        1.3  jdolecek }
    214        1.3  jdolecek 
    215        1.1   reinoud 
    216        1.7     bjh21 /* end of qms.c */
    217