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