mms.c revision 1.9 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.9 1994/03/29 04:36:16 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 /* Other initialization was done by mmsprobe. */
106 sc->sc_iobase = iobase;
107 sc->sc_state = 0;
108 }
109
110 int
111 mmsopen(dev, flag)
112 dev_t dev;
113 int flag;
114 {
115 int unit = MMSUNIT(dev);
116 struct mms_softc *sc;
117
118 if (unit >= mmscd.cd_ndevs)
119 return ENXIO;
120 sc = mmscd.cd_devs[unit];
121 if (!sc)
122 return ENXIO;
123
124 if (sc->sc_state & MMS_OPEN)
125 return EBUSY;
126
127 if (clalloc(&sc->sc_q, MMS_BSIZE, 0) == -1)
128 return ENOMEM;
129
130 sc->sc_state |= MMS_OPEN;
131 sc->sc_status = 0;
132 sc->sc_x = sc->sc_y = 0;
133
134 /* Enable interrupts. */
135 outb(sc->sc_iobase + MMS_ADDR, 0x07);
136 outb(sc->sc_iobase + MMS_DATA, 0x09);
137
138 return 0;
139 }
140
141 int
142 mmsclose(dev, flag)
143 dev_t dev;
144 int flag;
145 {
146 struct mms_softc *sc = mmscd.cd_devs[MMSUNIT(dev)];
147
148 /* Disable interrupts. */
149 outb(sc->sc_iobase + MMS_ADDR, 0x87);
150
151 sc->sc_state &= ~MMS_OPEN;
152
153 clfree(&sc->sc_q);
154
155 return 0;
156 }
157
158 int
159 mmsread(dev, uio, flag)
160 dev_t dev;
161 struct uio *uio;
162 int flag;
163 {
164 struct mms_softc *sc = mmscd.cd_devs[MMSUNIT(dev)];
165 int s;
166 int error;
167 size_t length;
168 u_char buffer[MMS_CHUNK];
169
170 /* Block until mouse activity occured. */
171
172 s = spltty();
173 while (sc->sc_q.c_cc == 0) {
174 if (flag & IO_NDELAY) {
175 splx(s);
176 return EWOULDBLOCK;
177 }
178 sc->sc_state |= MMS_ASLP;
179 if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0)) {
180 sc->sc_state &= ~MMS_ASLP;
181 splx(s);
182 return error;
183 }
184 }
185 splx(s);
186
187 /* Transfer as many chunks as possible. */
188
189 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
190 length = min(sc->sc_q.c_cc, uio->uio_resid);
191 if (length > sizeof(buffer))
192 length = sizeof(buffer);
193
194 /* Remove a small chunk from the input queue. */
195 (void) q_to_b(&sc->sc_q, buffer, length);
196
197 /* Copy the data to the user process. */
198 if (error = uiomove(buffer, length, uio))
199 break;
200 }
201
202 return error;
203 }
204
205 int
206 mmsioctl(dev, cmd, addr, flag)
207 dev_t dev;
208 int cmd;
209 caddr_t addr;
210 int flag;
211 {
212 struct mms_softc *sc = mmscd.cd_devs[MMSUNIT(dev)];
213 struct mouseinfo info;
214 int s;
215 int error;
216
217 switch (cmd) {
218 case MOUSEIOCREAD:
219 s = spltty();
220
221 info.status = sc->sc_status;
222 if (sc->sc_x || sc->sc_y)
223 info.status |= MOVEMENT;
224
225 if (sc->sc_x > 127)
226 info.xmotion = 127;
227 else if (sc->sc_x < -127)
228 /* Bounding at -127 avoids a bug in XFree86. */
229 info.xmotion = -127;
230 else
231 info.xmotion = sc->sc_x;
232
233 if (sc->sc_y > 127)
234 info.ymotion = 127;
235 else if (sc->sc_y < -127)
236 info.ymotion = -127;
237 else
238 info.ymotion = sc->sc_y;
239
240 /* Reset historical information. */
241 sc->sc_x = sc->sc_y = 0;
242 sc->sc_status &= ~BUTCHNGMASK;
243 flushq(&sc->sc_q);
244
245 splx(s);
246 error = copyout(&info, addr, sizeof(struct mouseinfo));
247 break;
248
249 default:
250 error = EINVAL;
251 break;
252 }
253
254 return error;
255 }
256
257 int
258 mmsintr(unit)
259 int unit;
260 {
261 struct mms_softc *sc = mmscd.cd_devs[unit];
262 u_short iobase = sc->sc_iobase;
263 u_char buttons, changed, status;
264 char dx, dy;
265 u_char buffer[5];
266
267 if ((sc->sc_state & MMS_OPEN) == 0)
268 /* Interrupts are not expected. */
269 return 0;
270
271 /* Freeze InPort registers (disabling interrupts). */
272 outb(iobase + MMS_ADDR, 0x07);
273 outb(iobase + MMS_DATA, 0x29);
274
275 outb(iobase + MMS_ADDR, 0x00);
276 status = inb(iobase + MMS_DATA);
277
278 if (status & 0x40) {
279 outb(iobase + MMS_ADDR, 1);
280 dx = inb(iobase + MMS_DATA);
281 dx = (dx == -128) ? -127 : dx;
282 outb(iobase + MMS_ADDR, 2);
283 dy = inb(iobase + MMS_DATA);
284 dy = (dy == -128) ? 127 : -dy;
285 } else
286 dx = dy = 0;
287
288 /* Unfreeze InPort registers (reenabling interrupts). */
289 outb(iobase + MMS_ADDR, 0x07);
290 outb(iobase + MMS_DATA, 0x09);
291
292 buttons = status & BUTSTATMASK;
293 changed = status & BUTCHNGMASK;
294 sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
295
296 if (dx || dy || changed) {
297 /* Update accumulated movements. */
298 sc->sc_x += dx;
299 sc->sc_y += dy;
300
301 /* Add this event to the queue. */
302 buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
303 buffer[1] = dx;
304 buffer[2] = dy;
305 buffer[3] = buffer[4] = 0;
306 (void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
307
308 if (sc->sc_state & MMS_ASLP) {
309 sc->sc_state &= ~MMS_ASLP;
310 wakeup((caddr_t)sc);
311 }
312 selwakeup(&sc->sc_rsel);
313 }
314
315 return 1;
316 }
317
318 int
319 mmsselect(dev, rw, p)
320 dev_t dev;
321 int rw;
322 struct proc *p;
323 {
324 struct mms_softc *sc = mmscd.cd_devs[MMSUNIT(dev)];
325 int s;
326 int ret;
327
328 if (rw == FWRITE)
329 return 0;
330
331 s = spltty();
332 if (!sc->sc_q.c_cc) {
333 selrecord(p, &sc->sc_rsel);
334 ret = 0;
335 } else
336 ret = 1;
337 splx(s);
338
339 return ret;
340 }
341