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