mms.c revision 1.40 1 /* $NetBSD: mms.c,v 1.40 2002/10/01 12:57:12 fvdl 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.40 2002/10/01 12:57:12 fvdl 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 <machine/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 struct device sc_dev;
49 void *sc_ih;
50
51 bus_space_tag_t sc_iot;
52 bus_space_handle_t sc_ioh;
53
54 int sc_enabled; /* device is open */
55
56 struct device *sc_wsmousedev;
57 };
58
59 int mmsprobe __P((struct device *, struct cfdata *, void *));
60 void mmsattach __P((struct device *, struct device *, void *));
61 int mmsintr __P((void *));
62
63 CFATTACH_DECL(mms, sizeof(struct mms_softc), mmsprobe, mmsattach, NULL, NULL)
64
65 int mms_enable __P((void *));
66 int mms_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
67 void mms_disable __P((void *));
68
69 const struct wsmouse_accessops mms_accessops = {
70 mms_enable,
71 mms_ioctl,
72 mms_disable,
73 };
74
75 int
76 mmsprobe(parent, match, aux)
77 struct device *parent;
78 struct cfdata *match;
79 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 address. */
95 if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
96 return 0;
97
98 if (ia->ia_irq[0].ir_irq == ISACF_IRQ_DEFAULT)
99 return 0;
100
101 /* Map the i/o space. */
102 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MMS_NPORTS, 0, &ioh))
103 return 0;
104
105 rv = 0;
106
107 /* Read identification register to see if present */
108 if (bus_space_read_1(iot, ioh, MMS_IDENT) != 0xde)
109 goto out;
110
111 /* Seems it was there; reset. */
112 bus_space_write_1(iot, ioh, MMS_ADDR, 0x87);
113
114 rv = 1;
115
116 ia->ia_nio = 1;
117 ia->ia_io[0].ir_size = MMS_NPORTS;
118
119 ia->ia_nirq = 1;
120
121 ia->ia_niomem = 0;
122 ia->ia_ndrq = 0;
123
124 out:
125 bus_space_unmap(iot, ioh, MMS_NPORTS);
126 return rv;
127 }
128
129 void
130 mmsattach(parent, self, aux)
131 struct device *parent, *self;
132 void *aux;
133 {
134 struct mms_softc *sc = (void *)self;
135 struct isa_attach_args *ia = aux;
136 bus_space_tag_t iot = ia->ia_iot;
137 bus_space_handle_t ioh;
138 struct wsmousedev_attach_args a;
139
140 printf("\n");
141
142 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MMS_NPORTS, 0, &ioh)) {
143 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
144 return;
145 }
146
147 /* Other initialization was done by mmsprobe. */
148 sc->sc_iot = iot;
149 sc->sc_ioh = ioh;
150 sc->sc_enabled = 0;
151
152 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
153 IST_PULSE, IPL_TTY, mmsintr, sc);
154
155 a.accessops = &mms_accessops;
156 a.accesscookie = sc;
157
158 /*
159 * Attach the wsmouse, saving a handle to it.
160 * Note that we don't need to check this pointer against NULL
161 * here or in psmintr, because if this fails lms_enable() will
162 * never be called, so lmsintr() will never be called.
163 */
164 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
165 }
166
167 int
168 mms_enable(v)
169 void *v;
170 {
171 struct mms_softc *sc = v;
172
173 if (sc->sc_enabled)
174 return EBUSY;
175
176 sc->sc_enabled = 1;
177
178 /* Enable interrupts. */
179 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x07);
180 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_DATA, 0x09);
181
182 return 0;
183 }
184
185 void
186 mms_disable(v)
187 void *v;
188 {
189 struct mms_softc *sc = v;
190
191 /* Disable interrupts. */
192 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x87);
193
194 sc->sc_enabled = 0;
195 }
196
197 int
198 mms_ioctl(v, cmd, data, flag, p)
199 void *v;
200 u_long cmd;
201 caddr_t data;
202 int flag;
203 struct proc *p;
204 {
205 #if 0
206 struct mms_softc *sc = v;
207 #endif
208
209 switch (cmd) {
210 case WSMOUSEIO_GTYPE:
211 *(u_int *)data = WSMOUSE_TYPE_MMS;
212 return (0);
213 }
214 return (EPASSTHROUGH);
215 }
216
217 int
218 mmsintr(arg)
219 void *arg;
220 {
221 struct mms_softc *sc = arg;
222 bus_space_tag_t iot = sc->sc_iot;
223 bus_space_handle_t ioh = sc->sc_ioh;
224 u_char status;
225 signed char dx, dy;
226 u_int buttons;
227 int changed;
228
229 if (!sc->sc_enabled)
230 /* Interrupts are not expected. */
231 return 0;
232
233 /* Freeze InPort registers (disabling interrupts). */
234 bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
235 bus_space_write_1(iot, ioh, MMS_DATA, 0x29);
236
237 bus_space_write_1(iot, ioh, MMS_ADDR, 0x00);
238 status = bus_space_read_1(iot, ioh, MMS_DATA);
239
240 if (status & 0x40) {
241 bus_space_write_1(iot, ioh, MMS_ADDR, 1);
242 dx = bus_space_read_1(iot, ioh, MMS_DATA);
243 /* Bounding at -127 avoids a bug in XFree86. */
244 dx = (dx == -128) ? -127 : dx;
245
246 bus_space_write_1(iot, ioh, MMS_ADDR, 2);
247 dy = bus_space_read_1(iot, ioh, MMS_DATA);
248 dy = (dy == -128) ? 127 : -dy;
249 } else
250 dx = dy = 0;
251
252 /* Unfreeze InPort registers (reenabling interrupts). */
253 bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
254 bus_space_write_1(iot, ioh, MMS_DATA, 0x09);
255
256 buttons = ((status & 0x04) ? 0x1 : 0) |
257 ((status & 0x02) ? 0x2 : 0) |
258 ((status & 0x01) ? 0x4 : 0);
259 changed = status & 0x38;
260
261 if (dx || dy || changed)
262 wsmouse_input(sc->sc_wsmousedev,
263 buttons, dx, dy, 0, WSMOUSE_INPUT_DELTA);
264
265 return -1;
266 }
267