Home | History | Annotate | Line # | Download | only in isa
lms.c revision 1.54.8.1
      1 /*	$NetBSD: lms.c,v 1.54.8.1 2009/05/13 17:17:50 jym 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: lms.c,v 1.54.8.1 2009/05/13 17:17:50 jym 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/bus.h>
     35 #include <machine/intr.h>
     36 
     37 #include <dev/isa/isavar.h>
     38 
     39 #include <dev/wscons/wsconsio.h>
     40 #include <dev/wscons/wsmousevar.h>
     41 
     42 #define	LMS_DATA	0       /* offset for data port, read-only */
     43 #define	LMS_SIGN	1       /* offset for signature port, read-write */
     44 #define	LMS_INTR	2       /* offset for interrupt port, read-only */
     45 #define	LMS_CNTRL	2       /* offset for control port, write-only */
     46 #define	LMS_CONFIG	3	/* for configuration port, read-write */
     47 #define	LMS_NPORTS	4
     48 
     49 struct lms_softc {		/* driver status information */
     50 	void *sc_ih;
     51 
     52 	bus_space_tag_t sc_iot;		/* bus i/o space identifier */
     53 	bus_space_handle_t sc_ioh;	/* bus i/o handle */
     54 
     55 	int sc_enabled; /* device is open */
     56 	int oldbuttons;	/* mouse button status */
     57 
     58 	device_t sc_wsmousedev;
     59 };
     60 
     61 static int lmsprobe(device_t, cfdata_t, void *);
     62 static void lmsattach(device_t, device_t, void *);
     63 static int lmsintr(void *);
     64 
     65 CFATTACH_DECL_NEW(lms, sizeof(struct lms_softc),
     66     lmsprobe, lmsattach, NULL, NULL);
     67 
     68 static int	lms_enable(void *);
     69 static int	lms_ioctl(void *, u_long, void *, int, struct lwp *);
     70 static void	lms_disable(void *);
     71 
     72 static const struct wsmouse_accessops lms_accessops = {
     73 	lms_enable,
     74 	lms_ioctl,
     75 	lms_disable,
     76 };
     77 
     78 static int
     79 lmsprobe(device_t parent, cfdata_t match, void *aux)
     80 {
     81 	struct isa_attach_args *ia = aux;
     82 	bus_space_tag_t iot = ia->ia_iot;
     83 	bus_space_handle_t ioh;
     84 	int rv;
     85 
     86 	if (ia->ia_nio < 1)
     87 		return (0);
     88 	if (ia->ia_nirq < 1)
     89 		return (0);
     90 
     91 	if (ISA_DIRECT_CONFIG(ia))
     92 		return (0);
     93 
     94 	/* Disallow wildcarded i/o base. */
     95 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
     96 		return 0;
     97 	if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
     98 		return 0;
     99 
    100 	/* Map the i/o space. */
    101 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, LMS_NPORTS, 0, &ioh))
    102 		return 0;
    103 
    104 	rv = 0;
    105 
    106 	/* Configure and check for port present. */
    107 	bus_space_write_1(iot, ioh, LMS_CONFIG, 0x91);
    108 	delay(10);
    109 	bus_space_write_1(iot, ioh, LMS_SIGN, 0x0c);
    110 	delay(10);
    111 	if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x0c)
    112 		goto out;
    113 	bus_space_write_1(iot, ioh, LMS_SIGN, 0x50);
    114 	delay(10);
    115 	if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x50)
    116 		goto out;
    117 
    118 	/* Disable interrupts. */
    119 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0x10);
    120 
    121 	rv = 1;
    122 	ia->ia_nio = 1;
    123 	ia->ia_io[0].ir_size = LMS_NPORTS;
    124 
    125 	ia->ia_nirq = 1;
    126 
    127 	ia->ia_niomem = 0;
    128 	ia->ia_ndrq = 0;
    129 
    130 out:
    131 	bus_space_unmap(iot, ioh, LMS_NPORTS);
    132 	return rv;
    133 }
    134 
    135 static void
    136 lmsattach(device_t parent, device_t self, void *aux)
    137 {
    138 	struct lms_softc *sc = device_private(self);
    139 	struct isa_attach_args *ia = aux;
    140 	bus_space_tag_t iot = ia->ia_iot;
    141 	bus_space_handle_t ioh;
    142 	struct wsmousedev_attach_args a;
    143 
    144 	aprint_naive(": Mouse\n");
    145 	aprint_normal(": Logitech Mouse\n");
    146 
    147 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, LMS_NPORTS, 0, &ioh)) {
    148 		aprint_error_dev(self, "can't map i/o space\n");
    149 		return;
    150 	}
    151 
    152 	/* Other initialization was done by lmsprobe. */
    153 	sc->sc_iot = iot;
    154 	sc->sc_ioh = ioh;
    155 	sc->sc_enabled = 0;
    156 
    157 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
    158 	    IST_PULSE, IPL_TTY, lmsintr, sc);
    159 
    160 	a.accessops = &lms_accessops;
    161 	a.accesscookie = sc;
    162 
    163 	/*
    164 	 * Attach the wsmouse, saving a handle to it.
    165 	 * Note that we don't need to check this pointer against NULL
    166 	 * here or in psmintr, because if this fails lms_enable() will
    167 	 * never be called, so lmsintr() will never be called.
    168 	 */
    169 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    170 }
    171 
    172 static int
    173 lms_enable(void *v)
    174 {
    175 	struct lms_softc *sc = v;
    176 
    177 	if (sc->sc_enabled)
    178 		return EBUSY;
    179 
    180 	sc->sc_enabled = 1;
    181 	sc->oldbuttons = 0;
    182 
    183 	/* Enable interrupts. */
    184 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0);
    185 
    186 	return 0;
    187 }
    188 
    189 static void
    190 lms_disable(void *v)
    191 {
    192 	struct lms_softc *sc = v;
    193 
    194 	/* Disable interrupts. */
    195 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0x10);
    196 
    197 	sc->sc_enabled = 0;
    198 }
    199 
    200 static int
    201 lms_ioctl(void *v, u_long cmd, void *data, int flag,
    202     struct lwp *l)
    203 {
    204 #if 0
    205 	struct lms_softc *sc = v;
    206 #endif
    207 
    208 	switch (cmd) {
    209 	case WSMOUSEIO_GTYPE:
    210 		*(u_int *)data = WSMOUSE_TYPE_LMS;
    211 		return (0);
    212 	}
    213 	return (EPASSTHROUGH);
    214 }
    215 
    216 static int
    217 lmsintr(void *arg)
    218 {
    219 	struct lms_softc *sc = arg;
    220 	bus_space_tag_t iot = sc->sc_iot;
    221 	bus_space_handle_t ioh = sc->sc_ioh;
    222 	u_char hi, lo;
    223 	signed char dx, dy;
    224 	u_int buttons;
    225 	int changed;
    226 
    227 	if (!sc->sc_enabled)
    228 		/* Interrupts are not expected. */
    229 		return 0;
    230 
    231 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0xab);
    232 	hi = bus_space_read_1(iot, ioh, LMS_DATA);
    233 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0x90);
    234 	lo = bus_space_read_1(iot, ioh, LMS_DATA);
    235 	dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
    236 	/* Bounding at -127 avoids a bug in XFree86. */
    237 	dx = (dx == -128) ? -127 : dx;
    238 
    239 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0xf0);
    240 	hi = bus_space_read_1(iot, ioh, LMS_DATA);
    241 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0xd0);
    242 	lo = bus_space_read_1(iot, ioh, LMS_DATA);
    243 	dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
    244 	dy = (dy == -128) ? 127 : -dy;
    245 
    246 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0);
    247 
    248 	buttons = ((hi & 0x80) ? 0 : 0x1) |
    249 		((hi & 0x40) ? 0 : 0x2) |
    250 		((hi & 0x20) ? 0 : 0x4);
    251 	changed = (buttons ^ sc->oldbuttons);
    252 	sc->oldbuttons = buttons;
    253 
    254 	if (dx || dy || changed)
    255 		wsmouse_input(sc->sc_wsmousedev,
    256 				buttons,
    257 				dx, dy, 0, 0,
    258 				WSMOUSE_INPUT_DELTA);
    259 
    260 	return -1;
    261 }
    262