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