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