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