1 /* $NetBSD: mms.c,v 1.55 2021/08/07 16:18:55 thorpej 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: mms.c,v 1.55 2021/08/07 16:18:55 thorpej 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/intr.h>
35 #include <sys/bus.h>
36
37 #include <dev/isa/isavar.h>
38
39 #include <dev/wscons/wsconsio.h>
40 #include <dev/wscons/wsmousevar.h>
41
42 #define MMS_ADDR 0 /* offset for register select */
43 #define MMS_DATA 1 /* offset for InPort data */
44 #define MMS_IDENT 2 /* offset for identification register */
45 #define MMS_NPORTS 4
46
47 struct mms_softc { /* driver status information */
48 void *sc_ih;
49
50 bus_space_tag_t sc_iot;
51 bus_space_handle_t sc_ioh;
52
53 int sc_enabled; /* device is open */
54
55 device_t sc_wsmousedev;
56 };
57
58 static int mmsprobe(device_t, cfdata_t, void *);
59 static void mmsattach(device_t, device_t, void *);
60 static int mmsintr(void *);
61
62 CFATTACH_DECL_NEW(mms, sizeof(struct mms_softc),
63 mmsprobe, mmsattach, NULL, NULL);
64
65 static int mms_enable(void *);
66 static int mms_ioctl(void *, u_long, void *, int, struct lwp *);
67 static void mms_disable(void *);
68
69 static const struct wsmouse_accessops mms_accessops = {
70 mms_enable,
71 mms_ioctl,
72 mms_disable,
73 };
74
75 static int
mmsprobe(device_t parent,cfdata_t match,void * aux)76 mmsprobe(device_t parent, cfdata_t match, void *aux)
77 {
78 struct isa_attach_args *ia = aux;
79 bus_space_tag_t iot = ia->ia_iot;
80 bus_space_handle_t ioh;
81 int rv;
82
83 if (ia->ia_nio < 1)
84 return (0);
85 if (ia->ia_nirq < 1)
86 return (0);
87
88 if (ISA_DIRECT_CONFIG(ia))
89 return (0);
90
91 /* Disallow wildcarded i/o address. */
92 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
93 return 0;
94
95 if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
96 return 0;
97
98 /* Map the i/o space. */
99 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MMS_NPORTS, 0, &ioh))
100 return 0;
101
102 rv = 0;
103
104 /* Read identification register to see if present */
105 if (bus_space_read_1(iot, ioh, MMS_IDENT) != 0xde)
106 goto out;
107
108 /* Seems it was there; reset. */
109 bus_space_write_1(iot, ioh, MMS_ADDR, 0x87);
110
111 rv = 1;
112
113 ia->ia_nio = 1;
114 ia->ia_io[0].ir_size = MMS_NPORTS;
115
116 ia->ia_nirq = 1;
117
118 ia->ia_niomem = 0;
119 ia->ia_ndrq = 0;
120
121 out:
122 bus_space_unmap(iot, ioh, MMS_NPORTS);
123 return rv;
124 }
125
126 static void
mmsattach(device_t parent,device_t self,void * aux)127 mmsattach(device_t parent, device_t self, void *aux)
128 {
129 struct mms_softc *sc = device_private(self);
130 struct isa_attach_args *ia = aux;
131 bus_space_tag_t iot = ia->ia_iot;
132 bus_space_handle_t ioh;
133 struct wsmousedev_attach_args a;
134
135 aprint_naive(": Mouse\n");
136 aprint_normal(": Microsoft Mouse\n");
137
138 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MMS_NPORTS, 0, &ioh)) {
139 aprint_error_dev(self, "can't map i/o space\n");
140 return;
141 }
142
143 /* Other initialization was done by mmsprobe. */
144 sc->sc_iot = iot;
145 sc->sc_ioh = ioh;
146 sc->sc_enabled = 0;
147
148 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
149 IST_PULSE, IPL_TTY, mmsintr, sc);
150
151 a.accessops = &mms_accessops;
152 a.accesscookie = sc;
153
154 /*
155 * Attach the wsmouse, saving a handle to it.
156 * Note that we don't need to check this pointer against NULL
157 * here or in psmintr, because if this fails lms_enable() will
158 * never be called, so lmsintr() will never be called.
159 */
160 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint, CFARGS_NONE);
161 }
162
163 static int
mms_enable(void * v)164 mms_enable(void *v)
165 {
166 struct mms_softc *sc = v;
167
168 if (sc->sc_enabled)
169 return EBUSY;
170
171 sc->sc_enabled = 1;
172
173 /* Enable interrupts. */
174 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x07);
175 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_DATA, 0x09);
176
177 return 0;
178 }
179
180 static void
mms_disable(void * v)181 mms_disable(void *v)
182 {
183 struct mms_softc *sc = v;
184
185 /* Disable interrupts. */
186 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x87);
187
188 sc->sc_enabled = 0;
189 }
190
191 static int
mms_ioctl(void * v,u_long cmd,void * data,int flag,struct lwp * l)192 mms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
193 {
194 #if 0
195 struct mms_softc *sc = v;
196 #endif
197
198 switch (cmd) {
199 case WSMOUSEIO_GTYPE:
200 *(u_int *)data = WSMOUSE_TYPE_MMS;
201 return (0);
202 }
203 return (EPASSTHROUGH);
204 }
205
206 static int
mmsintr(void * arg)207 mmsintr(void *arg)
208 {
209 struct mms_softc *sc = arg;
210 bus_space_tag_t iot = sc->sc_iot;
211 bus_space_handle_t ioh = sc->sc_ioh;
212 u_char status;
213 signed char dx, dy;
214 u_int buttons;
215 int changed;
216
217 if (!sc->sc_enabled)
218 /* Interrupts are not expected. */
219 return 0;
220
221 /* Freeze InPort registers (disabling interrupts). */
222 bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
223 bus_space_write_1(iot, ioh, MMS_DATA, 0x29);
224
225 bus_space_write_1(iot, ioh, MMS_ADDR, 0x00);
226 status = bus_space_read_1(iot, ioh, MMS_DATA);
227
228 if (status & 0x40) {
229 bus_space_write_1(iot, ioh, MMS_ADDR, 1);
230 dx = bus_space_read_1(iot, ioh, MMS_DATA);
231 /* Bounding at -127 avoids a bug in XFree86. */
232 dx = (dx == -128) ? -127 : dx;
233
234 bus_space_write_1(iot, ioh, MMS_ADDR, 2);
235 dy = bus_space_read_1(iot, ioh, MMS_DATA);
236 dy = (dy == -128) ? 127 : -dy;
237 } else
238 dx = dy = 0;
239
240 /* Unfreeze InPort registers (reenabling interrupts). */
241 bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
242 bus_space_write_1(iot, ioh, MMS_DATA, 0x09);
243
244 buttons = ((status & 0x04) ? 0x1 : 0) |
245 ((status & 0x02) ? 0x2 : 0) |
246 ((status & 0x01) ? 0x4 : 0);
247 changed = status & 0x38;
248
249 if (dx || dy || changed)
250 wsmouse_input(sc->sc_wsmousedev,
251 buttons,
252 dx, dy, 0, 0,
253 WSMOUSE_INPUT_DELTA);
254
255 return -1;
256 }
257