mms.c revision 1.33 1 /* $NetBSD: mms.c,v 1.33 1998/08/25 04:59:28 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/param.h>
27 #include <sys/kernel.h>
28 #include <sys/systm.h>
29 #include <sys/buf.h>
30 #include <sys/malloc.h>
31 #include <sys/ioctl.h>
32 #include <sys/tty.h>
33 #include <sys/file.h>
34 #include <sys/select.h>
35 #include <sys/proc.h>
36 #include <sys/vnode.h>
37 #include <sys/device.h>
38 #include <sys/poll.h>
39
40 #include <machine/cpu.h>
41 #include <machine/intr.h>
42 #include <machine/bus.h>
43 #include <machine/mouse.h>
44 #include <machine/conf.h>
45
46 #include <dev/isa/isavar.h>
47
48 #define MMS_ADDR 0 /* offset for register select */
49 #define MMS_DATA 1 /* offset for InPort data */
50 #define MMS_IDENT 2 /* offset for identification register */
51 #define MMS_NPORTS 4
52
53 #define MMS_CHUNK 128 /* chunk size for read */
54 #define MMS_BSIZE 1020 /* buffer size */
55
56 struct mms_softc { /* driver status information */
57 struct device sc_dev;
58 void *sc_ih;
59
60 bus_space_tag_t sc_iot;
61 bus_space_handle_t sc_ioh;
62
63 struct clist sc_q;
64 struct selinfo sc_rsel;
65 u_char sc_state; /* mouse driver state */
66 #define MMS_OPEN 0x01 /* device is open */
67 #define MMS_ASLP 0x02 /* waiting for mouse data */
68 u_char sc_status; /* mouse button status */
69 int sc_x, sc_y; /* accumulated motion in the X,Y axis */
70 };
71
72 int mmsprobe __P((struct device *, struct cfdata *, void *));
73 void mmsattach __P((struct device *, struct device *, void *));
74 int mmsintr __P((void *));
75
76 struct cfattach mms_ca = {
77 sizeof(struct mms_softc), mmsprobe, mmsattach
78 };
79
80 extern struct cfdriver mms_cd;
81
82 #define MMSUNIT(dev) (minor(dev))
83
84 int
85 mmsprobe(parent, match, aux)
86 struct device *parent;
87 struct cfdata *match;
88 void *aux;
89 {
90 struct isa_attach_args *ia = aux;
91 bus_space_tag_t iot = ia->ia_iot;
92 bus_space_handle_t ioh;
93 int rv;
94
95 /* Disallow wildcarded i/o address. */
96 if (ia->ia_iobase == ISACF_PORT_DEFAULT)
97 return 0;
98
99 /* Map the i/o space. */
100 if (bus_space_map(iot, ia->ia_iobase, MMS_NPORTS, 0, &ioh))
101 return 0;
102
103 rv = 0;
104
105 /* Read identification register to see if present */
106 if (bus_space_read_1(iot, ioh, MMS_IDENT) != 0xde)
107 goto out;
108
109 /* Seems it was there; reset. */
110 bus_space_write_1(iot, ioh, MMS_ADDR, 0x87);
111
112 rv = 1;
113 ia->ia_iosize = MMS_NPORTS;
114 ia->ia_msize = 0;
115
116 out:
117 bus_space_unmap(iot, ioh, MMS_NPORTS);
118 return rv;
119 }
120
121 void
122 mmsattach(parent, self, aux)
123 struct device *parent, *self;
124 void *aux;
125 {
126 struct mms_softc *sc = (void *)self;
127 struct isa_attach_args *ia = aux;
128 bus_space_tag_t iot = ia->ia_iot;
129 bus_space_handle_t ioh;
130
131 printf("\n");
132
133 if (bus_space_map(iot, ia->ia_iobase, MMS_NPORTS, 0, &ioh)) {
134 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
135 return;
136 }
137
138 /* Other initialization was done by mmsprobe. */
139 sc->sc_iot = iot;
140 sc->sc_ioh = ioh;
141 sc->sc_state = 0;
142
143 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
144 IPL_TTY, mmsintr, sc);
145 }
146
147 int
148 mmsopen(dev, flag, mode, p)
149 dev_t dev;
150 int flag;
151 int mode;
152 struct proc *p;
153 {
154 int unit = MMSUNIT(dev);
155 struct mms_softc *sc;
156
157 if (unit >= mms_cd.cd_ndevs)
158 return ENXIO;
159 sc = mms_cd.cd_devs[unit];
160 if (!sc)
161 return ENXIO;
162
163 if (sc->sc_state & MMS_OPEN)
164 return EBUSY;
165
166 if (clalloc(&sc->sc_q, MMS_BSIZE, 0) == -1)
167 return ENOMEM;
168
169 sc->sc_state |= MMS_OPEN;
170 sc->sc_status = 0;
171 sc->sc_x = sc->sc_y = 0;
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 int
181 mmsclose(dev, flag, mode, p)
182 dev_t dev;
183 int flag;
184 int mode;
185 struct proc *p;
186 {
187 struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
188
189 /* Disable interrupts. */
190 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x87);
191
192 sc->sc_state &= ~MMS_OPEN;
193
194 clfree(&sc->sc_q);
195
196 return 0;
197 }
198
199 int
200 mmsread(dev, uio, flag)
201 dev_t dev;
202 struct uio *uio;
203 int flag;
204 {
205 struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
206 int s;
207 int error = 0;
208 size_t length;
209 u_char buffer[MMS_CHUNK];
210
211 /* Block until mouse activity occured. */
212
213 s = spltty();
214 while (sc->sc_q.c_cc == 0) {
215 if (flag & IO_NDELAY) {
216 splx(s);
217 return EWOULDBLOCK;
218 }
219 sc->sc_state |= MMS_ASLP;
220 error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0);
221 if (error) {
222 sc->sc_state &= ~MMS_ASLP;
223 splx(s);
224 return error;
225 }
226 }
227 splx(s);
228
229 /* Transfer as many chunks as possible. */
230
231 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
232 length = min(sc->sc_q.c_cc, uio->uio_resid);
233 if (length > sizeof(buffer))
234 length = sizeof(buffer);
235
236 /* Remove a small chunk from the input queue. */
237 (void) q_to_b(&sc->sc_q, buffer, length);
238
239 /* Copy the data to the user process. */
240 if ((error = uiomove(buffer, length, uio)) != 0)
241 break;
242 }
243
244 return error;
245 }
246
247 int
248 mmsioctl(dev, cmd, addr, flag, p)
249 dev_t dev;
250 u_long cmd;
251 caddr_t addr;
252 int flag;
253 struct proc *p;
254 {
255 struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
256 struct mouseinfo info;
257 int s;
258 int error;
259
260 switch (cmd) {
261 case MOUSEIOCREAD:
262 s = spltty();
263
264 info.status = sc->sc_status;
265 if (sc->sc_x || sc->sc_y)
266 info.status |= MOVEMENT;
267
268 if (sc->sc_x > 127)
269 info.xmotion = 127;
270 else if (sc->sc_x < -127)
271 /* Bounding at -127 avoids a bug in XFree86. */
272 info.xmotion = -127;
273 else
274 info.xmotion = sc->sc_x;
275
276 if (sc->sc_y > 127)
277 info.ymotion = 127;
278 else if (sc->sc_y < -127)
279 info.ymotion = -127;
280 else
281 info.ymotion = sc->sc_y;
282
283 /* Reset historical information. */
284 sc->sc_x = sc->sc_y = 0;
285 sc->sc_status &= ~BUTCHNGMASK;
286 ndflush(&sc->sc_q, sc->sc_q.c_cc);
287
288 splx(s);
289 error = copyout(&info, addr, sizeof(struct mouseinfo));
290 break;
291
292 default:
293 error = EINVAL;
294 break;
295 }
296
297 return error;
298 }
299
300 int
301 mmsintr(arg)
302 void *arg;
303 {
304 struct mms_softc *sc = arg;
305 bus_space_tag_t iot = sc->sc_iot;
306 bus_space_handle_t ioh = sc->sc_ioh;
307 u_char buttons, changed, status;
308 char dx, dy;
309 u_char buffer[5];
310
311 if ((sc->sc_state & MMS_OPEN) == 0)
312 /* Interrupts are not expected. */
313 return 0;
314
315 /* Freeze InPort registers (disabling interrupts). */
316 bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
317 bus_space_write_1(iot, ioh, MMS_DATA, 0x29);
318
319 bus_space_write_1(iot, ioh, MMS_ADDR, 0x00);
320 status = bus_space_read_1(iot, ioh, MMS_DATA);
321
322 if (status & 0x40) {
323 bus_space_write_1(iot, ioh, MMS_ADDR, 1);
324 dx = bus_space_read_1(iot, ioh, MMS_DATA);
325 dx = (dx == -128) ? -127 : dx;
326 bus_space_write_1(iot, ioh, MMS_ADDR, 2);
327 dy = bus_space_read_1(iot, ioh, MMS_DATA);
328 dy = (dy == -128) ? 127 : -dy;
329 } else
330 dx = dy = 0;
331
332 /* Unfreeze InPort registers (reenabling interrupts). */
333 bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
334 bus_space_write_1(iot, ioh, MMS_DATA, 0x09);
335
336 buttons = status & BUTSTATMASK;
337 changed = status & BUTCHNGMASK;
338 sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
339
340 if (dx || dy || changed) {
341 /* Update accumulated movements. */
342 sc->sc_x += dx;
343 sc->sc_y += dy;
344
345 /* Add this event to the queue. */
346 buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
347 buffer[1] = dx;
348 buffer[2] = dy;
349 buffer[3] = buffer[4] = 0;
350 (void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
351
352 if (sc->sc_state & MMS_ASLP) {
353 sc->sc_state &= ~MMS_ASLP;
354 wakeup((caddr_t)sc);
355 }
356 selwakeup(&sc->sc_rsel);
357 }
358
359 return -1;
360 }
361
362 int
363 mmspoll(dev, events, p)
364 dev_t dev;
365 int events;
366 struct proc *p;
367 {
368 struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
369 int revents = 0;
370 int s = spltty();
371
372 if (events & (POLLIN | POLLRDNORM)) {
373 if (sc->sc_q.c_cc > 0)
374 revents |= events & (POLLIN | POLLRDNORM);
375 else
376 selrecord(p, &sc->sc_rsel);
377 }
378
379 splx(s);
380 return (revents);
381 }
382