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