1*7769e6a4Smpi /* $OpenBSD: mms.c,v 1.21 2022/02/21 10:24:28 mpi Exp $ */
229671eb9Sjbm /* $NetBSD: mms.c,v 1.35 2000/01/08 02:57:25 takemura Exp $ */
329671eb9Sjbm
429671eb9Sjbm /*-
529671eb9Sjbm * Copyright (c) 1993, 1994 Charles M. Hannum.
629671eb9Sjbm * Copyright (c) 1992, 1993 Erik Forsberg.
729671eb9Sjbm * All rights reserved.
829671eb9Sjbm *
929671eb9Sjbm * Redistribution and use in source and binary forms, with or without
1029671eb9Sjbm * modification, are permitted provided that the following conditions
1129671eb9Sjbm * are met:
1229671eb9Sjbm * 1. Redistributions of source code must retain the above copyright
1329671eb9Sjbm * notice, this list of conditions and the following disclaimer.
1429671eb9Sjbm *
1529671eb9Sjbm * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
1629671eb9Sjbm * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1729671eb9Sjbm * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
1829671eb9Sjbm * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1929671eb9Sjbm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2029671eb9Sjbm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2129671eb9Sjbm * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2229671eb9Sjbm * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2329671eb9Sjbm * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2429671eb9Sjbm * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2529671eb9Sjbm */
2629671eb9Sjbm
2729671eb9Sjbm #include <sys/param.h>
2829671eb9Sjbm #include <sys/systm.h>
2929671eb9Sjbm #include <sys/ioctl.h>
3029671eb9Sjbm #include <sys/device.h>
3129671eb9Sjbm
3229671eb9Sjbm #include <machine/intr.h>
3329671eb9Sjbm #include <machine/bus.h>
3429671eb9Sjbm
3529671eb9Sjbm #include <dev/isa/isavar.h>
3629671eb9Sjbm
3729671eb9Sjbm #include <dev/wscons/wsconsio.h>
3829671eb9Sjbm #include <dev/wscons/wsmousevar.h>
3929671eb9Sjbm
4029671eb9Sjbm #define MMS_ADDR 0 /* offset for register select */
4129671eb9Sjbm #define MMS_DATA 1 /* offset for InPort data */
4229671eb9Sjbm #define MMS_IDENT 2 /* offset for identification register */
4329671eb9Sjbm #define MMS_NPORTS 4
4429671eb9Sjbm
4529671eb9Sjbm struct mms_softc { /* driver status information */
4629671eb9Sjbm struct device sc_dev;
4729671eb9Sjbm void *sc_ih;
4829671eb9Sjbm
4929671eb9Sjbm bus_space_tag_t sc_iot;
5029671eb9Sjbm bus_space_handle_t sc_ioh;
5129671eb9Sjbm
5229671eb9Sjbm int sc_enabled; /* device is open */
5329671eb9Sjbm
5429671eb9Sjbm struct device *sc_wsmousedev;
5529671eb9Sjbm };
5629671eb9Sjbm
57c4071fd1Smillert int mmsprobe(struct device *, void *, void *);
58c4071fd1Smillert void mmsattach(struct device *, struct device *, void *);
59c4071fd1Smillert int mmsintr(void *);
6029671eb9Sjbm
61*7769e6a4Smpi const struct cfattach mms_ca = {
6229671eb9Sjbm sizeof(struct mms_softc), mmsprobe, mmsattach
6329671eb9Sjbm };
6429671eb9Sjbm
65c4071fd1Smillert int mms_enable(void *);
66c4071fd1Smillert int mms_ioctl(void *, u_long, caddr_t, int, struct proc *);
67c4071fd1Smillert void mms_disable(void *);
6829671eb9Sjbm
6929671eb9Sjbm const struct wsmouse_accessops mms_accessops = {
7029671eb9Sjbm mms_enable,
7129671eb9Sjbm mms_ioctl,
7229671eb9Sjbm mms_disable,
7329671eb9Sjbm };
7429671eb9Sjbm
7529671eb9Sjbm int
mmsprobe(struct device * parent,void * match,void * aux)7608f75f72Sjsg mmsprobe(struct device *parent, void *match, void *aux)
7729671eb9Sjbm {
7829671eb9Sjbm struct isa_attach_args *ia = aux;
7929671eb9Sjbm bus_space_tag_t iot = ia->ia_iot;
8029671eb9Sjbm bus_space_handle_t ioh;
8129671eb9Sjbm int rv;
8229671eb9Sjbm
8329671eb9Sjbm /* Disallow wildcarded i/o address. */
8429671eb9Sjbm if (ia->ia_iobase == IOBASEUNK)
8529671eb9Sjbm return 0;
8629671eb9Sjbm
8729671eb9Sjbm /* Map the i/o space. */
8829671eb9Sjbm if (bus_space_map(iot, ia->ia_iobase, MMS_NPORTS, 0, &ioh))
8929671eb9Sjbm return 0;
9029671eb9Sjbm
9129671eb9Sjbm rv = 0;
9229671eb9Sjbm
9329671eb9Sjbm /* Read identification register to see if present */
9429671eb9Sjbm if (bus_space_read_1(iot, ioh, MMS_IDENT) != 0xde)
9529671eb9Sjbm goto out;
9629671eb9Sjbm
9729671eb9Sjbm /* Seems it was there; reset. */
9829671eb9Sjbm bus_space_write_1(iot, ioh, MMS_ADDR, 0x87);
9929671eb9Sjbm
10029671eb9Sjbm rv = 1;
10129671eb9Sjbm ia->ia_iosize = MMS_NPORTS;
10229671eb9Sjbm ia->ia_msize = 0;
10329671eb9Sjbm
10429671eb9Sjbm out:
10529671eb9Sjbm bus_space_unmap(iot, ioh, MMS_NPORTS);
10629671eb9Sjbm return rv;
10729671eb9Sjbm }
10829671eb9Sjbm
10929671eb9Sjbm void
mmsattach(struct device * parent,struct device * self,void * aux)11008f75f72Sjsg mmsattach(struct device *parent, struct device *self, void *aux)
11129671eb9Sjbm {
11229671eb9Sjbm struct mms_softc *sc = (void *)self;
11329671eb9Sjbm struct isa_attach_args *ia = aux;
11429671eb9Sjbm bus_space_tag_t iot = ia->ia_iot;
11529671eb9Sjbm bus_space_handle_t ioh;
11629671eb9Sjbm struct wsmousedev_attach_args a;
11729671eb9Sjbm
11829671eb9Sjbm printf("\n");
11929671eb9Sjbm
12029671eb9Sjbm if (bus_space_map(iot, ia->ia_iobase, MMS_NPORTS, 0, &ioh)) {
12129671eb9Sjbm printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
12229671eb9Sjbm return;
12329671eb9Sjbm }
12429671eb9Sjbm
12529671eb9Sjbm /* Other initialization was done by mmsprobe. */
12629671eb9Sjbm sc->sc_iot = iot;
12729671eb9Sjbm sc->sc_ioh = ioh;
12829671eb9Sjbm sc->sc_enabled = 0;
12929671eb9Sjbm
13029671eb9Sjbm sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
13129671eb9Sjbm IPL_TTY, mmsintr, sc, sc->sc_dev.dv_xname);
13229671eb9Sjbm
13329671eb9Sjbm a.accessops = &mms_accessops;
13429671eb9Sjbm a.accesscookie = sc;
13529671eb9Sjbm
13629671eb9Sjbm /*
13729671eb9Sjbm * Attach the wsmouse, saving a handle to it.
13829671eb9Sjbm * Note that we don't need to check this pointer against NULL
13929671eb9Sjbm * here or in psmintr, because if this fails lms_enable() will
14029671eb9Sjbm * never be called, so lmsintr() will never be called.
14129671eb9Sjbm */
14229671eb9Sjbm sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
14329671eb9Sjbm }
14429671eb9Sjbm
14529671eb9Sjbm int
mms_enable(void * v)14608f75f72Sjsg mms_enable(void *v)
14729671eb9Sjbm {
14829671eb9Sjbm struct mms_softc *sc = v;
14929671eb9Sjbm
15029671eb9Sjbm if (sc->sc_enabled)
15129671eb9Sjbm return EBUSY;
15229671eb9Sjbm
15329671eb9Sjbm sc->sc_enabled = 1;
15429671eb9Sjbm
15529671eb9Sjbm /* Enable interrupts. */
15629671eb9Sjbm bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x07);
15729671eb9Sjbm bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_DATA, 0x09);
15829671eb9Sjbm
15929671eb9Sjbm return 0;
16029671eb9Sjbm }
16129671eb9Sjbm
16229671eb9Sjbm void
mms_disable(void * v)16308f75f72Sjsg mms_disable(void *v)
16429671eb9Sjbm {
16529671eb9Sjbm struct mms_softc *sc = v;
16629671eb9Sjbm
16729671eb9Sjbm /* Disable interrupts. */
16829671eb9Sjbm bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x87);
16929671eb9Sjbm
17029671eb9Sjbm sc->sc_enabled = 0;
17129671eb9Sjbm }
17229671eb9Sjbm
17329671eb9Sjbm int
mms_ioctl(void * v,u_long cmd,caddr_t data,int flag,struct proc * p)17408f75f72Sjsg mms_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
17529671eb9Sjbm {
17629671eb9Sjbm #if 0
17729671eb9Sjbm struct mms_softc *sc = v;
17829671eb9Sjbm #endif
17929671eb9Sjbm
18029671eb9Sjbm switch (cmd) {
18129671eb9Sjbm case WSMOUSEIO_GTYPE:
18229671eb9Sjbm *(u_int *)data = WSMOUSE_TYPE_MMS;
18329671eb9Sjbm return (0);
18429671eb9Sjbm }
18529671eb9Sjbm return (-1);
18629671eb9Sjbm }
18729671eb9Sjbm
18829671eb9Sjbm int
mmsintr(void * arg)18908f75f72Sjsg mmsintr(void *arg)
19029671eb9Sjbm {
19129671eb9Sjbm struct mms_softc *sc = arg;
19229671eb9Sjbm bus_space_tag_t iot = sc->sc_iot;
19329671eb9Sjbm bus_space_handle_t ioh = sc->sc_ioh;
19429671eb9Sjbm u_char status;
19529671eb9Sjbm signed char dx, dy;
19629671eb9Sjbm u_int buttons;
19729671eb9Sjbm int changed;
19829671eb9Sjbm
19929671eb9Sjbm if (!sc->sc_enabled)
20029671eb9Sjbm /* Interrupts are not expected. */
20129671eb9Sjbm return 0;
20229671eb9Sjbm
20329671eb9Sjbm /* Freeze InPort registers (disabling interrupts). */
20429671eb9Sjbm bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
20529671eb9Sjbm bus_space_write_1(iot, ioh, MMS_DATA, 0x29);
20629671eb9Sjbm
20729671eb9Sjbm bus_space_write_1(iot, ioh, MMS_ADDR, 0x00);
20829671eb9Sjbm status = bus_space_read_1(iot, ioh, MMS_DATA);
20929671eb9Sjbm
21029671eb9Sjbm if (status & 0x40) {
21129671eb9Sjbm bus_space_write_1(iot, ioh, MMS_ADDR, 1);
21229671eb9Sjbm dx = bus_space_read_1(iot, ioh, MMS_DATA);
21329671eb9Sjbm /* Bounding at -127 avoids a bug in XFree86. */
21429671eb9Sjbm dx = (dx == -128) ? -127 : dx;
21529671eb9Sjbm
21629671eb9Sjbm bus_space_write_1(iot, ioh, MMS_ADDR, 2);
21729671eb9Sjbm dy = bus_space_read_1(iot, ioh, MMS_DATA);
21829671eb9Sjbm dy = (dy == -128) ? 127 : -dy;
21929671eb9Sjbm } else
22029671eb9Sjbm dx = dy = 0;
22129671eb9Sjbm
22229671eb9Sjbm /* Unfreeze InPort registers (reenabling interrupts). */
22329671eb9Sjbm bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
22429671eb9Sjbm bus_space_write_1(iot, ioh, MMS_DATA, 0x09);
22529671eb9Sjbm
22629671eb9Sjbm buttons = ((status & 0x04) ? 0x1 : 0) |
22729671eb9Sjbm ((status & 0x02) ? 0x2 : 0) |
22829671eb9Sjbm ((status & 0x01) ? 0x4 : 0);
22929671eb9Sjbm changed = status & 0x38;
23029671eb9Sjbm
23129671eb9Sjbm if (dx || dy || changed)
23294712de5Sbru WSMOUSE_INPUT(sc->sc_wsmousedev, buttons, dx, dy, 0, 0);
23329671eb9Sjbm
23429671eb9Sjbm return -1;
23529671eb9Sjbm }
23629671eb9Sjbm
23729671eb9Sjbm struct cfdriver mms_cd = {
23829671eb9Sjbm NULL, "mms", DV_DULL
23929671eb9Sjbm };
240