1*7769e6a4Smpi /* $OpenBSD: lms.c,v 1.22 2022/02/21 10:24:28 mpi Exp $ */
229671eb9Sjbm /* $NetBSD: lms.c,v 1.38 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/bus.h>
3329671eb9Sjbm #include <machine/intr.h>
3429671eb9Sjbm
3529671eb9Sjbm #include <dev/isa/isavar.h>
3629671eb9Sjbm
3729671eb9Sjbm #include <dev/wscons/wsconsio.h>
3829671eb9Sjbm #include <dev/wscons/wsmousevar.h>
3929671eb9Sjbm
4029671eb9Sjbm #define LMS_DATA 0 /* offset for data port, read-only */
4129671eb9Sjbm #define LMS_SIGN 1 /* offset for signature port, read-write */
4229671eb9Sjbm #define LMS_INTR 2 /* offset for interrupt port, read-only */
4329671eb9Sjbm #define LMS_CNTRL 2 /* offset for control port, write-only */
4429671eb9Sjbm #define LMS_CONFIG 3 /* for configuration port, read-write */
4529671eb9Sjbm #define LMS_NPORTS 4
4629671eb9Sjbm
4729671eb9Sjbm struct lms_softc { /* driver status information */
4829671eb9Sjbm struct device sc_dev;
4929671eb9Sjbm void *sc_ih;
5029671eb9Sjbm
5129671eb9Sjbm bus_space_tag_t sc_iot; /* bus i/o space identifier */
5229671eb9Sjbm bus_space_handle_t sc_ioh; /* bus i/o handle */
5329671eb9Sjbm
5429671eb9Sjbm int sc_enabled; /* device is open */
5529671eb9Sjbm int oldbuttons; /* mouse button status */
5629671eb9Sjbm
5729671eb9Sjbm struct device *sc_wsmousedev;
5829671eb9Sjbm };
5929671eb9Sjbm
60c4071fd1Smillert int lmsprobe(struct device *, void *, void *);
61c4071fd1Smillert void lmsattach(struct device *, struct device *, void *);
62c4071fd1Smillert int lmsintr(void *);
6329671eb9Sjbm
64*7769e6a4Smpi const struct cfattach lms_ca = {
6529671eb9Sjbm sizeof(struct lms_softc), lmsprobe, lmsattach
6629671eb9Sjbm };
6729671eb9Sjbm
68c4071fd1Smillert int lms_enable(void *);
69c4071fd1Smillert int lms_ioctl(void *, u_long, caddr_t, int, struct proc *);
70c4071fd1Smillert void lms_disable(void *);
7129671eb9Sjbm
7229671eb9Sjbm const struct wsmouse_accessops lms_accessops = {
7329671eb9Sjbm lms_enable,
7429671eb9Sjbm lms_ioctl,
7529671eb9Sjbm lms_disable,
7629671eb9Sjbm };
7729671eb9Sjbm
7829671eb9Sjbm int
lmsprobe(struct device * parent,void * match,void * aux)7908f75f72Sjsg lmsprobe(struct device *parent, void *match, void *aux)
8029671eb9Sjbm {
8129671eb9Sjbm struct isa_attach_args *ia = aux;
8229671eb9Sjbm bus_space_tag_t iot = ia->ia_iot;
8329671eb9Sjbm bus_space_handle_t ioh;
8429671eb9Sjbm int rv;
8529671eb9Sjbm
8629671eb9Sjbm /* Disallow wildcarded i/o base. */
8729671eb9Sjbm if (ia->ia_iobase == IOBASEUNK)
8829671eb9Sjbm return 0;
8929671eb9Sjbm
9029671eb9Sjbm /* Map the i/o space. */
9129671eb9Sjbm if (bus_space_map(iot, ia->ia_iobase, LMS_NPORTS, 0, &ioh))
9229671eb9Sjbm return 0;
9329671eb9Sjbm
9429671eb9Sjbm rv = 0;
9529671eb9Sjbm
9629671eb9Sjbm /* Configure and check for port present. */
9729671eb9Sjbm bus_space_write_1(iot, ioh, LMS_CONFIG, 0x91);
9829671eb9Sjbm delay(10);
9929671eb9Sjbm bus_space_write_1(iot, ioh, LMS_SIGN, 0x0c);
10029671eb9Sjbm delay(10);
10129671eb9Sjbm if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x0c)
10229671eb9Sjbm goto out;
10329671eb9Sjbm bus_space_write_1(iot, ioh, LMS_SIGN, 0x50);
10429671eb9Sjbm delay(10);
10529671eb9Sjbm if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x50)
10629671eb9Sjbm goto out;
10729671eb9Sjbm
10829671eb9Sjbm /* Disable interrupts. */
10929671eb9Sjbm bus_space_write_1(iot, ioh, LMS_CNTRL, 0x10);
11029671eb9Sjbm
11129671eb9Sjbm rv = 1;
11229671eb9Sjbm ia->ia_iosize = LMS_NPORTS;
11329671eb9Sjbm ia->ia_msize = 0;
11429671eb9Sjbm
11529671eb9Sjbm out:
11629671eb9Sjbm bus_space_unmap(iot, ioh, LMS_NPORTS);
11729671eb9Sjbm return rv;
11829671eb9Sjbm }
11929671eb9Sjbm
12029671eb9Sjbm void
lmsattach(struct device * parent,struct device * self,void * aux)12108f75f72Sjsg lmsattach(struct device *parent, struct device *self, void *aux)
12229671eb9Sjbm {
12329671eb9Sjbm struct lms_softc *sc = (void *)self;
12429671eb9Sjbm struct isa_attach_args *ia = aux;
12529671eb9Sjbm bus_space_tag_t iot = ia->ia_iot;
12629671eb9Sjbm bus_space_handle_t ioh;
12729671eb9Sjbm struct wsmousedev_attach_args a;
12829671eb9Sjbm
12929671eb9Sjbm printf("\n");
13029671eb9Sjbm
13129671eb9Sjbm if (bus_space_map(iot, ia->ia_iobase, LMS_NPORTS, 0, &ioh)) {
13229671eb9Sjbm printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
13329671eb9Sjbm return;
13429671eb9Sjbm }
13529671eb9Sjbm
13629671eb9Sjbm /* Other initialization was done by lmsprobe. */
13729671eb9Sjbm sc->sc_iot = iot;
13829671eb9Sjbm sc->sc_ioh = ioh;
13929671eb9Sjbm sc->sc_enabled = 0;
14029671eb9Sjbm
14129671eb9Sjbm sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
14229671eb9Sjbm IPL_TTY, lmsintr, sc, sc->sc_dev.dv_xname);
14329671eb9Sjbm
14429671eb9Sjbm a.accessops = &lms_accessops;
14529671eb9Sjbm a.accesscookie = sc;
14629671eb9Sjbm
14729671eb9Sjbm /*
14829671eb9Sjbm * Attach the wsmouse, saving a handle to it.
14929671eb9Sjbm * Note that we don't need to check this pointer against NULL
15029671eb9Sjbm * here or in psmintr, because if this fails lms_enable() will
15129671eb9Sjbm * never be called, so lmsintr() will never be called.
15229671eb9Sjbm */
15329671eb9Sjbm sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
15429671eb9Sjbm }
15529671eb9Sjbm
15629671eb9Sjbm int
lms_enable(void * v)15708f75f72Sjsg lms_enable(void *v)
15829671eb9Sjbm {
15929671eb9Sjbm struct lms_softc *sc = v;
16029671eb9Sjbm
16129671eb9Sjbm if (sc->sc_enabled)
16229671eb9Sjbm return EBUSY;
16329671eb9Sjbm
16429671eb9Sjbm sc->sc_enabled = 1;
16529671eb9Sjbm sc->oldbuttons = 0;
16629671eb9Sjbm
16729671eb9Sjbm /* Enable interrupts. */
16829671eb9Sjbm bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0);
16929671eb9Sjbm
17029671eb9Sjbm return 0;
17129671eb9Sjbm }
17229671eb9Sjbm
17329671eb9Sjbm void
lms_disable(void * v)17408f75f72Sjsg lms_disable(void *v)
17529671eb9Sjbm {
17629671eb9Sjbm struct lms_softc *sc = v;
17729671eb9Sjbm
17829671eb9Sjbm /* Disable interrupts. */
17929671eb9Sjbm bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0x10);
18029671eb9Sjbm
18129671eb9Sjbm sc->sc_enabled = 0;
18229671eb9Sjbm }
18329671eb9Sjbm
18429671eb9Sjbm int
lms_ioctl(void * v,u_long cmd,caddr_t data,int flag,struct proc * p)18508f75f72Sjsg lms_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
18629671eb9Sjbm {
18729671eb9Sjbm #if 0
18829671eb9Sjbm struct lms_softc *sc = v;
18929671eb9Sjbm #endif
19029671eb9Sjbm
19129671eb9Sjbm switch (cmd) {
19229671eb9Sjbm case WSMOUSEIO_GTYPE:
19329671eb9Sjbm *(u_int *)data = WSMOUSE_TYPE_LMS;
19429671eb9Sjbm return (0);
19529671eb9Sjbm }
19629671eb9Sjbm return (-1);
19729671eb9Sjbm }
19829671eb9Sjbm
19929671eb9Sjbm int
lmsintr(void * arg)20008f75f72Sjsg lmsintr(void *arg)
20129671eb9Sjbm {
20229671eb9Sjbm struct lms_softc *sc = arg;
20329671eb9Sjbm bus_space_tag_t iot = sc->sc_iot;
20429671eb9Sjbm bus_space_handle_t ioh = sc->sc_ioh;
20529671eb9Sjbm u_char hi, lo;
20629671eb9Sjbm signed char dx, dy;
20729671eb9Sjbm u_int buttons;
20829671eb9Sjbm int changed;
20929671eb9Sjbm
21029671eb9Sjbm if (!sc->sc_enabled)
21129671eb9Sjbm /* Interrupts are not expected. */
21229671eb9Sjbm return 0;
21329671eb9Sjbm
21429671eb9Sjbm bus_space_write_1(iot, ioh, LMS_CNTRL, 0xab);
21529671eb9Sjbm hi = bus_space_read_1(iot, ioh, LMS_DATA);
21629671eb9Sjbm bus_space_write_1(iot, ioh, LMS_CNTRL, 0x90);
21729671eb9Sjbm lo = bus_space_read_1(iot, ioh, LMS_DATA);
21829671eb9Sjbm dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
21929671eb9Sjbm /* Bounding at -127 avoids a bug in XFree86. */
22029671eb9Sjbm dx = (dx == -128) ? -127 : dx;
22129671eb9Sjbm
22229671eb9Sjbm bus_space_write_1(iot, ioh, LMS_CNTRL, 0xf0);
22329671eb9Sjbm hi = bus_space_read_1(iot, ioh, LMS_DATA);
22429671eb9Sjbm bus_space_write_1(iot, ioh, LMS_CNTRL, 0xd0);
22529671eb9Sjbm lo = bus_space_read_1(iot, ioh, LMS_DATA);
22629671eb9Sjbm dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
22729671eb9Sjbm dy = (dy == -128) ? 127 : -dy;
22829671eb9Sjbm
22929671eb9Sjbm bus_space_write_1(iot, ioh, LMS_CNTRL, 0);
23029671eb9Sjbm
23129671eb9Sjbm buttons = ((hi & 0x80) ? 0 : 0x1) |
23229671eb9Sjbm ((hi & 0x40) ? 0 : 0x2) |
23329671eb9Sjbm ((hi & 0x20) ? 0 : 0x4);
23429671eb9Sjbm changed = (buttons ^ sc->oldbuttons);
23529671eb9Sjbm sc->oldbuttons = buttons;
23629671eb9Sjbm
23729671eb9Sjbm if (dx || dy || changed)
23894712de5Sbru WSMOUSE_INPUT(sc->sc_wsmousedev, buttons, dx, dy, 0, 0);
23929671eb9Sjbm
24029671eb9Sjbm return -1;
24129671eb9Sjbm }
24229671eb9Sjbm
24329671eb9Sjbm struct cfdriver lms_cd = {
24429671eb9Sjbm NULL, "lms", DV_DULL
24529671eb9Sjbm };
246