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