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