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