mms.c revision 1.26 1 /* $NetBSD: mms.c,v 1.26 1996/10/11 00:27:08 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994 Charles Hannum.
5 * Copyright (c) 1992, 1993 Erik Forsberg.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 #include <sys/poll.h>
39
40 #include <machine/cpu.h>
41 #include <machine/intr.h>
42 #include <machine/pio.h>
43 #include <machine/mouse.h>
44 #include <machine/conf.h>
45
46 #include <dev/isa/isavar.h>
47
48 #define MMS_ADDR 0 /* offset for register select */
49 #define MMS_DATA 1 /* offset for InPort data */
50 #define MMS_IDENT 2 /* offset for identification register */
51 #define MMS_NPORTS 4
52
53 #define MMS_CHUNK 128 /* chunk size for read */
54 #define MMS_BSIZE 1020 /* buffer size */
55
56 struct mms_softc { /* driver status information */
57 struct device sc_dev;
58 void *sc_ih;
59
60 struct clist sc_q;
61 struct selinfo sc_rsel;
62 int sc_iobase; /* I/O port base */
63 u_char sc_state; /* mouse driver state */
64 #define MMS_OPEN 0x01 /* device is open */
65 #define MMS_ASLP 0x02 /* waiting for mouse data */
66 u_char sc_status; /* mouse button status */
67 int sc_x, sc_y; /* accumulated motion in the X,Y axis */
68 };
69
70 int mmsprobe __P((struct device *, void *, void *));
71 void mmsattach __P((struct device *, struct device *, void *));
72 int mmsintr __P((void *));
73
74 struct cfattach mms_ca = {
75 sizeof(struct mms_softc), mmsprobe, mmsattach
76 };
77
78 struct cfdriver mms_cd = {
79 NULL, "mms", DV_TTY
80 };
81
82 #define MMSUNIT(dev) (minor(dev))
83
84 int
85 mmsprobe(parent, match, aux)
86 struct device *parent;
87 void *match, *aux;
88 {
89 struct isa_attach_args *ia = aux;
90 int iobase = ia->ia_iobase;
91
92 /* Read identification register to see if present */
93 if (inb(iobase + MMS_IDENT) != 0xde)
94 return 0;
95
96 /* Seems it was there; reset. */
97 outb(iobase + MMS_ADDR, 0x87);
98
99 ia->ia_iosize = MMS_NPORTS;
100 ia->ia_msize = 0;
101 return 1;
102 }
103
104 void
105 mmsattach(parent, self, aux)
106 struct device *parent, *self;
107 void *aux;
108 {
109 struct mms_softc *sc = (void *)self;
110 struct isa_attach_args *ia = aux;
111 int iobase = ia->ia_iobase;
112
113 kprintf("\n");
114
115 /* Other initialization was done by mmsprobe. */
116 sc->sc_iobase = iobase;
117 sc->sc_state = 0;
118
119 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
120 IPL_TTY, mmsintr, sc);
121 }
122
123 int
124 mmsopen(dev, flag, mode, p)
125 dev_t dev;
126 int flag;
127 int mode;
128 struct proc *p;
129 {
130 int unit = MMSUNIT(dev);
131 struct mms_softc *sc;
132
133 if (unit >= mms_cd.cd_ndevs)
134 return ENXIO;
135 sc = mms_cd.cd_devs[unit];
136 if (!sc)
137 return ENXIO;
138
139 if (sc->sc_state & MMS_OPEN)
140 return EBUSY;
141
142 if (clalloc(&sc->sc_q, MMS_BSIZE, 0) == -1)
143 return ENOMEM;
144
145 sc->sc_state |= MMS_OPEN;
146 sc->sc_status = 0;
147 sc->sc_x = sc->sc_y = 0;
148
149 /* Enable interrupts. */
150 outb(sc->sc_iobase + MMS_ADDR, 0x07);
151 outb(sc->sc_iobase + MMS_DATA, 0x09);
152
153 return 0;
154 }
155
156 int
157 mmsclose(dev, flag, mode, p)
158 dev_t dev;
159 int flag;
160 int mode;
161 struct proc *p;
162 {
163 struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
164
165 /* Disable interrupts. */
166 outb(sc->sc_iobase + MMS_ADDR, 0x87);
167
168 sc->sc_state &= ~MMS_OPEN;
169
170 clfree(&sc->sc_q);
171
172 return 0;
173 }
174
175 int
176 mmsread(dev, uio, flag)
177 dev_t dev;
178 struct uio *uio;
179 int flag;
180 {
181 struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
182 int s;
183 int error = 0;
184 size_t length;
185 u_char buffer[MMS_CHUNK];
186
187 /* Block until mouse activity occured. */
188
189 s = spltty();
190 while (sc->sc_q.c_cc == 0) {
191 if (flag & IO_NDELAY) {
192 splx(s);
193 return EWOULDBLOCK;
194 }
195 sc->sc_state |= MMS_ASLP;
196 error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0);
197 if (error) {
198 sc->sc_state &= ~MMS_ASLP;
199 splx(s);
200 return error;
201 }
202 }
203 splx(s);
204
205 /* Transfer as many chunks as possible. */
206
207 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
208 length = min(sc->sc_q.c_cc, uio->uio_resid);
209 if (length > sizeof(buffer))
210 length = sizeof(buffer);
211
212 /* Remove a small chunk from the input queue. */
213 (void) q_to_b(&sc->sc_q, buffer, length);
214
215 /* Copy the data to the user process. */
216 if ((error = uiomove(buffer, length, uio)) != 0)
217 break;
218 }
219
220 return error;
221 }
222
223 int
224 mmsioctl(dev, cmd, addr, flag, p)
225 dev_t dev;
226 u_long cmd;
227 caddr_t addr;
228 int flag;
229 struct proc *p;
230 {
231 struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
232 struct mouseinfo info;
233 int s;
234 int error;
235
236 switch (cmd) {
237 case MOUSEIOCREAD:
238 s = spltty();
239
240 info.status = sc->sc_status;
241 if (sc->sc_x || sc->sc_y)
242 info.status |= MOVEMENT;
243
244 if (sc->sc_x > 127)
245 info.xmotion = 127;
246 else if (sc->sc_x < -127)
247 /* Bounding at -127 avoids a bug in XFree86. */
248 info.xmotion = -127;
249 else
250 info.xmotion = sc->sc_x;
251
252 if (sc->sc_y > 127)
253 info.ymotion = 127;
254 else if (sc->sc_y < -127)
255 info.ymotion = -127;
256 else
257 info.ymotion = sc->sc_y;
258
259 /* Reset historical information. */
260 sc->sc_x = sc->sc_y = 0;
261 sc->sc_status &= ~BUTCHNGMASK;
262 ndflush(&sc->sc_q, sc->sc_q.c_cc);
263
264 splx(s);
265 error = copyout(&info, addr, sizeof(struct mouseinfo));
266 break;
267
268 default:
269 error = EINVAL;
270 break;
271 }
272
273 return error;
274 }
275
276 int
277 mmsintr(arg)
278 void *arg;
279 {
280 struct mms_softc *sc = arg;
281 int iobase = sc->sc_iobase;
282 u_char buttons, changed, status;
283 char dx, dy;
284 u_char buffer[5];
285
286 if ((sc->sc_state & MMS_OPEN) == 0)
287 /* Interrupts are not expected. */
288 return 0;
289
290 /* Freeze InPort registers (disabling interrupts). */
291 outb(iobase + MMS_ADDR, 0x07);
292 outb(iobase + MMS_DATA, 0x29);
293
294 outb(iobase + MMS_ADDR, 0x00);
295 status = inb(iobase + MMS_DATA);
296
297 if (status & 0x40) {
298 outb(iobase + MMS_ADDR, 1);
299 dx = inb(iobase + MMS_DATA);
300 dx = (dx == -128) ? -127 : dx;
301 outb(iobase + MMS_ADDR, 2);
302 dy = inb(iobase + MMS_DATA);
303 dy = (dy == -128) ? 127 : -dy;
304 } else
305 dx = dy = 0;
306
307 /* Unfreeze InPort registers (reenabling interrupts). */
308 outb(iobase + MMS_ADDR, 0x07);
309 outb(iobase + MMS_DATA, 0x09);
310
311 buttons = status & BUTSTATMASK;
312 changed = status & BUTCHNGMASK;
313 sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
314
315 if (dx || dy || changed) {
316 /* Update accumulated movements. */
317 sc->sc_x += dx;
318 sc->sc_y += dy;
319
320 /* Add this event to the queue. */
321 buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
322 buffer[1] = dx;
323 buffer[2] = dy;
324 buffer[3] = buffer[4] = 0;
325 (void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
326
327 if (sc->sc_state & MMS_ASLP) {
328 sc->sc_state &= ~MMS_ASLP;
329 wakeup((caddr_t)sc);
330 }
331 selwakeup(&sc->sc_rsel);
332 }
333
334 return -1;
335 }
336
337 int
338 mmspoll(dev, events, p)
339 dev_t dev;
340 int events;
341 struct proc *p;
342 {
343 struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
344 int revents = 0;
345 int s = spltty();
346
347 if (events & (POLLIN | POLLRDNORM))
348 if (sc->sc_q.c_cc > 0)
349 revents |= events & (POLLIN | POLLRDNORM);
350 else
351 selrecord(p, &sc->sc_rsel);
352
353 splx(s);
354 return (revents);
355 }
356