mms.c revision 1.7 1 1.1 andrew /*-
2 1.1 andrew * Copyright (c) 1992, 1993 Erik Forsberg.
3 1.1 andrew * All rights reserved.
4 1.1 andrew *
5 1.1 andrew * Redistribution and use in source and binary forms, with or without
6 1.1 andrew * modification, are permitted provided that the following conditions
7 1.1 andrew * are met:
8 1.1 andrew * 1. Redistributions of source code must retain the above copyright
9 1.1 andrew * notice, this list of conditions and the following disclaimer.
10 1.1 andrew *
11 1.1 andrew * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
12 1.1 andrew * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
13 1.1 andrew * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
14 1.1 andrew * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
15 1.1 andrew * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16 1.1 andrew * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
17 1.1 andrew * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
18 1.1 andrew * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19 1.1 andrew * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
20 1.1 andrew * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 1.1 andrew *
22 1.7 mycroft * $Id: mms.c,v 1.7 1993/12/20 09:06:28 mycroft Exp $
23 1.1 andrew */
24 1.1 andrew
25 1.2 andrew #include "mms.h"
26 1.2 andrew #if NMMS > 0
27 1.1 andrew
28 1.7 mycroft #include <sys/param.h>
29 1.7 mycroft #include <sys/kernel.h>
30 1.7 mycroft #include <sys/systm.h>
31 1.7 mycroft #include <sys/buf.h>
32 1.7 mycroft #include <sys/malloc.h>
33 1.7 mycroft #include <sys/ioctl.h>
34 1.7 mycroft #include <sys/tty.h>
35 1.7 mycroft #include <sys/file.h>
36 1.5 andrew #ifdef NetBSD
37 1.7 mycroft #include <sys/select.h>
38 1.1 andrew #endif
39 1.7 mycroft #include <sys/proc.h>
40 1.7 mycroft #include <sys/vnode.h>
41 1.7 mycroft
42 1.7 mycroft #include <machine/mouse.h>
43 1.7 mycroft #include <machine/pio.h>
44 1.1 andrew
45 1.7 mycroft #include <i386/isa/isa_device.h>
46 1.1 andrew
47 1.1 andrew #define ADDR 0 /* Offset for register select */
48 1.1 andrew #define DATA 1 /* Offset for InPort data */
49 1.1 andrew #define IDENT 2 /* Offset for identification register */
50 1.1 andrew
51 1.2 andrew #define MMSUNIT(dev) (minor(dev) >> 1)
52 1.1 andrew
53 1.1 andrew #ifndef min
54 1.1 andrew #define min(x,y) (x < y ? x : y)
55 1.1 andrew #endif min
56 1.1 andrew
57 1.2 andrew int mmsprobe (struct isa_device *);
58 1.2 andrew int mmsattach (struct isa_device *);
59 1.1 andrew
60 1.2 andrew static int mmsaddr[NMMS]; /* Base I/O port addresses per unit */
61 1.1 andrew
62 1.1 andrew #define MSBSZ 1024 /* Output queue size (pwr of 2 is best) */
63 1.1 andrew
64 1.1 andrew struct ringbuf {
65 1.1 andrew int count, first, last;
66 1.1 andrew char queue[MSBSZ];
67 1.1 andrew };
68 1.1 andrew
69 1.2 andrew static struct mms_softc { /* Driver status information */
70 1.1 andrew struct ringbuf inq; /* Input queue */
71 1.5 andrew #ifdef NetBSD
72 1.5 andrew struct selinfo rsel;
73 1.5 andrew #else
74 1.1 andrew pid_t rsel; /* Process selecting for Input */
75 1.1 andrew #endif
76 1.1 andrew unsigned char state; /* Mouse driver state */
77 1.1 andrew unsigned char status; /* Mouse button status */
78 1.1 andrew int x, y; /* accumulated motion in the X,Y axis */
79 1.2 andrew } mms_softc[NMMS];
80 1.1 andrew
81 1.1 andrew #define OPEN 1 /* Device is open */
82 1.1 andrew #define ASLP 2 /* Waiting for mouse data */
83 1.1 andrew
84 1.2 andrew struct isa_driver mmsdriver = { mmsprobe, mmsattach, "mms" };
85 1.1 andrew
86 1.2 andrew int mmsprobe(struct isa_device *dvp)
87 1.1 andrew {
88 1.1 andrew int ioport = dvp->id_iobase;
89 1.1 andrew
90 1.1 andrew /* Read identification register to see if present */
91 1.1 andrew
92 1.1 andrew if (inb(ioport+IDENT) != 0xDE)
93 1.1 andrew return(0);
94 1.1 andrew
95 1.1 andrew /* Seems it was there; reset */
96 1.1 andrew
97 1.1 andrew outb(ioport+ADDR, 0x87);
98 1.4 mycroft return(4);
99 1.1 andrew }
100 1.1 andrew
101 1.2 andrew int mmsattach(struct isa_device *dvp)
102 1.1 andrew {
103 1.1 andrew int unit = dvp->id_unit;
104 1.1 andrew int ioport = dvp->id_iobase;
105 1.2 andrew struct mms_softc *sc = &mms_softc[unit];
106 1.1 andrew
107 1.1 andrew /* Save I/O base address */
108 1.1 andrew
109 1.2 andrew mmsaddr[unit] = ioport;
110 1.1 andrew
111 1.1 andrew /* Setup initial state */
112 1.1 andrew
113 1.1 andrew sc->state = 0;
114 1.1 andrew
115 1.1 andrew /* Done */
116 1.1 andrew
117 1.1 andrew return(0);
118 1.1 andrew }
119 1.1 andrew
120 1.2 andrew int mmsopen(dev_t dev, int flag, int fmt, struct proc *p)
121 1.1 andrew {
122 1.2 andrew int unit = MMSUNIT(dev);
123 1.2 andrew struct mms_softc *sc;
124 1.1 andrew int ioport;
125 1.1 andrew
126 1.1 andrew /* Validate unit number */
127 1.1 andrew
128 1.2 andrew if (unit >= NMMS)
129 1.1 andrew return(ENXIO);
130 1.1 andrew
131 1.1 andrew /* Get device data */
132 1.1 andrew
133 1.2 andrew sc = &mms_softc[unit];
134 1.2 andrew ioport = mmsaddr[unit];
135 1.1 andrew
136 1.1 andrew /* If device does not exist */
137 1.1 andrew
138 1.1 andrew if (ioport == 0)
139 1.1 andrew return(ENXIO);
140 1.1 andrew
141 1.1 andrew /* Disallow multiple opens */
142 1.1 andrew
143 1.1 andrew if (sc->state & OPEN)
144 1.3 mycroft return(EBUSY);
145 1.1 andrew
146 1.1 andrew /* Initialize state */
147 1.1 andrew
148 1.1 andrew sc->state |= OPEN;
149 1.5 andrew #ifdef NetBSD
150 1.1 andrew sc->rsel.si_pid = 0;
151 1.1 andrew sc->rsel.si_coll = 0;
152 1.5 andrew #else
153 1.5 andrew sc->rsel = 0;
154 1.1 andrew #endif
155 1.1 andrew sc->status = 0;
156 1.1 andrew sc->x = 0;
157 1.1 andrew sc->y = 0;
158 1.1 andrew
159 1.1 andrew /* Allocate and initialize a ring buffer */
160 1.1 andrew
161 1.1 andrew sc->inq.count = sc->inq.first = sc->inq.last = 0;
162 1.1 andrew
163 1.1 andrew /* Setup Bus Mouse */
164 1.1 andrew
165 1.1 andrew outb(ioport+ADDR, 7);
166 1.1 andrew outb(ioport+DATA, 0x09);
167 1.1 andrew
168 1.1 andrew /* Successful open */
169 1.1 andrew
170 1.1 andrew return(0);
171 1.1 andrew }
172 1.1 andrew
173 1.2 andrew int mmsclose(dev_t dev, int flag, int fmt, struct proc *p)
174 1.1 andrew {
175 1.1 andrew int unit, ioport;
176 1.2 andrew struct mms_softc *sc;
177 1.1 andrew
178 1.1 andrew /* Get unit and associated info */
179 1.1 andrew
180 1.2 andrew unit = MMSUNIT(dev);
181 1.2 andrew sc = &mms_softc[unit];
182 1.2 andrew ioport = mmsaddr[unit];
183 1.1 andrew
184 1.1 andrew /* Reset Bus Mouse */
185 1.1 andrew
186 1.1 andrew outb(ioport+ADDR, 0x87);
187 1.1 andrew
188 1.1 andrew /* Complete the close */
189 1.1 andrew
190 1.1 andrew sc->state &= ~OPEN;
191 1.1 andrew
192 1.1 andrew /* close is almost always successful */
193 1.1 andrew
194 1.1 andrew return(0);
195 1.1 andrew }
196 1.1 andrew
197 1.2 andrew int mmsread(dev_t dev, struct uio *uio, int flag)
198 1.1 andrew {
199 1.1 andrew int s, error = 0;
200 1.1 andrew unsigned length;
201 1.2 andrew struct mms_softc *sc;
202 1.1 andrew unsigned char buffer[100];
203 1.1 andrew
204 1.1 andrew /* Get device information */
205 1.1 andrew
206 1.2 andrew sc = &mms_softc[MMSUNIT(dev)];
207 1.1 andrew
208 1.1 andrew /* Block until mouse activity occured */
209 1.1 andrew
210 1.1 andrew s = spltty();
211 1.1 andrew while (sc->inq.count == 0) {
212 1.1 andrew if (minor(dev) & 0x1) {
213 1.1 andrew splx(s);
214 1.1 andrew return(EWOULDBLOCK);
215 1.1 andrew }
216 1.1 andrew sc->state |= ASLP;
217 1.5 andrew error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0);
218 1.1 andrew if (error != 0) {
219 1.1 andrew splx(s);
220 1.1 andrew return(error);
221 1.1 andrew }
222 1.1 andrew }
223 1.1 andrew
224 1.1 andrew /* Transfer as many chunks as possible */
225 1.1 andrew
226 1.1 andrew while (sc->inq.count > 0 && uio->uio_resid > 0) {
227 1.1 andrew length = min(sc->inq.count, uio->uio_resid);
228 1.1 andrew if (length > sizeof(buffer))
229 1.1 andrew length = sizeof(buffer);
230 1.1 andrew
231 1.1 andrew /* Remove a small chunk from input queue */
232 1.1 andrew
233 1.1 andrew if (sc->inq.first + length >= MSBSZ) {
234 1.1 andrew bcopy(&sc->inq.queue[sc->inq.first],
235 1.1 andrew buffer, MSBSZ - sc->inq.first);
236 1.1 andrew bcopy(sc->inq.queue, &buffer[MSBSZ-sc->inq.first],
237 1.1 andrew length - (MSBSZ - sc->inq.first));
238 1.1 andrew }
239 1.1 andrew else
240 1.1 andrew bcopy(&sc->inq.queue[sc->inq.first], buffer, length);
241 1.1 andrew
242 1.1 andrew sc->inq.first = (sc->inq.first + length) % MSBSZ;
243 1.1 andrew sc->inq.count -= length;
244 1.1 andrew
245 1.1 andrew /* Copy data to user process */
246 1.1 andrew
247 1.1 andrew error = uiomove(buffer, length, uio);
248 1.1 andrew if (error)
249 1.1 andrew break;
250 1.1 andrew }
251 1.1 andrew
252 1.1 andrew sc->x = sc->y = 0;
253 1.1 andrew
254 1.1 andrew /* Allow interrupts again */
255 1.1 andrew
256 1.1 andrew splx(s);
257 1.1 andrew return(error);
258 1.1 andrew }
259 1.1 andrew
260 1.2 andrew int mmsioctl(dev_t dev, caddr_t addr, int cmd, int flag, struct proc *p)
261 1.1 andrew {
262 1.2 andrew struct mms_softc *sc;
263 1.1 andrew struct mouseinfo info;
264 1.1 andrew int s, error;
265 1.1 andrew
266 1.1 andrew /* Get device information */
267 1.1 andrew
268 1.2 andrew sc = &mms_softc[MMSUNIT(dev)];
269 1.1 andrew
270 1.1 andrew /* Perform IOCTL command */
271 1.1 andrew
272 1.1 andrew switch (cmd) {
273 1.1 andrew
274 1.1 andrew case MOUSEIOCREAD:
275 1.1 andrew
276 1.1 andrew /* Don't modify info while calculating */
277 1.1 andrew
278 1.1 andrew s = spltty();
279 1.1 andrew
280 1.1 andrew /* Build mouse status octet */
281 1.1 andrew
282 1.1 andrew info.status = sc->status;
283 1.1 andrew if (sc->x || sc->y)
284 1.1 andrew info.status |= MOVEMENT;
285 1.1 andrew
286 1.1 andrew /* Encode X and Y motion as good as we can */
287 1.1 andrew
288 1.1 andrew if (sc->x > 127)
289 1.1 andrew info.xmotion = 127;
290 1.1 andrew else if (sc->x < -128)
291 1.1 andrew info.xmotion = -128;
292 1.1 andrew else
293 1.1 andrew info.xmotion = sc->x;
294 1.1 andrew
295 1.1 andrew if (sc->y > 127)
296 1.1 andrew info.ymotion = 127;
297 1.1 andrew else if (sc->y < -128)
298 1.1 andrew info.ymotion = -128;
299 1.1 andrew else
300 1.1 andrew info.ymotion = sc->y;
301 1.1 andrew
302 1.1 andrew /* Reset historical information */
303 1.1 andrew
304 1.1 andrew sc->x = 0;
305 1.1 andrew sc->y = 0;
306 1.1 andrew sc->status &= ~BUTCHNGMASK;
307 1.1 andrew
308 1.1 andrew /* Allow interrupts and copy result buffer */
309 1.1 andrew
310 1.1 andrew splx(s);
311 1.1 andrew error = copyout(&info, addr, sizeof(struct mouseinfo));
312 1.1 andrew break;
313 1.1 andrew
314 1.1 andrew default:
315 1.1 andrew error = EINVAL;
316 1.1 andrew break;
317 1.1 andrew }
318 1.1 andrew
319 1.1 andrew /* Return error code */
320 1.1 andrew
321 1.1 andrew return(error);
322 1.1 andrew }
323 1.1 andrew
324 1.2 andrew void mmsintr(unit)
325 1.1 andrew int unit;
326 1.1 andrew {
327 1.2 andrew struct mms_softc *sc = &mms_softc[unit];
328 1.2 andrew int ioport = mmsaddr[unit];
329 1.1 andrew char dx, dy, status;
330 1.1 andrew
331 1.1 andrew /* Freeze InPort registers (disabling interrupts) */
332 1.1 andrew
333 1.1 andrew outb(ioport+ADDR, 7);
334 1.1 andrew outb(ioport+DATA, 0x29);
335 1.1 andrew
336 1.1 andrew /* Read mouse status */
337 1.1 andrew
338 1.1 andrew outb(ioport+ADDR, 0);
339 1.1 andrew status = inb(ioport+DATA);
340 1.1 andrew
341 1.1 andrew /* Check if any movement detected */
342 1.1 andrew
343 1.1 andrew if (status & 0x40) {
344 1.1 andrew outb(ioport+ADDR, 1);
345 1.1 andrew dx = inb(ioport+DATA);
346 1.1 andrew outb(ioport+ADDR, 2);
347 1.1 andrew dy = inb(ioport+DATA);
348 1.1 andrew dy = (dy == -128) ? 127 : -dy;
349 1.1 andrew }
350 1.1 andrew else
351 1.1 andrew dx = dy = 0;
352 1.1 andrew
353 1.1 andrew /* Unfreeze InPort Registers (re-enables interrupts) */
354 1.1 andrew
355 1.1 andrew outb(ioport+ADDR, 7);
356 1.1 andrew outb(ioport+DATA, 0x09);
357 1.1 andrew
358 1.1 andrew /* Update accumulated movements */
359 1.1 andrew
360 1.1 andrew sc->x += dx;
361 1.1 andrew sc->y += dy;
362 1.1 andrew
363 1.1 andrew /* Inclusive OR status changes, but always save only last state */
364 1.1 andrew
365 1.1 andrew sc->status |= status & BUTCHNGMASK;
366 1.1 andrew sc->status = (sc->status & ~BUTSTATMASK) | (status & BUTSTATMASK);
367 1.1 andrew
368 1.1 andrew /* If device in use and a change occurred... */
369 1.1 andrew
370 1.1 andrew if (sc->state & OPEN && status & 0x78 && sc->inq.count < (MSBSZ-5)) {
371 1.1 andrew status &= BUTSTATMASK;
372 1.1 andrew sc->inq.queue[sc->inq.last++] = 0x80 | (status ^ BUTSTATMASK);
373 1.1 andrew sc->inq.queue[sc->inq.last++ % MSBSZ] = dx;
374 1.1 andrew sc->inq.queue[sc->inq.last++ % MSBSZ] = dy;
375 1.1 andrew sc->inq.queue[sc->inq.last++ % MSBSZ] = 0;
376 1.1 andrew sc->inq.queue[sc->inq.last++ % MSBSZ] = 0;
377 1.1 andrew sc->inq.last = sc->inq.last % MSBSZ;
378 1.1 andrew sc->inq.count += 5;
379 1.1 andrew
380 1.1 andrew if (sc->state & ASLP) {
381 1.1 andrew sc->state &= ~ASLP;
382 1.5 andrew wakeup((caddr_t)sc);
383 1.1 andrew }
384 1.5 andrew #ifdef NetBSD
385 1.5 andrew selwakeup(&sc->rsel);
386 1.5 andrew #else
387 1.1 andrew if (sc->rsel) {
388 1.5 andrew selwakeup(sc->rsel, 0);
389 1.1 andrew sc->rsel = 0;
390 1.1 andrew }
391 1.1 andrew #endif
392 1.1 andrew }
393 1.1 andrew }
394 1.1 andrew
395 1.2 andrew int mmsselect(dev_t dev, int rw, struct proc *p)
396 1.1 andrew {
397 1.1 andrew int s, ret;
398 1.2 andrew struct mms_softc *sc = &mms_softc[MMSUNIT(dev)];
399 1.1 andrew
400 1.1 andrew /* Silly to select for output */
401 1.1 andrew
402 1.1 andrew if (rw == FWRITE)
403 1.1 andrew return(0);
404 1.1 andrew
405 1.1 andrew /* Return true if a mouse event available */
406 1.1 andrew
407 1.1 andrew s = spltty();
408 1.1 andrew if (sc->inq.count)
409 1.1 andrew ret = 1;
410 1.1 andrew else {
411 1.5 andrew #ifdef NetBSD
412 1.5 andrew selrecord(p, &sc->rsel);
413 1.5 andrew #else
414 1.1 andrew sc->rsel = p->p_pid;
415 1.1 andrew #endif
416 1.1 andrew ret = 0;
417 1.1 andrew }
418 1.1 andrew splx(s);
419 1.1 andrew
420 1.1 andrew return(ret);
421 1.1 andrew }
422 1.1 andrew #endif
423