mms.c revision 1.6.2.11 1 1.1 andrew /*-
2 1.6.2.8 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.11 mycroft * $Id: mms.c,v 1.6.2.11 1993/10/31 16:55:30 mycroft Exp $
24 1.1 andrew */
25 1.1 andrew
26 1.6.2.10 mycroft #include <sys/param.h>
27 1.6.2.10 mycroft #include <sys/kernel.h>
28 1.6.2.10 mycroft #include <sys/systm.h>
29 1.6.2.10 mycroft #include <sys/buf.h>
30 1.6.2.10 mycroft #include <sys/malloc.h>
31 1.6.2.10 mycroft #include <sys/ioctl.h>
32 1.6.2.10 mycroft #include <sys/tty.h>
33 1.6.2.10 mycroft #include <sys/file.h>
34 1.6.2.10 mycroft #include <sys/select.h>
35 1.6.2.10 mycroft #include <sys/proc.h>
36 1.6.2.10 mycroft #include <sys/vnode.h>
37 1.6.2.10 mycroft #include <sys/device.h>
38 1.6.2.10 mycroft
39 1.6.2.10 mycroft #include <machine/cpu.h>
40 1.6.2.10 mycroft #include <machine/mouse.h>
41 1.6.2.10 mycroft #include <machine/pio.h>
42 1.1 andrew
43 1.6.2.10 mycroft #include <i386/isa/isavar.h>
44 1.6.2.10 mycroft #include <i386/isa/icu.h>
45 1.1 andrew
46 1.6.2.1 mycroft #define MMS_ADDR 0 /* Offset for register select */
47 1.6.2.1 mycroft #define MMS_DATA 1 /* Offset for InPort data */
48 1.6.2.1 mycroft #define MMS_IDENT 2 /* Offset for identification register */
49 1.6.2.1 mycroft #define MMS_NPORTS 4
50 1.6.2.1 mycroft
51 1.6.2.1 mycroft #define MMS_CHUNK 128 /* chunk size for read */
52 1.6.2.5 mycroft #define MMS_BSIZE 1020 /* buffer size */
53 1.6.2.1 mycroft
54 1.6.2.1 mycroft struct mms_softc { /* Driver status information */
55 1.6.2.2 mycroft struct device sc_dev;
56 1.6.2.2 mycroft struct isadev sc_id;
57 1.6.2.2 mycroft struct intrhand sc_ih;
58 1.6.2.2 mycroft
59 1.6.2.5 mycroft struct clist sc_q;
60 1.6.2.1 mycroft struct selinfo sc_rsel;
61 1.6.2.1 mycroft u_short sc_iobase; /* I/O port base */
62 1.6.2.1 mycroft u_char sc_state; /* Mouse driver state */
63 1.6.2.1 mycroft #define MMS_OPEN 0x01 /* Device is open */
64 1.6.2.1 mycroft #define MMS_ASLP 0x02 /* Waiting for mouse data */
65 1.6.2.1 mycroft u_char sc_status; /* Mouse button status */
66 1.6.2.1 mycroft int sc_x, sc_y; /* accumulated motion in the X,Y axis */
67 1.1 andrew };
68 1.1 andrew
69 1.6.2.1 mycroft static int mmsprobe __P((struct device *, struct cfdata *, void *));
70 1.6.2.1 mycroft static void mmsforceintr __P((void *));
71 1.6.2.1 mycroft static void mmsattach __P((struct device *, struct device *, void *));
72 1.6.2.1 mycroft static int mmsintr __P((void *));
73 1.1 andrew
74 1.6.2.1 mycroft struct cfdriver mmscd =
75 1.6.2.9 mycroft { NULL, "mms", mmsprobe, mmsattach, DV_TTY, sizeof (struct mms_softc) };
76 1.1 andrew
77 1.6.2.5 mycroft #define MMSUNIT(dev) (minor(dev))
78 1.1 andrew
79 1.6.2.1 mycroft static int
80 1.6.2.1 mycroft mmsprobe(parent, cf, aux)
81 1.6.2.1 mycroft struct device *parent;
82 1.6.2.1 mycroft struct cfdata *cf;
83 1.6.2.1 mycroft void *aux;
84 1.1 andrew {
85 1.6.2.1 mycroft struct isa_attach_args *ia = aux;
86 1.6.2.1 mycroft u_short iobase = ia->ia_iobase;
87 1.1 andrew
88 1.6.2.3 mycroft if (iobase == IOBASEUNK)
89 1.6.2.3 mycroft return 0;
90 1.6.2.3 mycroft
91 1.1 andrew /* Read identification register to see if present */
92 1.6.2.1 mycroft if (inb(iobase + MMS_IDENT) != 0xde)
93 1.6.2.1 mycroft return 0;
94 1.1 andrew
95 1.1 andrew /* Seems it was there; reset */
96 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0x87);
97 1.1 andrew
98 1.6.2.3 mycroft if (ia->ia_irq == IRQUNK) {
99 1.6.2.3 mycroft ia->ia_irq = isa_discoverintr(mmsforceintr, aux);
100 1.6.2.3 mycroft if (ia->ia_irq == IRQNONE)
101 1.6.2.3 mycroft return 0;
102 1.6.2.11 mycroft /* reset again to disable interrupts */
103 1.6.2.11 mycroft outb(iobase + MMS_ADDR, 0x87);
104 1.6.2.3 mycroft }
105 1.1 andrew
106 1.6.2.1 mycroft ia->ia_iosize = MMS_NPORTS;
107 1.6.2.1 mycroft ia->ia_drq = DRQUNK;
108 1.6.2.1 mycroft ia->ia_msize = 0;
109 1.6.2.1 mycroft return 1;
110 1.1 andrew }
111 1.1 andrew
112 1.6.2.1 mycroft static void
113 1.6.2.1 mycroft mmsforceintr(aux)
114 1.6.2.1 mycroft void *aux;
115 1.1 andrew {
116 1.6.2.1 mycroft struct isa_attach_args *ia = aux;
117 1.6.2.1 mycroft u_short iobase = ia->ia_iobase;
118 1.1 andrew
119 1.6.2.1 mycroft /* enable interrupts; expect to get one in 1/60 second */
120 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0x07);
121 1.6.2.1 mycroft outb(iobase + MMS_DATA, 0x09);
122 1.1 andrew }
123 1.1 andrew
124 1.6.2.1 mycroft static void
125 1.6.2.1 mycroft mmsattach(parent, self, aux)
126 1.6.2.1 mycroft struct device *parent, *self;
127 1.6.2.1 mycroft void *aux;
128 1.1 andrew {
129 1.6.2.1 mycroft struct mms_softc *sc = (struct mms_softc *)self;
130 1.6.2.1 mycroft struct isa_attach_args *ia = aux;
131 1.6.2.1 mycroft u_short iobase = ia->ia_iobase;
132 1.6.2.1 mycroft
133 1.6.2.1 mycroft /* other initialization done by mmsprobe */
134 1.6.2.1 mycroft sc->sc_iobase = iobase;
135 1.6.2.1 mycroft sc->sc_state = 0;
136 1.1 andrew
137 1.6.2.2 mycroft printf(": Microsoft mouse\n");
138 1.6.2.3 mycroft isa_establish(&sc->sc_id, &sc->sc_dev);
139 1.6.2.2 mycroft
140 1.6.2.3 mycroft sc->sc_ih.ih_fun = mmsintr;
141 1.6.2.3 mycroft sc->sc_ih.ih_arg = sc;
142 1.6.2.3 mycroft intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
143 1.6.2.1 mycroft }
144 1.1 andrew
145 1.6.2.1 mycroft int
146 1.6.2.1 mycroft mmsopen(dev, flag)
147 1.6.2.1 mycroft dev_t dev;
148 1.6.2.1 mycroft int flag;
149 1.6.2.1 mycroft {
150 1.6.2.1 mycroft int unit = MMSUNIT(dev);
151 1.6.2.1 mycroft struct mms_softc *sc;
152 1.6.2.1 mycroft u_short iobase;
153 1.6.2.1 mycroft
154 1.6.2.1 mycroft if (unit >= mmscd.cd_ndevs)
155 1.6.2.1 mycroft return ENXIO;
156 1.6.2.2 mycroft sc = mmscd.cd_devs[unit];
157 1.6.2.1 mycroft if (!sc)
158 1.6.2.1 mycroft return ENXIO;
159 1.6.2.1 mycroft
160 1.6.2.1 mycroft if (sc->sc_state & MMS_OPEN)
161 1.6.2.1 mycroft return EBUSY;
162 1.6.2.1 mycroft
163 1.6.2.5 mycroft if (clalloc(&sc->sc_q, MMS_BSIZE, 0) == -1)
164 1.6.2.5 mycroft return ENOMEM;
165 1.6.2.5 mycroft
166 1.6.2.1 mycroft sc->sc_state |= MMS_OPEN;
167 1.6.2.1 mycroft sc->sc_status = 0;
168 1.6.2.1 mycroft sc->sc_x = sc->sc_y = 0;
169 1.6.2.1 mycroft
170 1.6.2.1 mycroft /* enable interrupts */
171 1.6.2.1 mycroft iobase = sc->sc_iobase;
172 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0x07);
173 1.6.2.1 mycroft outb(iobase + MMS_DATA, 0x09);
174 1.1 andrew
175 1.6.2.1 mycroft return 0;
176 1.6.2.1 mycroft }
177 1.1 andrew
178 1.6.2.1 mycroft int
179 1.6.2.1 mycroft mmsclose(dev, flag)
180 1.6.2.1 mycroft dev_t dev;
181 1.6.2.1 mycroft int flag;
182 1.6.2.1 mycroft {
183 1.6.2.1 mycroft int unit = MMSUNIT(dev);
184 1.6.2.2 mycroft struct mms_softc *sc = mmscd.cd_devs[unit];
185 1.1 andrew
186 1.6.2.1 mycroft /* disable interrupts */
187 1.6.2.1 mycroft outb(sc->sc_iobase + MMS_ADDR, 0x87);
188 1.1 andrew
189 1.6.2.1 mycroft sc->sc_state &= ~MMS_OPEN;
190 1.1 andrew
191 1.6.2.5 mycroft clfree(&sc->sc_q);
192 1.6.2.5 mycroft
193 1.6.2.1 mycroft return 0;
194 1.1 andrew }
195 1.1 andrew
196 1.6.2.1 mycroft int
197 1.6.2.1 mycroft mmsread(dev, uio, flag)
198 1.6.2.1 mycroft dev_t dev;
199 1.6.2.1 mycroft struct uio *uio;
200 1.6.2.1 mycroft int flag;
201 1.1 andrew {
202 1.6.2.1 mycroft int unit = MMSUNIT(dev);
203 1.6.2.2 mycroft struct mms_softc *sc = mmscd.cd_devs[unit];
204 1.6.2.1 mycroft int s;
205 1.6.2.1 mycroft int error;
206 1.6.2.1 mycroft size_t length;
207 1.6.2.1 mycroft u_char buffer[MMS_CHUNK];
208 1.1 andrew
209 1.1 andrew /* Block until mouse activity occured */
210 1.1 andrew
211 1.1 andrew s = spltty();
212 1.6.2.5 mycroft while (sc->sc_q.c_cc == 0) {
213 1.6.2.5 mycroft if (flag & IO_NDELAY) {
214 1.1 andrew splx(s);
215 1.6.2.1 mycroft return EWOULDBLOCK;
216 1.1 andrew }
217 1.6.2.1 mycroft sc->sc_state |= MMS_ASLP;
218 1.6.2.1 mycroft if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0)) {
219 1.6.2.1 mycroft sc->sc_state &= ~MMS_ASLP;
220 1.1 andrew splx(s);
221 1.6.2.1 mycroft return error;
222 1.1 andrew }
223 1.1 andrew }
224 1.6.2.10 mycroft splx(s);
225 1.1 andrew
226 1.1 andrew /* Transfer as many chunks as possible */
227 1.1 andrew
228 1.6.2.5 mycroft while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
229 1.6.2.5 mycroft length = min(sc->sc_q.c_cc, uio->uio_resid);
230 1.1 andrew if (length > sizeof(buffer))
231 1.1 andrew length = sizeof(buffer);
232 1.1 andrew
233 1.6.2.5 mycroft /* remove a small chunk from input queue */
234 1.6.2.5 mycroft (void) q_to_b(&sc->sc_q, buffer, length);
235 1.1 andrew
236 1.6.2.5 mycroft /* copy data to user process */
237 1.6.2.1 mycroft if (error = uiomove(buffer, length, uio))
238 1.1 andrew break;
239 1.1 andrew }
240 1.1 andrew
241 1.6.2.1 mycroft return error;
242 1.1 andrew }
243 1.1 andrew
244 1.6.2.1 mycroft int
245 1.6.2.1 mycroft mmsioctl(dev, cmd, addr, flag)
246 1.6.2.1 mycroft dev_t dev;
247 1.6.2.1 mycroft int cmd;
248 1.6.2.1 mycroft caddr_t addr;
249 1.6.2.1 mycroft int flag;
250 1.1 andrew {
251 1.6.2.1 mycroft int unit = MMSUNIT(dev);
252 1.6.2.2 mycroft struct mms_softc *sc = mmscd.cd_devs[unit];
253 1.6.2.1 mycroft struct mouseinfo info;
254 1.6.2.1 mycroft int s;
255 1.6.2.1 mycroft int error;
256 1.1 andrew
257 1.1 andrew switch (cmd) {
258 1.6.2.1 mycroft case MOUSEIOCREAD:
259 1.1 andrew s = spltty();
260 1.1 andrew
261 1.6.2.1 mycroft info.status = sc->sc_status;
262 1.6.2.1 mycroft if (sc->sc_x || sc->sc_y)
263 1.1 andrew info.status |= MOVEMENT;
264 1.1 andrew
265 1.6.2.1 mycroft if (sc->sc_x > 127)
266 1.1 andrew info.xmotion = 127;
267 1.6.2.1 mycroft else if (sc->sc_x < -127)
268 1.6.2.1 mycroft /* bounding at -127 avoids a bug in XFree86 */
269 1.6.2.1 mycroft info.xmotion = -127;
270 1.1 andrew else
271 1.6.2.1 mycroft info.xmotion = sc->sc_x;
272 1.1 andrew
273 1.6.2.1 mycroft if (sc->sc_y > 127)
274 1.1 andrew info.ymotion = 127;
275 1.6.2.1 mycroft else if (sc->sc_y < -127)
276 1.6.2.1 mycroft info.ymotion = -127;
277 1.1 andrew else
278 1.6.2.1 mycroft info.ymotion = sc->sc_y;
279 1.1 andrew
280 1.6.2.5 mycroft /* reset historical information */
281 1.6.2.1 mycroft sc->sc_x = 0;
282 1.6.2.1 mycroft sc->sc_y = 0;
283 1.6.2.1 mycroft sc->sc_status &= ~BUTCHNGMASK;
284 1.6.2.5 mycroft flushq(&sc->sc_q);
285 1.1 andrew
286 1.1 andrew splx(s);
287 1.1 andrew error = copyout(&info, addr, sizeof(struct mouseinfo));
288 1.1 andrew break;
289 1.1 andrew
290 1.6.2.1 mycroft default:
291 1.1 andrew error = EINVAL;
292 1.1 andrew break;
293 1.6.2.1 mycroft }
294 1.1 andrew
295 1.6.2.1 mycroft return error;
296 1.1 andrew }
297 1.1 andrew
298 1.6.2.1 mycroft int
299 1.6.2.1 mycroft mmsintr(arg)
300 1.6.2.1 mycroft void *arg;
301 1.1 andrew {
302 1.6.2.1 mycroft struct mms_softc *sc = (struct mms_softc *)arg;
303 1.6.2.1 mycroft u_short iobase = sc->sc_iobase;
304 1.6.2.1 mycroft u_char buttons, changed, status;
305 1.6.2.1 mycroft char dx, dy;
306 1.6.2.5 mycroft u_char buffer[5];
307 1.6.2.1 mycroft
308 1.6.2.1 mycroft if ((sc->sc_state & MMS_OPEN) == 0)
309 1.6.2.1 mycroft /* interrupts not expected */
310 1.6.2.1 mycroft return 0;
311 1.1 andrew
312 1.1 andrew /* Freeze InPort registers (disabling interrupts) */
313 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0x07);
314 1.6.2.1 mycroft outb(iobase + MMS_DATA, 0x29);
315 1.1 andrew
316 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0);
317 1.6.2.1 mycroft status = inb(iobase + MMS_DATA);
318 1.1 andrew
319 1.1 andrew if (status & 0x40) {
320 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 1);
321 1.6.2.1 mycroft dx = inb(iobase + MMS_DATA);
322 1.6.2.1 mycroft dx = (dx == -128) ? -127 : dx;
323 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 2);
324 1.6.2.1 mycroft dy = inb(iobase + MMS_DATA);
325 1.1 andrew dy = (dy == -128) ? 127 : -dy;
326 1.6.2.1 mycroft } else
327 1.1 andrew dx = dy = 0;
328 1.1 andrew
329 1.1 andrew /* Unfreeze InPort Registers (re-enables interrupts) */
330 1.6.2.1 mycroft outb(iobase + MMS_ADDR, 0x07);
331 1.6.2.1 mycroft outb(iobase + MMS_DATA, 0x09);
332 1.1 andrew
333 1.6.2.1 mycroft buttons = status & BUTSTATMASK;
334 1.6.2.1 mycroft changed = status & BUTCHNGMASK;
335 1.6.2.1 mycroft sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
336 1.6.2.1 mycroft
337 1.6.2.1 mycroft if (dx || dy || changed) {
338 1.6.2.5 mycroft /* update accumulated movements */
339 1.6.2.1 mycroft sc->sc_x += dx;
340 1.6.2.1 mycroft sc->sc_y += dy;
341 1.6.2.1 mycroft
342 1.6.2.5 mycroft buffer[0] = 0x80 | (~status & BUTSTATMASK);
343 1.6.2.5 mycroft buffer[1] = dx;
344 1.6.2.5 mycroft buffer[2] = dy;
345 1.6.2.5 mycroft buffer[3] = buffer[4] = 0;
346 1.6.2.5 mycroft (void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
347 1.1 andrew
348 1.6.2.1 mycroft if (sc->sc_state & MMS_ASLP) {
349 1.6.2.1 mycroft sc->sc_state &= ~MMS_ASLP;
350 1.5 andrew wakeup((caddr_t)sc);
351 1.1 andrew }
352 1.6.2.1 mycroft selwakeup(&sc->sc_rsel);
353 1.6.2.1 mycroft }
354 1.6.2.1 mycroft
355 1.6.2.1 mycroft return 1;
356 1.1 andrew }
357 1.1 andrew
358 1.6.2.1 mycroft int
359 1.6.2.1 mycroft mmsselect(dev, rw, p)
360 1.6.2.1 mycroft dev_t dev;
361 1.6.2.1 mycroft int rw;
362 1.6.2.1 mycroft struct proc *p;
363 1.1 andrew {
364 1.6.2.1 mycroft int unit = MMSUNIT(dev);
365 1.6.2.2 mycroft struct mms_softc *sc = mmscd.cd_devs[unit];
366 1.6.2.1 mycroft int s;
367 1.6.2.1 mycroft int ret;
368 1.1 andrew
369 1.1 andrew if (rw == FWRITE)
370 1.6.2.1 mycroft return 0;
371 1.1 andrew
372 1.1 andrew s = spltty();
373 1.6.2.5 mycroft if (sc->sc_q.c_cc)
374 1.1 andrew ret = 1;
375 1.1 andrew else {
376 1.6.2.1 mycroft selrecord(p, &sc->sc_rsel);
377 1.1 andrew ret = 0;
378 1.1 andrew }
379 1.1 andrew splx(s);
380 1.1 andrew
381 1.6.2.1 mycroft return ret;
382 1.1 andrew }
383