mms.c revision 1.6.2.3 1 1.1 andrew /*-
2 1.6.2.1 mycroft * Copyright (c) 1993 Charles Hannum
3 1.1 andrew * Copyright (c) 1992, 1993 Erik Forsberg.
4 1.1 andrew * All rights reserved.
5 1.1 andrew *
6 1.1 andrew * Redistribution and use in source and binary forms, with or without
7 1.1 andrew * modification, are permitted provided that the following conditions
8 1.1 andrew * are met:
9 1.1 andrew * 1. Redistributions of source code must retain the above copyright
10 1.1 andrew * notice, this list of conditions and the following disclaimer.
11 1.1 andrew *
12 1.1 andrew * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 1.1 andrew * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14 1.1 andrew * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
15 1.1 andrew * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 1.1 andrew * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 1.1 andrew * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 1.1 andrew * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 1.1 andrew * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 1.1 andrew * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 1.1 andrew * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 1.1 andrew *
23 1.6.2.3 mycroft * $Id: mms.c,v 1.6.2.3 1993/09/30 20:19:08 mycroft Exp $
24 1.1 andrew */
25 1.1 andrew
26 1.1 andrew #include "param.h"
27 1.1 andrew #include "kernel.h"
28 1.1 andrew #include "systm.h"
29 1.1 andrew #include "buf.h"
30 1.1 andrew #include "malloc.h"
31 1.1 andrew #include "ioctl.h"
32 1.1 andrew #include "tty.h"
33 1.1 andrew #include "file.h"
34 1.1 andrew #include "select.h"
35 1.1 andrew #include "proc.h"
36 1.1 andrew #include "vnode.h"
37 1.6.2.1 mycroft #include "sys/device.h"
38 1.1 andrew
39 1.6.2.1 mycroft #include "i386/isa/isavar.h"
40 1.6.2.3 mycroft #include "i386/isa/isa.h"
41 1.6.2.3 mycroft #include "i386/isa/icu.h"
42 1.1 andrew #include "i386/include/mouse.h"
43 1.6.2.1 mycroft #include "i386/include/pio.h"
44 1.1 andrew
45 1.6.2.1 mycroft #define MMS_ADDR 0 /* Offset for register select */
46 1.6.2.1 mycroft #define MMS_DATA 1 /* Offset for InPort data */
47 1.6.2.1 mycroft #define MMS_IDENT 2 /* Offset for identification register */
48 1.6.2.1 mycroft #define MMS_NPORTS 4
49 1.6.2.1 mycroft
50 1.6.2.1 mycroft #define MMS_CHUNK 128 /* chunk size for read */
51 1.6.2.1 mycroft #define MMS_BSIZE 1024 /* buffer size */
52 1.6.2.1 mycroft
53 1.6.2.1 mycroft struct mms_softc { /* Driver status information */
54 1.6.2.2 mycroft struct device sc_dev;
55 1.6.2.2 mycroft struct isadev sc_id;
56 1.6.2.2 mycroft struct intrhand sc_ih;
57 1.6.2.2 mycroft
58 1.6.2.2 mycroft struct ringbuf { /* Input queue */
59 1.6.2.1 mycroft int rb_count, rb_first, rb_last;
60 1.6.2.1 mycroft char rb_data[MMS_BSIZE];
61 1.6.2.1 mycroft } sc_q;
62 1.6.2.1 mycroft struct selinfo sc_rsel;
63 1.6.2.1 mycroft u_short sc_iobase; /* I/O port base */
64 1.6.2.1 mycroft u_char sc_flags; /* Driver flags */
65 1.6.2.1 mycroft #define MMS_NOBLOCK 0x01
66 1.6.2.1 mycroft u_char sc_state; /* Mouse driver state */
67 1.6.2.1 mycroft #define MMS_OPEN 0x01 /* Device is open */
68 1.6.2.1 mycroft #define MMS_ASLP 0x02 /* Waiting for mouse data */
69 1.6.2.1 mycroft u_char sc_status; /* Mouse button status */
70 1.6.2.1 mycroft int sc_x, sc_y; /* accumulated motion in the X,Y axis */
71 1.1 andrew };
72 1.1 andrew
73 1.6.2.1 mycroft static int mmsprobe __P((struct device *, struct cfdata *, void *));
74 1.6.2.1 mycroft static void mmsforceintr __P((void *));
75 1.6.2.1 mycroft static void mmsattach __P((struct device *, struct device *, void *));
76 1.6.2.1 mycroft static int mmsintr __P((void *));
77 1.1 andrew
78 1.6.2.1 mycroft struct cfdriver mmscd =
79 1.6.2.1 mycroft { NULL, "mms", mmsprobe, mmsattach, sizeof (struct mms_softc) };
80 1.1 andrew
81 1.6.2.1 mycroft #define MMSUNIT(dev) (minor(dev) >> 1)
82 1.6.2.1 mycroft #define MMSFLAGS(dev) (minor(dev) & 0x01)
83 1.1 andrew
84 1.6.2.1 mycroft static int
85 1.6.2.1 mycroft mmsprobe(parent, cf, aux)
86 1.6.2.1 mycroft struct device *parent;
87 1.6.2.1 mycroft struct cfdata *cf;
88 1.6.2.1 mycroft void *aux;
89 1.1 andrew {
90 1.6.2.1 mycroft struct isa_attach_args *ia = aux;
91 1.6.2.1 mycroft u_short iobase = ia->ia_iobase;
92 1.1 andrew
93 1.6.2.3 mycroft if (iobase == IOBASEUNK)
94 1.6.2.3 mycroft return 0;
95 1.6.2.3 mycroft
96 1.1 andrew /* Read identification register to see if present */
97 1.6.2.1 mycroft if (inb(iobase + MMS_IDENT) != 0xde)
98 1.6.2.1 mycroft return 0;
99 1.1 andrew
100 1.1 andrew /* Seems it was there; reset */
101 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0x87);
102 1.1 andrew
103 1.6.2.3 mycroft if (ia->ia_irq == IRQUNK) {
104 1.6.2.3 mycroft ia->ia_irq = isa_discoverintr(mmsforceintr, aux);
105 1.6.2.3 mycroft if (ia->ia_irq == IRQNONE)
106 1.6.2.3 mycroft return 0;
107 1.6.2.3 mycroft }
108 1.1 andrew
109 1.6.2.1 mycroft /* reset again to disable interrupts */
110 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0x87);
111 1.1 andrew
112 1.6.2.1 mycroft ia->ia_iosize = MMS_NPORTS;
113 1.6.2.1 mycroft ia->ia_drq = DRQUNK;
114 1.6.2.1 mycroft ia->ia_msize = 0;
115 1.6.2.1 mycroft return 1;
116 1.1 andrew }
117 1.1 andrew
118 1.6.2.1 mycroft static void
119 1.6.2.1 mycroft mmsforceintr(aux)
120 1.6.2.1 mycroft void *aux;
121 1.1 andrew {
122 1.6.2.1 mycroft struct isa_attach_args *ia = aux;
123 1.6.2.1 mycroft u_short iobase = ia->ia_iobase;
124 1.1 andrew
125 1.6.2.1 mycroft /* enable interrupts; expect to get one in 1/60 second */
126 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0x07);
127 1.6.2.1 mycroft outb(iobase + MMS_DATA, 0x09);
128 1.1 andrew }
129 1.1 andrew
130 1.6.2.1 mycroft static void
131 1.6.2.1 mycroft mmsattach(parent, self, aux)
132 1.6.2.1 mycroft struct device *parent, *self;
133 1.6.2.1 mycroft void *aux;
134 1.1 andrew {
135 1.6.2.1 mycroft struct mms_softc *sc = (struct mms_softc *)self;
136 1.6.2.1 mycroft struct isa_attach_args *ia = aux;
137 1.6.2.1 mycroft u_short iobase = ia->ia_iobase;
138 1.6.2.1 mycroft
139 1.6.2.1 mycroft /* other initialization done by mmsprobe */
140 1.6.2.1 mycroft sc->sc_iobase = iobase;
141 1.6.2.1 mycroft sc->sc_state = 0;
142 1.1 andrew
143 1.6.2.2 mycroft printf(": Microsoft mouse\n");
144 1.6.2.3 mycroft isa_establish(&sc->sc_id, &sc->sc_dev);
145 1.6.2.2 mycroft
146 1.6.2.3 mycroft sc->sc_ih.ih_fun = mmsintr;
147 1.6.2.3 mycroft sc->sc_ih.ih_arg = sc;
148 1.6.2.3 mycroft intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
149 1.6.2.1 mycroft }
150 1.1 andrew
151 1.6.2.1 mycroft int
152 1.6.2.1 mycroft mmsopen(dev, flag)
153 1.6.2.1 mycroft dev_t dev;
154 1.6.2.1 mycroft int flag;
155 1.6.2.1 mycroft {
156 1.6.2.1 mycroft int unit = MMSUNIT(dev);
157 1.6.2.1 mycroft struct mms_softc *sc;
158 1.6.2.1 mycroft u_short iobase;
159 1.6.2.1 mycroft
160 1.6.2.1 mycroft if (unit >= mmscd.cd_ndevs)
161 1.6.2.1 mycroft return ENXIO;
162 1.6.2.2 mycroft sc = mmscd.cd_devs[unit];
163 1.6.2.1 mycroft if (!sc)
164 1.6.2.1 mycroft return ENXIO;
165 1.6.2.1 mycroft
166 1.6.2.1 mycroft if (sc->sc_state & MMS_OPEN)
167 1.6.2.1 mycroft return EBUSY;
168 1.6.2.1 mycroft
169 1.6.2.1 mycroft sc->sc_state |= MMS_OPEN;
170 1.6.2.1 mycroft sc->sc_status = 0;
171 1.6.2.1 mycroft sc->sc_x = sc->sc_y = 0;
172 1.6.2.1 mycroft sc->sc_q.rb_count = sc->sc_q.rb_first = sc->sc_q.rb_last = 0;
173 1.6.2.1 mycroft
174 1.6.2.1 mycroft /* enable interrupts */
175 1.6.2.1 mycroft iobase = sc->sc_iobase;
176 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0x07);
177 1.6.2.1 mycroft outb(iobase + MMS_DATA, 0x09);
178 1.1 andrew
179 1.6.2.1 mycroft return 0;
180 1.6.2.1 mycroft }
181 1.1 andrew
182 1.6.2.1 mycroft int
183 1.6.2.1 mycroft mmsclose(dev, flag)
184 1.6.2.1 mycroft dev_t dev;
185 1.6.2.1 mycroft int flag;
186 1.6.2.1 mycroft {
187 1.6.2.1 mycroft int unit = MMSUNIT(dev);
188 1.6.2.2 mycroft struct mms_softc *sc = mmscd.cd_devs[unit];
189 1.1 andrew
190 1.6.2.1 mycroft /* disable interrupts */
191 1.6.2.1 mycroft outb(sc->sc_iobase + MMS_ADDR, 0x87);
192 1.1 andrew
193 1.6.2.1 mycroft sc->sc_state &= ~MMS_OPEN;
194 1.1 andrew
195 1.6.2.1 mycroft return 0;
196 1.1 andrew }
197 1.1 andrew
198 1.6.2.1 mycroft int
199 1.6.2.1 mycroft mmsread(dev, uio, flag)
200 1.6.2.1 mycroft dev_t dev;
201 1.6.2.1 mycroft struct uio *uio;
202 1.6.2.1 mycroft int flag;
203 1.1 andrew {
204 1.6.2.1 mycroft int unit = MMSUNIT(dev);
205 1.6.2.2 mycroft struct mms_softc *sc = mmscd.cd_devs[unit];
206 1.6.2.1 mycroft int s;
207 1.6.2.1 mycroft int error;
208 1.6.2.1 mycroft size_t length;
209 1.6.2.1 mycroft u_char buffer[MMS_CHUNK];
210 1.1 andrew
211 1.1 andrew /* Block until mouse activity occured */
212 1.1 andrew
213 1.1 andrew s = spltty();
214 1.6.2.1 mycroft while (sc->sc_q.rb_count == 0) {
215 1.6.2.1 mycroft if (sc->sc_flags & MMS_NOBLOCK) {
216 1.1 andrew splx(s);
217 1.6.2.1 mycroft return EWOULDBLOCK;
218 1.1 andrew }
219 1.6.2.1 mycroft sc->sc_state |= MMS_ASLP;
220 1.6.2.1 mycroft if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0)) {
221 1.6.2.1 mycroft sc->sc_state &= ~MMS_ASLP;
222 1.1 andrew splx(s);
223 1.6.2.1 mycroft return error;
224 1.1 andrew }
225 1.1 andrew }
226 1.1 andrew
227 1.1 andrew /* Transfer as many chunks as possible */
228 1.1 andrew
229 1.6.2.1 mycroft while (sc->sc_q.rb_count > 0 && uio->uio_resid > 0) {
230 1.6.2.1 mycroft length = min(sc->sc_q.rb_count, uio->uio_resid);
231 1.1 andrew if (length > sizeof(buffer))
232 1.1 andrew length = sizeof(buffer);
233 1.1 andrew
234 1.1 andrew /* Remove a small chunk from input queue */
235 1.1 andrew
236 1.6.2.1 mycroft if (sc->sc_q.rb_first + length >= MMS_BSIZE) {
237 1.6.2.1 mycroft size_t left = MMS_BSIZE - sc->sc_q.rb_first;
238 1.6.2.1 mycroft bcopy(&sc->sc_q.rb_data[sc->sc_q.rb_first], buffer,
239 1.6.2.1 mycroft left);
240 1.6.2.1 mycroft bcopy(sc->sc_q.rb_data, &buffer[left], length - left);
241 1.6.2.1 mycroft } else
242 1.6.2.1 mycroft bcopy(&sc->sc_q.rb_data[sc->sc_q.rb_first], buffer,
243 1.6.2.1 mycroft length);
244 1.1 andrew
245 1.6.2.1 mycroft sc->sc_q.rb_first = (sc->sc_q.rb_first + length) % MMS_BSIZE;
246 1.6.2.1 mycroft sc->sc_q.rb_count -= length;
247 1.1 andrew
248 1.1 andrew /* Copy data to user process */
249 1.1 andrew
250 1.6.2.1 mycroft if (error = uiomove(buffer, length, uio))
251 1.1 andrew break;
252 1.1 andrew }
253 1.1 andrew
254 1.6.2.1 mycroft /* reset counters */
255 1.6.2.1 mycroft sc->sc_x = sc->sc_y = 0;
256 1.1 andrew
257 1.1 andrew splx(s);
258 1.6.2.1 mycroft return error;
259 1.1 andrew }
260 1.1 andrew
261 1.6.2.1 mycroft int
262 1.6.2.1 mycroft mmsioctl(dev, cmd, addr, flag)
263 1.6.2.1 mycroft dev_t dev;
264 1.6.2.1 mycroft int cmd;
265 1.6.2.1 mycroft caddr_t addr;
266 1.6.2.1 mycroft int flag;
267 1.1 andrew {
268 1.6.2.1 mycroft int unit = MMSUNIT(dev);
269 1.6.2.2 mycroft struct mms_softc *sc = mmscd.cd_devs[unit];
270 1.6.2.1 mycroft struct mouseinfo info;
271 1.6.2.1 mycroft int s;
272 1.6.2.1 mycroft int error;
273 1.1 andrew
274 1.1 andrew switch (cmd) {
275 1.6.2.1 mycroft case MOUSEIOCREAD:
276 1.1 andrew s = spltty();
277 1.1 andrew
278 1.6.2.1 mycroft info.status = sc->sc_status;
279 1.6.2.1 mycroft if (sc->sc_x || sc->sc_y)
280 1.1 andrew info.status |= MOVEMENT;
281 1.1 andrew
282 1.6.2.1 mycroft if (sc->sc_x > 127)
283 1.1 andrew info.xmotion = 127;
284 1.6.2.1 mycroft else if (sc->sc_x < -127)
285 1.6.2.1 mycroft /* bounding at -127 avoids a bug in XFree86 */
286 1.6.2.1 mycroft info.xmotion = -127;
287 1.1 andrew else
288 1.6.2.1 mycroft info.xmotion = sc->sc_x;
289 1.1 andrew
290 1.6.2.1 mycroft if (sc->sc_y > 127)
291 1.1 andrew info.ymotion = 127;
292 1.6.2.1 mycroft else if (sc->sc_y < -127)
293 1.6.2.1 mycroft info.ymotion = -127;
294 1.1 andrew else
295 1.6.2.1 mycroft info.ymotion = sc->sc_y;
296 1.1 andrew
297 1.6.2.1 mycroft sc->sc_x = 0;
298 1.6.2.1 mycroft sc->sc_y = 0;
299 1.6.2.1 mycroft sc->sc_status &= ~BUTCHNGMASK;
300 1.1 andrew
301 1.1 andrew splx(s);
302 1.1 andrew error = copyout(&info, addr, sizeof(struct mouseinfo));
303 1.1 andrew break;
304 1.1 andrew
305 1.6.2.1 mycroft default:
306 1.1 andrew error = EINVAL;
307 1.1 andrew break;
308 1.6.2.1 mycroft }
309 1.1 andrew
310 1.6.2.1 mycroft return error;
311 1.1 andrew }
312 1.1 andrew
313 1.6.2.1 mycroft int
314 1.6.2.1 mycroft mmsintr(arg)
315 1.6.2.1 mycroft void *arg;
316 1.1 andrew {
317 1.6.2.1 mycroft struct mms_softc *sc = (struct mms_softc *)arg;
318 1.6.2.1 mycroft u_short iobase = sc->sc_iobase;
319 1.6.2.1 mycroft u_char buttons, changed, status;
320 1.6.2.1 mycroft char dx, dy;
321 1.6.2.1 mycroft
322 1.6.2.1 mycroft if ((sc->sc_state & MMS_OPEN) == 0)
323 1.6.2.1 mycroft /* interrupts not expected */
324 1.6.2.1 mycroft return 0;
325 1.1 andrew
326 1.1 andrew /* Freeze InPort registers (disabling interrupts) */
327 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0x07);
328 1.6.2.1 mycroft outb(iobase + MMS_DATA, 0x29);
329 1.1 andrew
330 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0);
331 1.6.2.1 mycroft status = inb(iobase + MMS_DATA);
332 1.1 andrew
333 1.1 andrew if (status & 0x40) {
334 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 1);
335 1.6.2.1 mycroft dx = inb(iobase + MMS_DATA);
336 1.6.2.1 mycroft dx = (dx == -128) ? -127 : dx;
337 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 2);
338 1.6.2.1 mycroft dy = inb(iobase + MMS_DATA);
339 1.1 andrew dy = (dy == -128) ? 127 : -dy;
340 1.6.2.1 mycroft } else
341 1.1 andrew dx = dy = 0;
342 1.1 andrew
343 1.1 andrew /* Unfreeze InPort Registers (re-enables interrupts) */
344 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0x07);
345 1.6.2.1 mycroft outb(iobase + MMS_DATA, 0x09);
346 1.1 andrew
347 1.6.2.1 mycroft buttons = status & BUTSTATMASK;
348 1.6.2.1 mycroft changed = status & BUTCHNGMASK;
349 1.6.2.1 mycroft sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
350 1.6.2.1 mycroft
351 1.6.2.1 mycroft if (dx || dy || changed) {
352 1.6.2.1 mycroft int last = sc->sc_q.rb_last;
353 1.6.2.1 mycroft char *cp = &sc->sc_q.rb_data[last];
354 1.6.2.1 mycroft int count = sc->sc_q.rb_count;
355 1.6.2.1 mycroft
356 1.6.2.1 mycroft /* Update accumulated movements */
357 1.6.2.1 mycroft sc->sc_x += dx;
358 1.6.2.1 mycroft sc->sc_y += dy;
359 1.6.2.1 mycroft
360 1.6.2.1 mycroft if ((count += 5) > MMS_BSIZE)
361 1.6.2.1 mycroft return 1;
362 1.6.2.1 mycroft sc->sc_q.rb_count = count;
363 1.6.2.1 mycroft
364 1.6.2.1 mycroft #define next() \
365 1.6.2.1 mycroft if (++last >= MMS_BSIZE) { \
366 1.6.2.1 mycroft last = 0; \
367 1.6.2.1 mycroft cp = sc->sc_q.rb_data; \
368 1.6.2.1 mycroft } else \
369 1.6.2.1 mycroft cp++;
370 1.6.2.1 mycroft *cp = 0x80 | (~status & BUTSTATMASK);
371 1.6.2.1 mycroft next();
372 1.6.2.1 mycroft *cp = dx;
373 1.6.2.1 mycroft next();
374 1.6.2.1 mycroft *cp = dy;
375 1.6.2.1 mycroft next();
376 1.6.2.1 mycroft *cp = 0;
377 1.6.2.1 mycroft next();
378 1.6.2.1 mycroft *cp = 0;
379 1.6.2.1 mycroft next();
380 1.6.2.1 mycroft sc->sc_q.rb_last = last;
381 1.1 andrew
382 1.6.2.1 mycroft if (sc->sc_state & MMS_ASLP) {
383 1.6.2.1 mycroft sc->sc_state &= ~MMS_ASLP;
384 1.5 andrew wakeup((caddr_t)sc);
385 1.1 andrew }
386 1.6.2.1 mycroft selwakeup(&sc->sc_rsel);
387 1.6.2.1 mycroft }
388 1.6.2.1 mycroft
389 1.6.2.1 mycroft return 1;
390 1.1 andrew }
391 1.1 andrew
392 1.6.2.1 mycroft int
393 1.6.2.1 mycroft mmsselect(dev, rw, p)
394 1.6.2.1 mycroft dev_t dev;
395 1.6.2.1 mycroft int rw;
396 1.6.2.1 mycroft struct proc *p;
397 1.1 andrew {
398 1.6.2.1 mycroft int unit = MMSUNIT(dev);
399 1.6.2.2 mycroft struct mms_softc *sc = mmscd.cd_devs[unit];
400 1.6.2.1 mycroft int s;
401 1.6.2.1 mycroft int ret;
402 1.1 andrew
403 1.1 andrew if (rw == FWRITE)
404 1.6.2.1 mycroft return 0;
405 1.1 andrew
406 1.1 andrew s = spltty();
407 1.6.2.1 mycroft if (sc->sc_q.rb_count)
408 1.1 andrew ret = 1;
409 1.1 andrew else {
410 1.6.2.1 mycroft selrecord(p, &sc->sc_rsel);
411 1.1 andrew ret = 0;
412 1.1 andrew }
413 1.1 andrew splx(s);
414 1.1 andrew
415 1.6.2.1 mycroft return ret;
416 1.1 andrew }
417