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