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