lms.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: lms.c,v 1.7 1993/12/20 09:06:17 mycroft Exp $
23 1.1 andrew */
24 1.1 andrew
25 1.1 andrew #include "lms.h"
26 1.1 andrew #if NLMS > 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 DATA 0 /* Offset for data port, read-only */
48 1.1 andrew #define SIGN 1 /* Offset for signature port, read-write */
49 1.1 andrew #define INTR 2 /* Offset for interrupt port, read-only */
50 1.1 andrew #define CNTRL 2 /* Offset for control port, write-only */
51 1.1 andrew #define CONFIG 3 /* for configuration port, read-write */
52 1.1 andrew
53 1.1 andrew #define LMSUNIT(dev) (minor(dev) >> 1)
54 1.1 andrew
55 1.1 andrew #ifndef min
56 1.1 andrew #define min(x,y) (x < y ? x : y)
57 1.1 andrew #endif min
58 1.1 andrew
59 1.1 andrew int lmsprobe (struct isa_device *);
60 1.1 andrew int lmsattach (struct isa_device *);
61 1.1 andrew
62 1.1 andrew static int lmsaddr[NLMS]; /* Base I/O port addresses per unit */
63 1.1 andrew
64 1.1 andrew #define MSBSZ 1024 /* Output queue size (pwr of 2 is best) */
65 1.1 andrew
66 1.1 andrew struct ringbuf {
67 1.1 andrew int count, first, last;
68 1.1 andrew char queue[MSBSZ];
69 1.1 andrew };
70 1.1 andrew
71 1.1 andrew static struct lms_softc { /* Driver status information */
72 1.1 andrew struct ringbuf inq; /* Input queue */
73 1.5 andrew #ifdef NetBSD
74 1.5 andrew struct selinfo rsel;
75 1.5 andrew #else
76 1.1 andrew pid_t rsel; /* Process selecting for Input */
77 1.1 andrew #endif
78 1.1 andrew unsigned char state; /* Mouse driver state */
79 1.1 andrew unsigned char status; /* Mouse button status */
80 1.1 andrew unsigned char button; /* Previous mouse button status bits */
81 1.1 andrew int x, y; /* accumulated motion in the X,Y axis */
82 1.1 andrew } lms_softc[NLMS];
83 1.1 andrew
84 1.1 andrew #define OPEN 1 /* Device is open */
85 1.1 andrew #define ASLP 2 /* Waiting for mouse data */
86 1.1 andrew
87 1.1 andrew struct isa_driver lmsdriver = { lmsprobe, lmsattach, "lms" };
88 1.1 andrew
89 1.1 andrew int lmsprobe(struct isa_device *dvp)
90 1.1 andrew {
91 1.1 andrew int ioport = dvp->id_iobase;
92 1.1 andrew int val;
93 1.1 andrew
94 1.1 andrew /* Configure and check for port present */
95 1.1 andrew
96 1.1 andrew outb(ioport+CONFIG, 0x91);
97 1.1 andrew DELAY(10);
98 1.1 andrew outb(ioport+SIGN, 0x0C);
99 1.1 andrew DELAY(10);
100 1.1 andrew val = inb(ioport+SIGN);
101 1.1 andrew DELAY(10);
102 1.1 andrew outb(ioport+SIGN, 0x50);
103 1.1 andrew
104 1.1 andrew /* Check if something is out there */
105 1.1 andrew
106 1.1 andrew if (val == 0x0C && inb(ioport+SIGN) == 0x50)
107 1.4 mycroft return(4);
108 1.1 andrew
109 1.1 andrew /* Not present */
110 1.1 andrew
111 1.1 andrew return(0);
112 1.1 andrew }
113 1.1 andrew
114 1.1 andrew int lmsattach(struct isa_device *dvp)
115 1.1 andrew {
116 1.1 andrew int unit = dvp->id_unit;
117 1.1 andrew int ioport = dvp->id_iobase;
118 1.1 andrew struct lms_softc *sc = &lms_softc[unit];
119 1.1 andrew
120 1.1 andrew /* Save I/O base address */
121 1.1 andrew
122 1.1 andrew lmsaddr[unit] = ioport;
123 1.1 andrew
124 1.1 andrew /* Disable mouse interrupts */
125 1.1 andrew
126 1.1 andrew outb(ioport+CNTRL, 0x10);
127 1.1 andrew
128 1.1 andrew /* Setup initial state */
129 1.1 andrew
130 1.1 andrew sc->state = 0;
131 1.1 andrew
132 1.1 andrew /* Done */
133 1.1 andrew
134 1.1 andrew return(0);
135 1.1 andrew }
136 1.1 andrew
137 1.1 andrew int lmsopen(dev_t dev, int flag, int fmt, struct proc *p)
138 1.1 andrew {
139 1.1 andrew int unit = LMSUNIT(dev);
140 1.1 andrew struct lms_softc *sc;
141 1.1 andrew int ioport;
142 1.1 andrew
143 1.1 andrew /* Validate unit number */
144 1.1 andrew
145 1.1 andrew if (unit >= NLMS)
146 1.1 andrew return(ENXIO);
147 1.1 andrew
148 1.1 andrew /* Get device data */
149 1.1 andrew
150 1.1 andrew sc = &lms_softc[unit];
151 1.1 andrew ioport = lmsaddr[unit];
152 1.1 andrew
153 1.1 andrew /* If device does not exist */
154 1.1 andrew
155 1.1 andrew if (ioport == 0)
156 1.1 andrew return(ENXIO);
157 1.1 andrew
158 1.1 andrew /* Disallow multiple opens */
159 1.1 andrew
160 1.1 andrew if (sc->state & OPEN)
161 1.2 mycroft return(EBUSY);
162 1.1 andrew
163 1.1 andrew /* Initialize state */
164 1.1 andrew
165 1.1 andrew sc->state |= OPEN;
166 1.5 andrew #ifdef NetBSD
167 1.1 andrew sc->rsel.si_pid = 0;
168 1.1 andrew sc->rsel.si_coll = 0;
169 1.5 andrew #else
170 1.5 andrew sc->rsel = 0;
171 1.1 andrew #endif
172 1.1 andrew sc->status = 0;
173 1.1 andrew sc->button = 0;
174 1.1 andrew sc->x = 0;
175 1.1 andrew sc->y = 0;
176 1.1 andrew
177 1.1 andrew /* Allocate and initialize a ring buffer */
178 1.1 andrew
179 1.1 andrew sc->inq.count = sc->inq.first = sc->inq.last = 0;
180 1.1 andrew
181 1.1 andrew /* Enable Bus Mouse interrupts */
182 1.1 andrew
183 1.1 andrew outb(ioport+CNTRL, 0);
184 1.1 andrew
185 1.1 andrew /* Successful open */
186 1.1 andrew
187 1.1 andrew return(0);
188 1.1 andrew }
189 1.1 andrew
190 1.1 andrew int lmsclose(dev_t dev, int flag, int fmt, struct proc *p)
191 1.1 andrew {
192 1.1 andrew int unit, ioport;
193 1.1 andrew struct lms_softc *sc;
194 1.1 andrew
195 1.1 andrew /* Get unit and associated info */
196 1.1 andrew
197 1.1 andrew unit = LMSUNIT(dev);
198 1.1 andrew sc = &lms_softc[unit];
199 1.1 andrew ioport = lmsaddr[unit];
200 1.1 andrew
201 1.1 andrew /* Disable further mouse interrupts */
202 1.1 andrew
203 1.1 andrew outb(ioport+CNTRL, 0x10);
204 1.1 andrew
205 1.1 andrew /* Complete the close */
206 1.1 andrew
207 1.1 andrew sc->state &= ~OPEN;
208 1.1 andrew
209 1.1 andrew /* close is almost always successful */
210 1.1 andrew
211 1.1 andrew return(0);
212 1.1 andrew }
213 1.1 andrew
214 1.1 andrew int lmsread(dev_t dev, struct uio *uio, int flag)
215 1.1 andrew {
216 1.1 andrew int s;
217 1.1 andrew int error = 0; /* keep compiler quiet, even though initialisation
218 1.1 andrew is unnecessary */
219 1.1 andrew unsigned length;
220 1.1 andrew struct lms_softc *sc;
221 1.1 andrew unsigned char buffer[100];
222 1.1 andrew
223 1.1 andrew /* Get device information */
224 1.1 andrew
225 1.1 andrew sc = &lms_softc[LMSUNIT(dev)];
226 1.1 andrew
227 1.1 andrew /* Block until mouse activity occured */
228 1.1 andrew
229 1.1 andrew s = spltty();
230 1.1 andrew while (sc->inq.count == 0) {
231 1.1 andrew if (minor(dev) & 0x1) {
232 1.1 andrew splx(s);
233 1.1 andrew return(EWOULDBLOCK);
234 1.1 andrew }
235 1.1 andrew sc->state |= ASLP;
236 1.5 andrew error = tsleep((caddr_t)sc, PZERO | PCATCH, "lmsrea", 0);
237 1.1 andrew if (error != 0) {
238 1.1 andrew splx(s);
239 1.1 andrew return(error);
240 1.1 andrew }
241 1.1 andrew }
242 1.1 andrew
243 1.1 andrew /* Transfer as many chunks as possible */
244 1.1 andrew
245 1.1 andrew while (sc->inq.count > 0 && uio->uio_resid > 0) {
246 1.1 andrew length = min(sc->inq.count, uio->uio_resid);
247 1.1 andrew if (length > sizeof(buffer))
248 1.1 andrew length = sizeof(buffer);
249 1.1 andrew
250 1.1 andrew /* Remove a small chunk from input queue */
251 1.1 andrew
252 1.1 andrew if (sc->inq.first + length >= MSBSZ) {
253 1.1 andrew bcopy(&sc->inq.queue[sc->inq.first],
254 1.1 andrew buffer, MSBSZ - sc->inq.first);
255 1.1 andrew bcopy(sc->inq.queue, &buffer[MSBSZ-sc->inq.first],
256 1.1 andrew length - (MSBSZ - sc->inq.first));
257 1.1 andrew }
258 1.1 andrew else
259 1.1 andrew bcopy(&sc->inq.queue[sc->inq.first], buffer, length);
260 1.1 andrew
261 1.1 andrew sc->inq.first = (sc->inq.first + length) % MSBSZ;
262 1.1 andrew sc->inq.count -= length;
263 1.1 andrew
264 1.1 andrew /* Copy data to user process */
265 1.1 andrew
266 1.1 andrew error = uiomove(buffer, length, uio);
267 1.1 andrew if (error)
268 1.1 andrew break;
269 1.1 andrew }
270 1.1 andrew
271 1.1 andrew sc->x = sc->y = 0;
272 1.1 andrew
273 1.1 andrew /* Allow interrupts again */
274 1.1 andrew
275 1.1 andrew splx(s);
276 1.1 andrew return(error);
277 1.1 andrew }
278 1.1 andrew
279 1.1 andrew int lmsioctl(dev_t dev, caddr_t addr, int cmd, int flag, struct proc *p)
280 1.1 andrew {
281 1.1 andrew struct lms_softc *sc;
282 1.1 andrew struct mouseinfo info;
283 1.1 andrew int s, error;
284 1.1 andrew
285 1.1 andrew /* Get device information */
286 1.1 andrew
287 1.1 andrew sc = &lms_softc[LMSUNIT(dev)];
288 1.1 andrew
289 1.1 andrew /* Perform IOCTL command */
290 1.1 andrew
291 1.1 andrew switch (cmd) {
292 1.1 andrew
293 1.1 andrew case MOUSEIOCREAD:
294 1.1 andrew
295 1.1 andrew /* Don't modify info while calculating */
296 1.1 andrew
297 1.1 andrew s = spltty();
298 1.1 andrew
299 1.1 andrew /* Build mouse status octet */
300 1.1 andrew
301 1.1 andrew info.status = sc->status;
302 1.1 andrew if (sc->x || sc->y)
303 1.1 andrew info.status |= MOVEMENT;
304 1.1 andrew
305 1.1 andrew /* Encode X and Y motion as good as we can */
306 1.1 andrew
307 1.1 andrew if (sc->x > 127)
308 1.1 andrew info.xmotion = 127;
309 1.3 mycroft else if (sc->x < -127)
310 1.3 mycroft info.xmotion = -127;
311 1.1 andrew else
312 1.1 andrew info.xmotion = sc->x;
313 1.1 andrew
314 1.1 andrew if (sc->y > 127)
315 1.1 andrew info.ymotion = 127;
316 1.3 mycroft else if (sc->y < -127)
317 1.3 mycroft info.ymotion = -127;
318 1.1 andrew else
319 1.1 andrew info.ymotion = sc->y;
320 1.1 andrew
321 1.1 andrew /* Reset historical information */
322 1.1 andrew
323 1.1 andrew sc->x = 0;
324 1.1 andrew sc->y = 0;
325 1.1 andrew sc->status &= ~BUTCHNGMASK;
326 1.1 andrew
327 1.1 andrew /* Allow interrupts and copy result buffer */
328 1.1 andrew
329 1.1 andrew splx(s);
330 1.1 andrew error = copyout(&info, addr, sizeof(struct mouseinfo));
331 1.1 andrew break;
332 1.1 andrew
333 1.1 andrew default:
334 1.1 andrew error = EINVAL;
335 1.1 andrew break;
336 1.1 andrew }
337 1.1 andrew
338 1.1 andrew /* Return error code */
339 1.1 andrew
340 1.1 andrew return(error);
341 1.1 andrew }
342 1.1 andrew
343 1.1 andrew void lmsintr(unit)
344 1.1 andrew int unit;
345 1.1 andrew {
346 1.1 andrew struct lms_softc *sc = &lms_softc[unit];
347 1.1 andrew int ioport = lmsaddr[unit];
348 1.1 andrew char hi, lo, dx, dy, buttons, changed;
349 1.1 andrew
350 1.1 andrew outb(ioport+CNTRL, 0xAB);
351 1.1 andrew hi = inb(ioport+DATA) & 15;
352 1.1 andrew outb(ioport+CNTRL, 0x90);
353 1.1 andrew lo = inb(ioport+DATA) & 15;
354 1.1 andrew dx = (hi << 4) | lo;
355 1.3 mycroft dx = (dx == -128) ? -127 : dx;
356 1.3 mycroft
357 1.1 andrew outb(ioport+CNTRL, 0xF0);
358 1.1 andrew hi = inb(ioport+DATA);
359 1.1 andrew outb(ioport+CNTRL, 0xD0);
360 1.1 andrew lo = inb(ioport+DATA);
361 1.1 andrew outb(ioport+CNTRL, 0);
362 1.1 andrew dy = ((hi & 15) << 4) | (lo & 15);
363 1.1 andrew dy = (dy == -128) ? 127 : -dy;
364 1.3 mycroft
365 1.1 andrew buttons = (~hi >> 5) & 7;
366 1.1 andrew changed = buttons ^ sc->button;
367 1.1 andrew sc->button = buttons;
368 1.1 andrew sc->status = buttons | (sc->status & ~BUTSTATMASK) | (changed << 3);
369 1.1 andrew
370 1.1 andrew /* Update accumulated movements */
371 1.1 andrew
372 1.1 andrew sc->x += dx;
373 1.1 andrew sc->y += dy;
374 1.1 andrew
375 1.1 andrew /* If device in use and a change occurred... */
376 1.1 andrew
377 1.1 andrew if (sc->state & OPEN && (dx || dy || changed)) {
378 1.1 andrew sc->inq.queue[sc->inq.last++] = 0x80 | (buttons ^ BUTSTATMASK);
379 1.1 andrew sc->inq.queue[sc->inq.last++ % MSBSZ] = dx;
380 1.1 andrew sc->inq.queue[sc->inq.last++ % MSBSZ] = dy;
381 1.1 andrew sc->inq.queue[sc->inq.last++ % MSBSZ] = 0;
382 1.1 andrew sc->inq.queue[sc->inq.last++ % MSBSZ] = 0;
383 1.1 andrew sc->inq.last = sc->inq.last % MSBSZ;
384 1.1 andrew sc->inq.count += 5;
385 1.1 andrew
386 1.1 andrew if (sc->state & ASLP) {
387 1.1 andrew sc->state &= ~ASLP;
388 1.5 andrew wakeup((caddr_t)sc);
389 1.1 andrew }
390 1.5 andrew #ifdef NetBSD
391 1.5 andrew selwakeup(&sc->rsel);
392 1.5 andrew #else
393 1.1 andrew if (sc->rsel) {
394 1.5 andrew selwakeup(sc->rsel, 0);
395 1.1 andrew sc->rsel = 0;
396 1.1 andrew }
397 1.1 andrew #endif
398 1.5 andrew }
399 1.1 andrew }
400 1.1 andrew
401 1.1 andrew int lmsselect(dev_t dev, int rw, struct proc *p)
402 1.1 andrew {
403 1.1 andrew int s, ret;
404 1.1 andrew struct lms_softc *sc = &lms_softc[LMSUNIT(dev)];
405 1.1 andrew
406 1.1 andrew /* Silly to select for output */
407 1.1 andrew
408 1.1 andrew if (rw == FWRITE)
409 1.1 andrew return(0);
410 1.1 andrew
411 1.1 andrew /* Return true if a mouse event available */
412 1.1 andrew
413 1.1 andrew s = spltty();
414 1.1 andrew if (sc->inq.count)
415 1.1 andrew ret = 1;
416 1.1 andrew else {
417 1.5 andrew #ifdef NetBSD
418 1.5 andrew selrecord(p, &sc->rsel);
419 1.5 andrew #else
420 1.1 andrew sc->rsel = p->p_pid;
421 1.1 andrew #endif
422 1.1 andrew ret = 0;
423 1.1 andrew }
424 1.1 andrew splx(s);
425 1.1 andrew
426 1.1 andrew return(ret);
427 1.1 andrew }
428 1.1 andrew #endif
429