Home | History | Annotate | Line # | Download | only in isa
mms.c revision 1.36
      1 /*	$NetBSD: mms.c,v 1.36 2001/11/15 07:03:32 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994 Charles M. Hannum.
      5  * Copyright (c) 1992, 1993 Erik Forsberg.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
     15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
     17  * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include <sys/cdefs.h>
     27 __KERNEL_RCSID(0, "$NetBSD: mms.c,v 1.36 2001/11/15 07:03:32 lukem Exp $");
     28 
     29 #include <sys/param.h>
     30 #include <sys/systm.h>
     31 #include <sys/ioctl.h>
     32 #include <sys/device.h>
     33 
     34 #include <machine/intr.h>
     35 #include <machine/bus.h>
     36 
     37 #include <dev/isa/isavar.h>
     38 
     39 #include <dev/wscons/wsconsio.h>
     40 #include <dev/wscons/wsmousevar.h>
     41 
     42 #define	MMS_ADDR	0	/* offset for register select */
     43 #define	MMS_DATA	1	/* offset for InPort data */
     44 #define	MMS_IDENT	2	/* offset for identification register */
     45 #define	MMS_NPORTS	4
     46 
     47 struct mms_softc {		/* driver status information */
     48 	struct device sc_dev;
     49 	void *sc_ih;
     50 
     51 	bus_space_tag_t sc_iot;
     52 	bus_space_handle_t sc_ioh;
     53 
     54 	int sc_enabled; /* device is open */
     55 
     56 	struct device *sc_wsmousedev;
     57 };
     58 
     59 int mmsprobe __P((struct device *, struct cfdata *, void *));
     60 void mmsattach __P((struct device *, struct device *, void *));
     61 int mmsintr __P((void *));
     62 
     63 struct cfattach mms_ca = {
     64 	sizeof(struct mms_softc), mmsprobe, mmsattach
     65 };
     66 
     67 int	mms_enable __P((void *));
     68 int	mms_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     69 void	mms_disable __P((void *));
     70 
     71 const struct wsmouse_accessops mms_accessops = {
     72 	mms_enable,
     73 	mms_ioctl,
     74 	mms_disable,
     75 };
     76 
     77 int
     78 mmsprobe(parent, match, aux)
     79 	struct device *parent;
     80 	struct cfdata *match;
     81 	void *aux;
     82 {
     83 	struct isa_attach_args *ia = aux;
     84 	bus_space_tag_t iot = ia->ia_iot;
     85 	bus_space_handle_t ioh;
     86 	int rv;
     87 
     88 	/* Disallow wildcarded i/o address. */
     89 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
     90 		return 0;
     91 
     92 	/* Map the i/o space. */
     93 	if (bus_space_map(iot, ia->ia_iobase, MMS_NPORTS, 0, &ioh))
     94 		return 0;
     95 
     96 	rv = 0;
     97 
     98 	/* Read identification register to see if present */
     99 	if (bus_space_read_1(iot, ioh, MMS_IDENT) != 0xde)
    100 		goto out;
    101 
    102 	/* Seems it was there; reset. */
    103 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x87);
    104 
    105 	rv = 1;
    106 	ia->ia_iosize = MMS_NPORTS;
    107 	ia->ia_msize = 0;
    108 
    109 out:
    110 	bus_space_unmap(iot, ioh, MMS_NPORTS);
    111 	return rv;
    112 }
    113 
    114 void
    115 mmsattach(parent, self, aux)
    116 	struct device *parent, *self;
    117 	void *aux;
    118 {
    119 	struct mms_softc *sc = (void *)self;
    120 	struct isa_attach_args *ia = aux;
    121 	bus_space_tag_t iot = ia->ia_iot;
    122 	bus_space_handle_t ioh;
    123 	struct wsmousedev_attach_args a;
    124 
    125 	printf("\n");
    126 
    127 	if (bus_space_map(iot, ia->ia_iobase, MMS_NPORTS, 0, &ioh)) {
    128 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    129 		return;
    130 	}
    131 
    132 	/* Other initialization was done by mmsprobe. */
    133 	sc->sc_iot = iot;
    134 	sc->sc_ioh = ioh;
    135 	sc->sc_enabled = 0;
    136 
    137 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
    138 	    IPL_TTY, mmsintr, sc);
    139 
    140 	a.accessops = &mms_accessops;
    141 	a.accesscookie = sc;
    142 
    143 	/*
    144 	 * Attach the wsmouse, saving a handle to it.
    145 	 * Note that we don't need to check this pointer against NULL
    146 	 * here or in psmintr, because if this fails lms_enable() will
    147 	 * never be called, so lmsintr() will never be called.
    148 	 */
    149 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    150 }
    151 
    152 int
    153 mms_enable(v)
    154 	void *v;
    155 {
    156 	struct mms_softc *sc = v;
    157 
    158 	if (sc->sc_enabled)
    159 		return EBUSY;
    160 
    161 	sc->sc_enabled = 1;
    162 
    163 	/* Enable interrupts. */
    164 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x07);
    165 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_DATA, 0x09);
    166 
    167 	return 0;
    168 }
    169 
    170 void
    171 mms_disable(v)
    172 	void *v;
    173 {
    174 	struct mms_softc *sc = v;
    175 
    176 	/* Disable interrupts. */
    177 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x87);
    178 
    179 	sc->sc_enabled = 0;
    180 }
    181 
    182 int
    183 mms_ioctl(v, cmd, data, flag, p)
    184 	void *v;
    185 	u_long cmd;
    186 	caddr_t data;
    187 	int flag;
    188 	struct proc *p;
    189 {
    190 #if 0
    191 	struct mms_softc *sc = v;
    192 #endif
    193 
    194 	switch (cmd) {
    195 	case WSMOUSEIO_GTYPE:
    196 		*(u_int *)data = WSMOUSE_TYPE_MMS;
    197 		return (0);
    198 	}
    199 	return (-1);
    200 }
    201 
    202 int
    203 mmsintr(arg)
    204 	void *arg;
    205 {
    206 	struct mms_softc *sc = arg;
    207 	bus_space_tag_t iot = sc->sc_iot;
    208 	bus_space_handle_t ioh = sc->sc_ioh;
    209 	u_char status;
    210 	signed char dx, dy;
    211 	u_int buttons;
    212 	int changed;
    213 
    214 	if (!sc->sc_enabled)
    215 		/* Interrupts are not expected. */
    216 		return 0;
    217 
    218 	/* Freeze InPort registers (disabling interrupts). */
    219 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
    220 	bus_space_write_1(iot, ioh, MMS_DATA, 0x29);
    221 
    222 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x00);
    223 	status = bus_space_read_1(iot, ioh, MMS_DATA);
    224 
    225 	if (status & 0x40) {
    226 		bus_space_write_1(iot, ioh, MMS_ADDR, 1);
    227 		dx = bus_space_read_1(iot, ioh, MMS_DATA);
    228 		/* Bounding at -127 avoids a bug in XFree86. */
    229 		dx = (dx == -128) ? -127 : dx;
    230 
    231 		bus_space_write_1(iot, ioh, MMS_ADDR, 2);
    232 		dy = bus_space_read_1(iot, ioh, MMS_DATA);
    233 		dy = (dy == -128) ? 127 : -dy;
    234 	} else
    235 		dx = dy = 0;
    236 
    237 	/* Unfreeze InPort registers (reenabling interrupts). */
    238 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
    239 	bus_space_write_1(iot, ioh, MMS_DATA, 0x09);
    240 
    241 	buttons = ((status & 0x04) ? 0x1 : 0) |
    242 		((status & 0x02) ? 0x2 : 0) |
    243 		((status & 0x01) ? 0x4 : 0);
    244 	changed = status & 0x38;
    245 
    246 	if (dx || dy || changed)
    247 		wsmouse_input(sc->sc_wsmousedev,
    248 			      buttons, dx, dy, 0, WSMOUSE_INPUT_DELTA);
    249 
    250 	return -1;
    251 }
    252