Home | History | Annotate | Line # | Download | only in isa
mms.c revision 1.35
      1 /*	$NetBSD: mms.c,v 1.35 2000/01/08 02:57:25 takemura 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/param.h>
     27 #include <sys/systm.h>
     28 #include <sys/ioctl.h>
     29 #include <sys/device.h>
     30 
     31 #include <machine/intr.h>
     32 #include <machine/bus.h>
     33 
     34 #include <dev/isa/isavar.h>
     35 
     36 #include <dev/wscons/wsconsio.h>
     37 #include <dev/wscons/wsmousevar.h>
     38 
     39 #define	MMS_ADDR	0	/* offset for register select */
     40 #define	MMS_DATA	1	/* offset for InPort data */
     41 #define	MMS_IDENT	2	/* offset for identification register */
     42 #define	MMS_NPORTS	4
     43 
     44 struct mms_softc {		/* driver status information */
     45 	struct device sc_dev;
     46 	void *sc_ih;
     47 
     48 	bus_space_tag_t sc_iot;
     49 	bus_space_handle_t sc_ioh;
     50 
     51 	int sc_enabled; /* device is open */
     52 
     53 	struct device *sc_wsmousedev;
     54 };
     55 
     56 int mmsprobe __P((struct device *, struct cfdata *, void *));
     57 void mmsattach __P((struct device *, struct device *, void *));
     58 int mmsintr __P((void *));
     59 
     60 struct cfattach mms_ca = {
     61 	sizeof(struct mms_softc), mmsprobe, mmsattach
     62 };
     63 
     64 int	mms_enable __P((void *));
     65 int	mms_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     66 void	mms_disable __P((void *));
     67 
     68 const struct wsmouse_accessops mms_accessops = {
     69 	mms_enable,
     70 	mms_ioctl,
     71 	mms_disable,
     72 };
     73 
     74 int
     75 mmsprobe(parent, match, aux)
     76 	struct device *parent;
     77 	struct cfdata *match;
     78 	void *aux;
     79 {
     80 	struct isa_attach_args *ia = aux;
     81 	bus_space_tag_t iot = ia->ia_iot;
     82 	bus_space_handle_t ioh;
     83 	int rv;
     84 
     85 	/* Disallow wildcarded i/o address. */
     86 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
     87 		return 0;
     88 
     89 	/* Map the i/o space. */
     90 	if (bus_space_map(iot, ia->ia_iobase, MMS_NPORTS, 0, &ioh))
     91 		return 0;
     92 
     93 	rv = 0;
     94 
     95 	/* Read identification register to see if present */
     96 	if (bus_space_read_1(iot, ioh, MMS_IDENT) != 0xde)
     97 		goto out;
     98 
     99 	/* Seems it was there; reset. */
    100 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x87);
    101 
    102 	rv = 1;
    103 	ia->ia_iosize = MMS_NPORTS;
    104 	ia->ia_msize = 0;
    105 
    106 out:
    107 	bus_space_unmap(iot, ioh, MMS_NPORTS);
    108 	return rv;
    109 }
    110 
    111 void
    112 mmsattach(parent, self, aux)
    113 	struct device *parent, *self;
    114 	void *aux;
    115 {
    116 	struct mms_softc *sc = (void *)self;
    117 	struct isa_attach_args *ia = aux;
    118 	bus_space_tag_t iot = ia->ia_iot;
    119 	bus_space_handle_t ioh;
    120 	struct wsmousedev_attach_args a;
    121 
    122 	printf("\n");
    123 
    124 	if (bus_space_map(iot, ia->ia_iobase, MMS_NPORTS, 0, &ioh)) {
    125 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    126 		return;
    127 	}
    128 
    129 	/* Other initialization was done by mmsprobe. */
    130 	sc->sc_iot = iot;
    131 	sc->sc_ioh = ioh;
    132 	sc->sc_enabled = 0;
    133 
    134 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
    135 	    IPL_TTY, mmsintr, sc);
    136 
    137 	a.accessops = &mms_accessops;
    138 	a.accesscookie = sc;
    139 
    140 	/*
    141 	 * Attach the wsmouse, saving a handle to it.
    142 	 * Note that we don't need to check this pointer against NULL
    143 	 * here or in psmintr, because if this fails lms_enable() will
    144 	 * never be called, so lmsintr() will never be called.
    145 	 */
    146 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    147 }
    148 
    149 int
    150 mms_enable(v)
    151 	void *v;
    152 {
    153 	struct mms_softc *sc = v;
    154 
    155 	if (sc->sc_enabled)
    156 		return EBUSY;
    157 
    158 	sc->sc_enabled = 1;
    159 
    160 	/* Enable interrupts. */
    161 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x07);
    162 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_DATA, 0x09);
    163 
    164 	return 0;
    165 }
    166 
    167 void
    168 mms_disable(v)
    169 	void *v;
    170 {
    171 	struct mms_softc *sc = v;
    172 
    173 	/* Disable interrupts. */
    174 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x87);
    175 
    176 	sc->sc_enabled = 0;
    177 }
    178 
    179 int
    180 mms_ioctl(v, cmd, data, flag, p)
    181 	void *v;
    182 	u_long cmd;
    183 	caddr_t data;
    184 	int flag;
    185 	struct proc *p;
    186 {
    187 #if 0
    188 	struct mms_softc *sc = v;
    189 #endif
    190 
    191 	switch (cmd) {
    192 	case WSMOUSEIO_GTYPE:
    193 		*(u_int *)data = WSMOUSE_TYPE_MMS;
    194 		return (0);
    195 	}
    196 	return (-1);
    197 }
    198 
    199 int
    200 mmsintr(arg)
    201 	void *arg;
    202 {
    203 	struct mms_softc *sc = arg;
    204 	bus_space_tag_t iot = sc->sc_iot;
    205 	bus_space_handle_t ioh = sc->sc_ioh;
    206 	u_char status;
    207 	signed char dx, dy;
    208 	u_int buttons;
    209 	int changed;
    210 
    211 	if (!sc->sc_enabled)
    212 		/* Interrupts are not expected. */
    213 		return 0;
    214 
    215 	/* Freeze InPort registers (disabling interrupts). */
    216 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
    217 	bus_space_write_1(iot, ioh, MMS_DATA, 0x29);
    218 
    219 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x00);
    220 	status = bus_space_read_1(iot, ioh, MMS_DATA);
    221 
    222 	if (status & 0x40) {
    223 		bus_space_write_1(iot, ioh, MMS_ADDR, 1);
    224 		dx = bus_space_read_1(iot, ioh, MMS_DATA);
    225 		/* Bounding at -127 avoids a bug in XFree86. */
    226 		dx = (dx == -128) ? -127 : dx;
    227 
    228 		bus_space_write_1(iot, ioh, MMS_ADDR, 2);
    229 		dy = bus_space_read_1(iot, ioh, MMS_DATA);
    230 		dy = (dy == -128) ? 127 : -dy;
    231 	} else
    232 		dx = dy = 0;
    233 
    234 	/* Unfreeze InPort registers (reenabling interrupts). */
    235 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
    236 	bus_space_write_1(iot, ioh, MMS_DATA, 0x09);
    237 
    238 	buttons = ((status & 0x04) ? 0x1 : 0) |
    239 		((status & 0x02) ? 0x2 : 0) |
    240 		((status & 0x01) ? 0x4 : 0);
    241 	changed = status & 0x38;
    242 
    243 	if (dx || dy || changed)
    244 		wsmouse_input(sc->sc_wsmousedev,
    245 			      buttons, dx, dy, 0, WSMOUSE_INPUT_DELTA);
    246 
    247 	return -1;
    248 }
    249