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