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