lms.c revision 1.6.2.3 1 1.1 andrew /*-
2 1.1 andrew * Copyright (c) 1992, 1993 Erik Forsberg.
3 1.1 andrew * All rights reserved.
4 1.1 andrew *
5 1.1 andrew * Redistribution and use in source and binary forms, with or without
6 1.1 andrew * modification, are permitted provided that the following conditions
7 1.1 andrew * are met:
8 1.1 andrew * 1. Redistributions of source code must retain the above copyright
9 1.1 andrew * notice, this list of conditions and the following disclaimer.
10 1.1 andrew *
11 1.1 andrew * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
12 1.1 andrew * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
13 1.1 andrew * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
14 1.1 andrew * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
15 1.1 andrew * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16 1.1 andrew * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
17 1.1 andrew * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
18 1.1 andrew * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19 1.1 andrew * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
20 1.1 andrew * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 1.1 andrew *
22 1.6.2.3 mycroft * $Id: lms.c,v 1.6.2.3 1993/09/29 08:28:18 mycroft Exp $
23 1.1 andrew */
24 1.1 andrew
25 1.1 andrew #include "param.h"
26 1.1 andrew #include "kernel.h"
27 1.1 andrew #include "systm.h"
28 1.1 andrew #include "buf.h"
29 1.1 andrew #include "malloc.h"
30 1.1 andrew #include "ioctl.h"
31 1.1 andrew #include "tty.h"
32 1.1 andrew #include "file.h"
33 1.1 andrew #include "select.h"
34 1.1 andrew #include "proc.h"
35 1.1 andrew #include "vnode.h"
36 1.6.2.1 mycroft #include "sys/device.h"
37 1.1 andrew
38 1.6.2.1 mycroft #include "i386/isa/isavar.h"
39 1.1 andrew #include "i386/include/mouse.h"
40 1.6.2.1 mycroft #include "i386/include/pio.h"
41 1.1 andrew
42 1.6.2.1 mycroft #define LMS_DATA 0 /* Offset for data port, read-only */
43 1.6.2.1 mycroft #define LMS_SIGN 1 /* Offset for signature port, read-write */
44 1.6.2.1 mycroft #define LMS_INTR 2 /* Offset for interrupt port, read-only */
45 1.6.2.1 mycroft #define LMS_CNTRL 2 /* Offset for control port, write-only */
46 1.6.2.1 mycroft #define LMS_CONFIG 3 /* for configuration port, read-write */
47 1.6.2.1 mycroft #define LMS_NPORTS 4
48 1.6.2.1 mycroft
49 1.6.2.1 mycroft #define LMS_CHUNK 128 /* chunk size for read */
50 1.6.2.1 mycroft #define LMS_BSIZE 1024 /* buffer size */
51 1.6.2.1 mycroft
52 1.6.2.1 mycroft struct lms_softc { /* Driver status information */
53 1.6.2.1 mycroft struct ringbuf { /* Input queue */
54 1.6.2.1 mycroft int rb_count, rb_first, rb_last;
55 1.6.2.1 mycroft char rb_data[LMS_BSIZE];
56 1.6.2.1 mycroft } sc_q;
57 1.6.2.1 mycroft struct selinfo sc_rsel;
58 1.6.2.1 mycroft u_short sc_iobase; /* I/O port base */
59 1.6.2.1 mycroft u_char sc_flags; /* Driver flags */
60 1.6.2.3 mycroft #define LMS_NOBLOCK 0x01
61 1.6.2.1 mycroft u_char sc_state; /* Mouse driver state */
62 1.6.2.2 mycroft #define LMS_OPEN 0x01 /* Device is open */
63 1.6.2.2 mycroft #define LMS_ASLP 0x02 /* Waiting for mouse data */
64 1.6.2.1 mycroft u_char sc_status; /* Mouse button status */
65 1.6.2.1 mycroft u_char sc_button; /* Previous mouse button status bits */
66 1.6.2.1 mycroft int sc_x, sc_y; /* accumulated motion in the X,Y axis */
67 1.1 andrew };
68 1.1 andrew
69 1.6.2.1 mycroft static int lmsprobe __P((struct device *, struct cfdata *, void *));
70 1.6.2.1 mycroft static void lmsforceintr __P((void *));
71 1.6.2.1 mycroft static void lmsattach __P((struct device *, struct device *, void *));
72 1.6.2.1 mycroft static int lmsintr __P((void *));
73 1.1 andrew
74 1.6.2.1 mycroft struct cfdriver lmscd =
75 1.6.2.1 mycroft { NULL, "lms", lmsprobe, lmsattach, sizeof (struct lms_softc) };
76 1.1 andrew
77 1.6.2.1 mycroft #define LMSUNIT(dev) (minor(dev) >> 1)
78 1.6.2.1 mycroft #define LMSFLAGS(dev) (minor(dev) & 0x01)
79 1.1 andrew
80 1.6.2.1 mycroft static int
81 1.6.2.1 mycroft lmsprobe(parent, cf, aux)
82 1.6.2.1 mycroft struct device *parent;
83 1.6.2.1 mycroft struct cfdata *cf;
84 1.6.2.1 mycroft void *aux;
85 1.1 andrew {
86 1.6.2.1 mycroft struct isa_attach_args *ia = aux;
87 1.6.2.1 mycroft u_short iobase = ia->ia_iobase;
88 1.1 andrew
89 1.1 andrew /* Configure and check for port present */
90 1.6.2.1 mycroft outb(iobase + LMS_CONFIG, 0x91);
91 1.6.2.1 mycroft delay(10);
92 1.6.2.1 mycroft outb(iobase + LMS_SIGN, 0x0c);
93 1.6.2.1 mycroft delay(10);
94 1.6.2.1 mycroft if (inb(iobase + LMS_SIGN) != 0x0c)
95 1.6.2.1 mycroft return 0;
96 1.6.2.1 mycroft outb(iobase + LMS_SIGN, 0x50);
97 1.6.2.1 mycroft delay(10);
98 1.6.2.1 mycroft if (inb(iobase + LMS_SIGN) != 0x50)
99 1.6.2.1 mycroft return 0;
100 1.6.2.1 mycroft
101 1.6.2.1 mycroft /* XXXX isa_discoverintr */
102 1.6.2.1 mycroft
103 1.6.2.1 mycroft /* disable interrupts */
104 1.6.2.1 mycroft outb(iobase + LMS_CNTRL, 0x10);
105 1.6.2.1 mycroft
106 1.6.2.1 mycroft ia->ia_iosize = LMS_NPORTS;
107 1.6.2.1 mycroft ia->ia_drq = DRQUNK;
108 1.6.2.1 mycroft ia->ia_msize = 0;
109 1.6.2.1 mycroft return 1;
110 1.1 andrew }
111 1.1 andrew
112 1.6.2.1 mycroft static void
113 1.6.2.1 mycroft lmsforceintr(aux)
114 1.6.2.1 mycroft void *aux;
115 1.1 andrew {
116 1.6.2.1 mycroft struct isa_attach_args *ia = aux;
117 1.1 andrew
118 1.6.2.1 mycroft /* enable interrupts; expect to get one in 1/60 second */
119 1.6.2.1 mycroft outb(ia->ia_iobase + LMS_CNTRL, 0);
120 1.1 andrew }
121 1.1 andrew
122 1.6.2.1 mycroft static void
123 1.6.2.1 mycroft lmsattach(parent, self, aux)
124 1.6.2.1 mycroft struct device *parent, *self;
125 1.6.2.1 mycroft void *aux;
126 1.1 andrew {
127 1.6.2.1 mycroft struct lms_softc *sc = (struct lms_softc *)self;
128 1.6.2.1 mycroft struct isa_attach_args *ia = aux;
129 1.6.2.1 mycroft u_short iobase = ia->ia_iobase;
130 1.6.2.1 mycroft
131 1.6.2.1 mycroft /* other initialization done by lmsprobe */
132 1.6.2.1 mycroft sc->sc_iobase = iobase;
133 1.6.2.1 mycroft sc->sc_state = 0;
134 1.1 andrew
135 1.6.2.1 mycroft /* XXXX isa_establishintr */
136 1.1 andrew }
137 1.1 andrew
138 1.6.2.1 mycroft int
139 1.6.2.1 mycroft lmsopen(dev, flag)
140 1.6.2.1 mycroft dev_t dev;
141 1.6.2.1 mycroft int flag;
142 1.1 andrew {
143 1.6.2.1 mycroft int unit = LMSUNIT(dev);
144 1.6.2.1 mycroft struct lms_softc *sc;
145 1.1 andrew
146 1.6.2.1 mycroft if (unit >= lmscd.cd_ndevs)
147 1.6.2.1 mycroft return ENXIO;
148 1.6.2.1 mycroft sc = (struct lms_softc *)lmscd.cd_devs[unit];
149 1.6.2.1 mycroft if (!sc)
150 1.6.2.1 mycroft return ENXIO;
151 1.6.2.1 mycroft
152 1.6.2.2 mycroft if (sc->sc_state & LMS_OPEN)
153 1.6.2.1 mycroft return EBUSY;
154 1.6.2.1 mycroft
155 1.6.2.2 mycroft sc->sc_state |= LMS_OPEN;
156 1.6.2.1 mycroft sc->sc_status = 0;
157 1.6.2.1 mycroft sc->sc_button = 0;
158 1.6.2.1 mycroft sc->sc_x = sc->sc_y = 0;
159 1.6.2.1 mycroft sc->sc_q.rb_count = sc->sc_q.rb_first = sc->sc_q.rb_last = 0;
160 1.1 andrew
161 1.6.2.1 mycroft /* enable interrupts */
162 1.6.2.1 mycroft outb(sc->sc_iobase + LMS_CNTRL, 0);
163 1.1 andrew
164 1.6.2.1 mycroft return 0;
165 1.6.2.1 mycroft }
166 1.1 andrew
167 1.6.2.1 mycroft int
168 1.6.2.1 mycroft lmsclose(dev, flag)
169 1.6.2.1 mycroft dev_t dev;
170 1.6.2.1 mycroft int flag;
171 1.6.2.1 mycroft {
172 1.6.2.1 mycroft int unit = LMSUNIT(dev);
173 1.6.2.1 mycroft struct lms_softc *sc = (struct lms_softc *)lmscd.cd_devs[unit];
174 1.1 andrew
175 1.6.2.1 mycroft /* disable interrupts */
176 1.6.2.1 mycroft outb(sc->sc_iobase + LMS_CNTRL, 0x10);
177 1.1 andrew
178 1.6.2.2 mycroft sc->sc_state &= ~LMS_OPEN;
179 1.1 andrew
180 1.6.2.1 mycroft return 0;
181 1.1 andrew }
182 1.1 andrew
183 1.6.2.1 mycroft int
184 1.6.2.1 mycroft lmsread(dev, uio, flag)
185 1.6.2.1 mycroft dev_t dev;
186 1.6.2.1 mycroft struct uio *uio;
187 1.6.2.1 mycroft int flag;
188 1.1 andrew {
189 1.6.2.1 mycroft int unit = LMSUNIT(dev);
190 1.6.2.1 mycroft struct lms_softc *sc = (struct lms_softc *)lmscd.cd_devs[unit];
191 1.6.2.1 mycroft int s;
192 1.6.2.1 mycroft int error;
193 1.6.2.1 mycroft size_t length;
194 1.6.2.1 mycroft u_char buffer[LMS_CHUNK];
195 1.1 andrew
196 1.1 andrew /* Block until mouse activity occured */
197 1.1 andrew
198 1.1 andrew s = spltty();
199 1.6.2.1 mycroft while (sc->sc_q.rb_count == 0) {
200 1.6.2.3 mycroft if (sc->sc_flags & LMS_NOBLOCK) {
201 1.1 andrew splx(s);
202 1.6.2.1 mycroft return EWOULDBLOCK;
203 1.1 andrew }
204 1.6.2.2 mycroft sc->sc_state |= LMS_ASLP;
205 1.6.2.1 mycroft if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "lmsrea", 0)) {
206 1.6.2.2 mycroft sc->sc_state &= ~LMS_ASLP;
207 1.1 andrew splx(s);
208 1.6.2.1 mycroft return error;
209 1.1 andrew }
210 1.1 andrew }
211 1.1 andrew
212 1.1 andrew /* Transfer as many chunks as possible */
213 1.1 andrew
214 1.6.2.1 mycroft while (sc->sc_q.rb_count > 0 && uio->uio_resid > 0) {
215 1.6.2.1 mycroft length = min(sc->sc_q.rb_count, uio->uio_resid);
216 1.1 andrew if (length > sizeof(buffer))
217 1.1 andrew length = sizeof(buffer);
218 1.1 andrew
219 1.1 andrew /* Remove a small chunk from input queue */
220 1.1 andrew
221 1.6.2.1 mycroft if (sc->sc_q.rb_first + length >= LMS_BSIZE) {
222 1.6.2.1 mycroft size_t left = LMS_BSIZE - sc->sc_q.rb_first;
223 1.6.2.1 mycroft bcopy(&sc->sc_q.rb_data[sc->sc_q.rb_first], buffer,
224 1.6.2.1 mycroft left);
225 1.6.2.1 mycroft bcopy(sc->sc_q.rb_data, &buffer[left], length - left);
226 1.6.2.1 mycroft } else
227 1.6.2.1 mycroft bcopy(&sc->sc_q.rb_data[sc->sc_q.rb_first], buffer,
228 1.6.2.1 mycroft length);
229 1.1 andrew
230 1.6.2.1 mycroft sc->sc_q.rb_first = (sc->sc_q.rb_first + length) % LMS_BSIZE;
231 1.6.2.1 mycroft sc->sc_q.rb_count -= length;
232 1.1 andrew
233 1.1 andrew /* Copy data to user process */
234 1.1 andrew
235 1.6.2.1 mycroft if (error = uiomove(buffer, length, uio))
236 1.1 andrew break;
237 1.1 andrew }
238 1.1 andrew
239 1.6.2.1 mycroft /* reset counters */
240 1.6.2.1 mycroft sc->sc_x = sc->sc_y = 0;
241 1.1 andrew
242 1.1 andrew splx(s);
243 1.6.2.1 mycroft return error;
244 1.1 andrew }
245 1.1 andrew
246 1.6.2.1 mycroft int
247 1.6.2.1 mycroft lmsioctl(dev, cmd, addr, flag)
248 1.6.2.1 mycroft dev_t dev;
249 1.6.2.1 mycroft int cmd;
250 1.6.2.1 mycroft caddr_t addr;
251 1.6.2.1 mycroft int flag;
252 1.1 andrew {
253 1.6.2.1 mycroft int unit = LMSUNIT(dev);
254 1.6.2.1 mycroft struct lms_softc *sc = (struct lms_softc *)lmscd.cd_devs[unit];
255 1.6.2.1 mycroft struct mouseinfo info;
256 1.6.2.1 mycroft int s;
257 1.6.2.1 mycroft int error;
258 1.1 andrew
259 1.1 andrew switch (cmd) {
260 1.6.2.1 mycroft case MOUSEIOCREAD:
261 1.1 andrew s = spltty();
262 1.1 andrew
263 1.6.2.1 mycroft info.status = sc->sc_status;
264 1.6.2.1 mycroft if (sc->sc_x || sc->sc_y)
265 1.1 andrew info.status |= MOVEMENT;
266 1.1 andrew
267 1.6.2.1 mycroft if (sc->sc_x > 127)
268 1.1 andrew info.xmotion = 127;
269 1.6.2.1 mycroft else if (sc->sc_x < -127)
270 1.6.2.1 mycroft /* bounding at -127 avoids a bug in XFree86 */
271 1.3 mycroft info.xmotion = -127;
272 1.1 andrew else
273 1.6.2.1 mycroft info.xmotion = sc->sc_x;
274 1.1 andrew
275 1.6.2.1 mycroft if (sc->sc_y > 127)
276 1.1 andrew info.ymotion = 127;
277 1.6.2.1 mycroft else if (sc->sc_y < -127)
278 1.3 mycroft info.ymotion = -127;
279 1.1 andrew else
280 1.6.2.1 mycroft info.ymotion = sc->sc_y;
281 1.1 andrew
282 1.6.2.1 mycroft sc->sc_x = 0;
283 1.6.2.1 mycroft sc->sc_y = 0;
284 1.6.2.1 mycroft sc->sc_status &= ~BUTCHNGMASK;
285 1.1 andrew
286 1.1 andrew splx(s);
287 1.1 andrew error = copyout(&info, addr, sizeof(struct mouseinfo));
288 1.1 andrew break;
289 1.1 andrew
290 1.6.2.1 mycroft default:
291 1.1 andrew error = EINVAL;
292 1.1 andrew break;
293 1.6.2.1 mycroft }
294 1.1 andrew
295 1.6.2.1 mycroft return error;
296 1.1 andrew }
297 1.1 andrew
298 1.6.2.1 mycroft int
299 1.6.2.1 mycroft lmsintr(arg)
300 1.6.2.1 mycroft void *arg;
301 1.1 andrew {
302 1.6.2.1 mycroft struct lms_softc *sc = (struct lms_softc *)arg;
303 1.6.2.1 mycroft u_short iobase = sc->sc_iobase;
304 1.6.2.1 mycroft u_char hi, lo, buttons, changed;
305 1.6.2.1 mycroft char dx, dy;
306 1.6.2.1 mycroft
307 1.6.2.2 mycroft if ((sc->sc_state & LMS_OPEN) == 0)
308 1.6.2.1 mycroft /* interrupts not expected */
309 1.6.2.1 mycroft return 0;
310 1.6.2.1 mycroft
311 1.6.2.1 mycroft outb(iobase + LMS_CNTRL, 0xab);
312 1.6.2.1 mycroft hi = inb(iobase + LMS_DATA);
313 1.6.2.1 mycroft outb(iobase + LMS_CNTRL, 0x90);
314 1.6.2.1 mycroft lo = inb(iobase + LMS_DATA);
315 1.6.2.1 mycroft dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
316 1.6.2.1 mycroft /* bounding at -127 avoids a bug in XFree86 */
317 1.3 mycroft dx = (dx == -128) ? -127 : dx;
318 1.3 mycroft
319 1.6.2.1 mycroft outb(iobase + LMS_CNTRL, 0xf0);
320 1.6.2.1 mycroft hi = inb(iobase + LMS_DATA);
321 1.6.2.1 mycroft outb(iobase + LMS_CNTRL, 0xd0);
322 1.6.2.1 mycroft lo = inb(iobase + LMS_DATA);
323 1.6.2.1 mycroft dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
324 1.1 andrew dy = (dy == -128) ? 127 : -dy;
325 1.3 mycroft
326 1.6.2.1 mycroft outb(iobase + LMS_CNTRL, 0);
327 1.6.2.1 mycroft
328 1.1 andrew buttons = (~hi >> 5) & 7;
329 1.6.2.1 mycroft changed = buttons ^ sc->sc_button;
330 1.6.2.1 mycroft sc->sc_button = buttons;
331 1.6.2.1 mycroft sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) |
332 1.6.2.1 mycroft (changed << 3);
333 1.6.2.1 mycroft
334 1.6.2.2 mycroft if ((sc->sc_state & LMS_OPEN) && (dx || dy || changed)) {
335 1.6.2.1 mycroft int last = sc->sc_q.rb_last;
336 1.6.2.1 mycroft char *cp = &sc->sc_q.rb_data[last];
337 1.6.2.1 mycroft int count = sc->sc_q.rb_count;
338 1.6.2.1 mycroft
339 1.6.2.1 mycroft /* Update accumulated movements */
340 1.6.2.1 mycroft sc->sc_x += dx;
341 1.6.2.1 mycroft sc->sc_y += dy;
342 1.6.2.1 mycroft
343 1.6.2.1 mycroft if ((count += 5) > LMS_BSIZE)
344 1.6.2.1 mycroft return 1;
345 1.6.2.1 mycroft sc->sc_q.rb_count = count;
346 1.6.2.1 mycroft
347 1.6.2.1 mycroft #define next() \
348 1.6.2.1 mycroft if (++last >= LMS_BSIZE) { \
349 1.6.2.1 mycroft last = 0; \
350 1.6.2.1 mycroft cp = sc->sc_q.rb_data; \
351 1.6.2.1 mycroft } else \
352 1.6.2.1 mycroft cp++;
353 1.6.2.1 mycroft *cp = 0x80 | (buttons ^ BUTSTATMASK);
354 1.6.2.1 mycroft next();
355 1.6.2.1 mycroft *cp = dx;
356 1.6.2.1 mycroft next();
357 1.6.2.1 mycroft *cp = dy;
358 1.6.2.1 mycroft next();
359 1.6.2.1 mycroft *cp = 0;
360 1.6.2.1 mycroft next();
361 1.6.2.1 mycroft *cp = 0;
362 1.6.2.1 mycroft next();
363 1.6.2.1 mycroft sc->sc_q.rb_last = last;
364 1.1 andrew
365 1.6.2.2 mycroft if (sc->sc_state & LMS_ASLP) {
366 1.6.2.2 mycroft sc->sc_state &= ~LMS_ASLP;
367 1.5 andrew wakeup((caddr_t)sc);
368 1.1 andrew }
369 1.6.2.1 mycroft selwakeup(&sc->sc_rsel);
370 1.5 andrew }
371 1.6.2.1 mycroft
372 1.6.2.1 mycroft return 1;
373 1.1 andrew }
374 1.1 andrew
375 1.6.2.1 mycroft int
376 1.6.2.1 mycroft lmsselect(dev, rw, p)
377 1.6.2.1 mycroft dev_t dev;
378 1.6.2.1 mycroft int rw;
379 1.6.2.1 mycroft struct proc *p;
380 1.1 andrew {
381 1.6.2.1 mycroft int unit = LMSUNIT(dev);
382 1.6.2.1 mycroft struct lms_softc *sc = (struct lms_softc *)lmscd.cd_devs[unit];
383 1.6.2.1 mycroft int s;
384 1.6.2.1 mycroft int ret;
385 1.1 andrew
386 1.1 andrew if (rw == FWRITE)
387 1.6.2.1 mycroft return 0;
388 1.1 andrew
389 1.1 andrew s = spltty();
390 1.6.2.1 mycroft if (sc->sc_q.rb_count)
391 1.1 andrew ret = 1;
392 1.1 andrew else {
393 1.6.2.1 mycroft selrecord(p, &sc->sc_rsel);
394 1.1 andrew ret = 0;
395 1.1 andrew }
396 1.1 andrew splx(s);
397 1.1 andrew
398 1.6.2.1 mycroft return ret;
399 1.1 andrew }
400