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