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