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