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