mms.c revision 1.4 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
39 1.1 andrew /* define this if you're using 386BSD rather than NetBSD */
40 1.1 andrew /* #define 386BSD_KERNEL */
41 1.1 andrew
42 1.2 andrew #include "mms.h"
43 1.1 andrew
44 1.2 andrew #if NMMS > 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.1 andrew #ifndef 386BSD_KERNEL
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 ADDR 0 /* Offset for register select */
65 1.1 andrew #define DATA 1 /* Offset for InPort data */
66 1.1 andrew #define IDENT 2 /* Offset for identification register */
67 1.1 andrew
68 1.2 andrew #define MMSUNIT(dev) (minor(dev) >> 1)
69 1.1 andrew
70 1.1 andrew #ifndef min
71 1.1 andrew #define min(x,y) (x < y ? x : y)
72 1.1 andrew #endif min
73 1.1 andrew
74 1.2 andrew int mmsprobe (struct isa_device *);
75 1.2 andrew int mmsattach (struct isa_device *);
76 1.1 andrew
77 1.2 andrew static int mmsaddr[NMMS]; /* Base I/O port addresses per unit */
78 1.1 andrew
79 1.1 andrew #define MSBSZ 1024 /* Output queue size (pwr of 2 is best) */
80 1.1 andrew
81 1.1 andrew struct ringbuf {
82 1.1 andrew int count, first, last;
83 1.1 andrew char queue[MSBSZ];
84 1.1 andrew };
85 1.1 andrew
86 1.2 andrew static struct mms_softc { /* Driver status information */
87 1.1 andrew struct ringbuf inq; /* Input queue */
88 1.1 andrew #ifdef 386BSD_KERNEL
89 1.1 andrew pid_t rsel; /* Process selecting for Input */
90 1.1 andrew #else
91 1.1 andrew struct selinfo rsel;
92 1.1 andrew #endif
93 1.1 andrew unsigned char state; /* Mouse driver state */
94 1.1 andrew unsigned char status; /* Mouse button status */
95 1.1 andrew int x, y; /* accumulated motion in the X,Y axis */
96 1.2 andrew } mms_softc[NMMS];
97 1.1 andrew
98 1.1 andrew #define OPEN 1 /* Device is open */
99 1.1 andrew #define ASLP 2 /* Waiting for mouse data */
100 1.1 andrew
101 1.2 andrew struct isa_driver mmsdriver = { mmsprobe, mmsattach, "mms" };
102 1.1 andrew
103 1.2 andrew int mmsprobe(struct isa_device *dvp)
104 1.1 andrew {
105 1.1 andrew int ioport = dvp->id_iobase;
106 1.1 andrew
107 1.1 andrew /* Read identification register to see if present */
108 1.1 andrew
109 1.1 andrew if (inb(ioport+IDENT) != 0xDE)
110 1.1 andrew return(0);
111 1.1 andrew
112 1.1 andrew /* Seems it was there; reset */
113 1.1 andrew
114 1.1 andrew outb(ioport+ADDR, 0x87);
115 1.4 mycroft return(4);
116 1.1 andrew }
117 1.1 andrew
118 1.2 andrew int mmsattach(struct isa_device *dvp)
119 1.1 andrew {
120 1.1 andrew int unit = dvp->id_unit;
121 1.1 andrew int ioport = dvp->id_iobase;
122 1.2 andrew struct mms_softc *sc = &mms_softc[unit];
123 1.1 andrew
124 1.1 andrew /* Save I/O base address */
125 1.1 andrew
126 1.2 andrew mmsaddr[unit] = ioport;
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.2 andrew int mmsopen(dev_t dev, int flag, int fmt, struct proc *p)
138 1.1 andrew {
139 1.2 andrew int unit = MMSUNIT(dev);
140 1.2 andrew struct mms_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.2 andrew if (unit >= NMMS)
146 1.1 andrew return(ENXIO);
147 1.1 andrew
148 1.1 andrew /* Get device data */
149 1.1 andrew
150 1.2 andrew sc = &mms_softc[unit];
151 1.2 andrew ioport = mmsaddr[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.3 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.1 andrew #ifdef 386BSD_KERNEL
167 1.1 andrew sc->rsel = 0;
168 1.1 andrew #else
169 1.1 andrew sc->rsel.si_pid = 0;
170 1.1 andrew sc->rsel.si_coll = 0;
171 1.1 andrew #endif
172 1.1 andrew sc->status = 0;
173 1.1 andrew sc->x = 0;
174 1.1 andrew sc->y = 0;
175 1.1 andrew
176 1.1 andrew /* Allocate and initialize a ring buffer */
177 1.1 andrew
178 1.1 andrew sc->inq.count = sc->inq.first = sc->inq.last = 0;
179 1.1 andrew
180 1.1 andrew /* Setup Bus Mouse */
181 1.1 andrew
182 1.1 andrew outb(ioport+ADDR, 7);
183 1.1 andrew outb(ioport+DATA, 0x09);
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.2 andrew int mmsclose(dev_t dev, int flag, int fmt, struct proc *p)
191 1.1 andrew {
192 1.1 andrew int unit, ioport;
193 1.2 andrew struct mms_softc *sc;
194 1.1 andrew
195 1.1 andrew /* Get unit and associated info */
196 1.1 andrew
197 1.2 andrew unit = MMSUNIT(dev);
198 1.2 andrew sc = &mms_softc[unit];
199 1.2 andrew ioport = mmsaddr[unit];
200 1.1 andrew
201 1.1 andrew /* Reset Bus Mouse */
202 1.1 andrew
203 1.1 andrew outb(ioport+ADDR, 0x87);
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.2 andrew int mmsread(dev_t dev, struct uio *uio, int flag)
215 1.1 andrew {
216 1.1 andrew int s, error = 0;
217 1.1 andrew unsigned length;
218 1.2 andrew struct mms_softc *sc;
219 1.1 andrew unsigned char buffer[100];
220 1.1 andrew
221 1.1 andrew /* Get device information */
222 1.1 andrew
223 1.2 andrew sc = &mms_softc[MMSUNIT(dev)];
224 1.1 andrew
225 1.1 andrew /* Block until mouse activity occured */
226 1.1 andrew
227 1.1 andrew s = spltty();
228 1.1 andrew while (sc->inq.count == 0) {
229 1.1 andrew if (minor(dev) & 0x1) {
230 1.1 andrew splx(s);
231 1.1 andrew return(EWOULDBLOCK);
232 1.1 andrew }
233 1.1 andrew sc->state |= ASLP;
234 1.2 andrew error = tsleep(sc, PZERO | PCATCH, "mmsrea", 0);
235 1.1 andrew if (error != 0) {
236 1.1 andrew splx(s);
237 1.1 andrew return(error);
238 1.1 andrew }
239 1.1 andrew }
240 1.1 andrew
241 1.1 andrew /* Transfer as many chunks as possible */
242 1.1 andrew
243 1.1 andrew while (sc->inq.count > 0 && uio->uio_resid > 0) {
244 1.1 andrew length = min(sc->inq.count, uio->uio_resid);
245 1.1 andrew if (length > sizeof(buffer))
246 1.1 andrew length = sizeof(buffer);
247 1.1 andrew
248 1.1 andrew /* Remove a small chunk from input queue */
249 1.1 andrew
250 1.1 andrew if (sc->inq.first + length >= MSBSZ) {
251 1.1 andrew bcopy(&sc->inq.queue[sc->inq.first],
252 1.1 andrew buffer, MSBSZ - sc->inq.first);
253 1.1 andrew bcopy(sc->inq.queue, &buffer[MSBSZ-sc->inq.first],
254 1.1 andrew length - (MSBSZ - sc->inq.first));
255 1.1 andrew }
256 1.1 andrew else
257 1.1 andrew bcopy(&sc->inq.queue[sc->inq.first], buffer, length);
258 1.1 andrew
259 1.1 andrew sc->inq.first = (sc->inq.first + length) % MSBSZ;
260 1.1 andrew sc->inq.count -= length;
261 1.1 andrew
262 1.1 andrew /* Copy data to user process */
263 1.1 andrew
264 1.1 andrew error = uiomove(buffer, length, uio);
265 1.1 andrew if (error)
266 1.1 andrew break;
267 1.1 andrew }
268 1.1 andrew
269 1.1 andrew sc->x = sc->y = 0;
270 1.1 andrew
271 1.1 andrew /* Allow interrupts again */
272 1.1 andrew
273 1.1 andrew splx(s);
274 1.1 andrew return(error);
275 1.1 andrew }
276 1.1 andrew
277 1.2 andrew int mmsioctl(dev_t dev, caddr_t addr, int cmd, int flag, struct proc *p)
278 1.1 andrew {
279 1.2 andrew struct mms_softc *sc;
280 1.1 andrew struct mouseinfo info;
281 1.1 andrew int s, error;
282 1.1 andrew
283 1.1 andrew /* Get device information */
284 1.1 andrew
285 1.2 andrew sc = &mms_softc[MMSUNIT(dev)];
286 1.1 andrew
287 1.1 andrew /* Perform IOCTL command */
288 1.1 andrew
289 1.1 andrew switch (cmd) {
290 1.1 andrew
291 1.1 andrew case MOUSEIOCREAD:
292 1.1 andrew
293 1.1 andrew /* Don't modify info while calculating */
294 1.1 andrew
295 1.1 andrew s = spltty();
296 1.1 andrew
297 1.1 andrew /* Build mouse status octet */
298 1.1 andrew
299 1.1 andrew info.status = sc->status;
300 1.1 andrew if (sc->x || sc->y)
301 1.1 andrew info.status |= MOVEMENT;
302 1.1 andrew
303 1.1 andrew /* Encode X and Y motion as good as we can */
304 1.1 andrew
305 1.1 andrew if (sc->x > 127)
306 1.1 andrew info.xmotion = 127;
307 1.1 andrew else if (sc->x < -128)
308 1.1 andrew info.xmotion = -128;
309 1.1 andrew else
310 1.1 andrew info.xmotion = sc->x;
311 1.1 andrew
312 1.1 andrew if (sc->y > 127)
313 1.1 andrew info.ymotion = 127;
314 1.1 andrew else if (sc->y < -128)
315 1.1 andrew info.ymotion = -128;
316 1.1 andrew else
317 1.1 andrew info.ymotion = sc->y;
318 1.1 andrew
319 1.1 andrew /* Reset historical information */
320 1.1 andrew
321 1.1 andrew sc->x = 0;
322 1.1 andrew sc->y = 0;
323 1.1 andrew sc->status &= ~BUTCHNGMASK;
324 1.1 andrew
325 1.1 andrew /* Allow interrupts and copy result buffer */
326 1.1 andrew
327 1.1 andrew splx(s);
328 1.1 andrew error = copyout(&info, addr, sizeof(struct mouseinfo));
329 1.1 andrew break;
330 1.1 andrew
331 1.1 andrew default:
332 1.1 andrew error = EINVAL;
333 1.1 andrew break;
334 1.1 andrew }
335 1.1 andrew
336 1.1 andrew /* Return error code */
337 1.1 andrew
338 1.1 andrew return(error);
339 1.1 andrew }
340 1.1 andrew
341 1.2 andrew void mmsintr(unit)
342 1.1 andrew int unit;
343 1.1 andrew {
344 1.2 andrew struct mms_softc *sc = &mms_softc[unit];
345 1.2 andrew int ioport = mmsaddr[unit];
346 1.1 andrew char dx, dy, status;
347 1.1 andrew
348 1.1 andrew /* Freeze InPort registers (disabling interrupts) */
349 1.1 andrew
350 1.1 andrew outb(ioport+ADDR, 7);
351 1.1 andrew outb(ioport+DATA, 0x29);
352 1.1 andrew
353 1.1 andrew /* Read mouse status */
354 1.1 andrew
355 1.1 andrew outb(ioport+ADDR, 0);
356 1.1 andrew status = inb(ioport+DATA);
357 1.1 andrew
358 1.1 andrew /* Check if any movement detected */
359 1.1 andrew
360 1.1 andrew if (status & 0x40) {
361 1.1 andrew outb(ioport+ADDR, 1);
362 1.1 andrew dx = inb(ioport+DATA);
363 1.1 andrew outb(ioport+ADDR, 2);
364 1.1 andrew dy = inb(ioport+DATA);
365 1.1 andrew dy = (dy == -128) ? 127 : -dy;
366 1.1 andrew }
367 1.1 andrew else
368 1.1 andrew dx = dy = 0;
369 1.1 andrew
370 1.1 andrew /* Unfreeze InPort Registers (re-enables interrupts) */
371 1.1 andrew
372 1.1 andrew outb(ioport+ADDR, 7);
373 1.1 andrew outb(ioport+DATA, 0x09);
374 1.1 andrew
375 1.1 andrew /* Update accumulated movements */
376 1.1 andrew
377 1.1 andrew sc->x += dx;
378 1.1 andrew sc->y += dy;
379 1.1 andrew
380 1.1 andrew /* Inclusive OR status changes, but always save only last state */
381 1.1 andrew
382 1.1 andrew sc->status |= status & BUTCHNGMASK;
383 1.1 andrew sc->status = (sc->status & ~BUTSTATMASK) | (status & BUTSTATMASK);
384 1.1 andrew
385 1.1 andrew /* If device in use and a change occurred... */
386 1.1 andrew
387 1.1 andrew if (sc->state & OPEN && status & 0x78 && sc->inq.count < (MSBSZ-5)) {
388 1.1 andrew status &= BUTSTATMASK;
389 1.1 andrew sc->inq.queue[sc->inq.last++] = 0x80 | (status ^ BUTSTATMASK);
390 1.1 andrew sc->inq.queue[sc->inq.last++ % MSBSZ] = dx;
391 1.1 andrew sc->inq.queue[sc->inq.last++ % MSBSZ] = dy;
392 1.1 andrew sc->inq.queue[sc->inq.last++ % MSBSZ] = 0;
393 1.1 andrew sc->inq.queue[sc->inq.last++ % MSBSZ] = 0;
394 1.1 andrew sc->inq.last = sc->inq.last % MSBSZ;
395 1.1 andrew sc->inq.count += 5;
396 1.1 andrew
397 1.1 andrew if (sc->state & ASLP) {
398 1.1 andrew sc->state &= ~ASLP;
399 1.1 andrew wakeup(sc);
400 1.1 andrew }
401 1.1 andrew #ifdef 386BSD_KERNEL
402 1.1 andrew if (sc->rsel) {
403 1.1 andrew selwakeup(&sc->rsel, 0);
404 1.1 andrew sc->rsel = 0;
405 1.1 andrew }
406 1.1 andrew #else
407 1.1 andrew selwakeup(&sc->rsel);
408 1.1 andrew #endif
409 1.1 andrew }
410 1.1 andrew }
411 1.1 andrew
412 1.2 andrew int mmsselect(dev_t dev, int rw, struct proc *p)
413 1.1 andrew {
414 1.1 andrew int s, ret;
415 1.2 andrew struct mms_softc *sc = &mms_softc[MMSUNIT(dev)];
416 1.1 andrew
417 1.1 andrew /* Silly to select for output */
418 1.1 andrew
419 1.1 andrew if (rw == FWRITE)
420 1.1 andrew return(0);
421 1.1 andrew
422 1.1 andrew /* Return true if a mouse event available */
423 1.1 andrew
424 1.1 andrew s = spltty();
425 1.1 andrew if (sc->inq.count)
426 1.1 andrew ret = 1;
427 1.1 andrew else {
428 1.1 andrew #ifdef 386BSD_KERNEL
429 1.1 andrew sc->rsel = p->p_pid;
430 1.1 andrew #else
431 1.1 andrew selrecord(p, &sc->rsel);
432 1.1 andrew #endif
433 1.1 andrew ret = 0;
434 1.1 andrew }
435 1.1 andrew splx(s);
436 1.1 andrew
437 1.1 andrew return(ret);
438 1.1 andrew }
439 1.1 andrew #endif
440