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