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