lms.c revision 1.22 1 /* $NetBSD: lms.c,v 1.22 1996/03/17 01:31:14 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994 Charles Hannum.
5 * Copyright (c) 1992, 1993 Erik Forsberg.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include <sys/param.h>
27 #include <sys/kernel.h>
28 #include <sys/systm.h>
29 #include <sys/buf.h>
30 #include <sys/malloc.h>
31 #include <sys/ioctl.h>
32 #include <sys/tty.h>
33 #include <sys/file.h>
34 #include <sys/select.h>
35 #include <sys/proc.h>
36 #include <sys/vnode.h>
37 #include <sys/device.h>
38
39 #include <machine/cpu.h>
40 #include <machine/pio.h>
41 #include <machine/mouse.h>
42
43 #include <dev/isa/isavar.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 void *sc_ih;
58
59 struct clist sc_q;
60 struct selinfo sc_rsel;
61 int sc_iobase; /* I/O port base */
62 u_char sc_state; /* mouse driver state */
63 #define LMS_OPEN 0x01 /* device is open */
64 #define LMS_ASLP 0x02 /* waiting for mouse data */
65 u_char sc_status; /* mouse button status */
66 int sc_x, sc_y; /* accumulated motion in the X,Y axis */
67 };
68
69 int lmsprobe __P((struct device *, void *, void *));
70 void lmsattach __P((struct device *, struct device *, void *));
71 int lmsintr __P((void *));
72
73 struct cfattach lms_ca = {
74 sizeof(struct lms_softc), lmsprobe, lmsattach
75 };
76
77 struct cfdriver lms_cd = {
78 NULL, "lms", DV_TTY
79 };
80
81 #define LMSUNIT(dev) (minor(dev))
82
83 int
84 lmsprobe(parent, match, aux)
85 struct device *parent;
86 void *match, *aux;
87 {
88 struct isa_attach_args *ia = aux;
89 int iobase = ia->ia_iobase;
90
91 /* Configure and check for port present. */
92 outb(iobase + LMS_CONFIG, 0x91);
93 delay(10);
94 outb(iobase + LMS_SIGN, 0x0c);
95 delay(10);
96 if (inb(iobase + LMS_SIGN) != 0x0c)
97 return 0;
98 outb(iobase + LMS_SIGN, 0x50);
99 delay(10);
100 if (inb(iobase + LMS_SIGN) != 0x50)
101 return 0;
102
103 /* Disable interrupts. */
104 outb(iobase + LMS_CNTRL, 0x10);
105
106 ia->ia_iosize = LMS_NPORTS;
107 ia->ia_msize = 0;
108 return 1;
109 }
110
111 void
112 lmsattach(parent, self, aux)
113 struct device *parent, *self;
114 void *aux;
115 {
116 struct lms_softc *sc = (void *)self;
117 struct isa_attach_args *ia = aux;
118 int iobase = ia->ia_iobase;
119
120 printf("\n");
121
122 /* Other initialization was done by lmsprobe. */
123 sc->sc_iobase = iobase;
124 sc->sc_state = 0;
125
126 sc->sc_ih = isa_intr_establish(ia->ia_irq, IST_PULSE, IPL_TTY, lmsintr,
127 sc);
128 }
129
130 int
131 lmsopen(dev, flag)
132 dev_t dev;
133 int flag;
134 {
135 int unit = LMSUNIT(dev);
136 struct lms_softc *sc;
137
138 if (unit >= lms_cd.cd_ndevs)
139 return ENXIO;
140 sc = lms_cd.cd_devs[unit];
141 if (!sc)
142 return ENXIO;
143
144 if (sc->sc_state & LMS_OPEN)
145 return EBUSY;
146
147 if (clalloc(&sc->sc_q, LMS_BSIZE, 0) == -1)
148 return ENOMEM;
149
150 sc->sc_state |= LMS_OPEN;
151 sc->sc_status = 0;
152 sc->sc_x = sc->sc_y = 0;
153
154 /* Enable interrupts. */
155 outb(sc->sc_iobase + LMS_CNTRL, 0);
156
157 return 0;
158 }
159
160 int
161 lmsclose(dev, flag)
162 dev_t dev;
163 int flag;
164 {
165 struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
166
167 /* Disable interrupts. */
168 outb(sc->sc_iobase + LMS_CNTRL, 0x10);
169
170 sc->sc_state &= ~LMS_OPEN;
171
172 clfree(&sc->sc_q);
173
174 return 0;
175 }
176
177 int
178 lmsread(dev, uio, flag)
179 dev_t dev;
180 struct uio *uio;
181 int flag;
182 {
183 struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
184 int s;
185 int error;
186 size_t length;
187 u_char buffer[LMS_CHUNK];
188
189 /* Block until mouse activity occured. */
190
191 s = spltty();
192 while (sc->sc_q.c_cc == 0) {
193 if (flag & IO_NDELAY) {
194 splx(s);
195 return EWOULDBLOCK;
196 }
197 sc->sc_state |= LMS_ASLP;
198 if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "lmsrea", 0)) {
199 sc->sc_state &= ~LMS_ASLP;
200 splx(s);
201 return error;
202 }
203 }
204 splx(s);
205
206 /* Transfer as many chunks as possible. */
207
208 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
209 length = min(sc->sc_q.c_cc, uio->uio_resid);
210 if (length > sizeof(buffer))
211 length = sizeof(buffer);
212
213 /* Remove a small chunk from the input queue. */
214 (void) q_to_b(&sc->sc_q, buffer, length);
215
216 /* Copy the data to the user process. */
217 if (error = uiomove(buffer, length, uio))
218 break;
219 }
220
221 return error;
222 }
223
224 int
225 lmsioctl(dev, cmd, addr, flag)
226 dev_t dev;
227 u_long cmd;
228 caddr_t addr;
229 int flag;
230 {
231 struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
232 struct mouseinfo info;
233 int s;
234 int error;
235
236 switch (cmd) {
237 case MOUSEIOCREAD:
238 s = spltty();
239
240 info.status = sc->sc_status;
241 if (sc->sc_x || sc->sc_y)
242 info.status |= MOVEMENT;
243
244 if (sc->sc_x > 127)
245 info.xmotion = 127;
246 else if (sc->sc_x < -127)
247 /* Bounding at -127 avoids a bug in XFree86. */
248 info.xmotion = -127;
249 else
250 info.xmotion = sc->sc_x;
251
252 if (sc->sc_y > 127)
253 info.ymotion = 127;
254 else if (sc->sc_y < -127)
255 info.ymotion = -127;
256 else
257 info.ymotion = sc->sc_y;
258
259 /* Reset historical information. */
260 sc->sc_x = sc->sc_y = 0;
261 sc->sc_status &= ~BUTCHNGMASK;
262 ndflush(&sc->sc_q, sc->sc_q.c_cc);
263
264 splx(s);
265 error = copyout(&info, addr, sizeof(struct mouseinfo));
266 break;
267
268 default:
269 error = EINVAL;
270 break;
271 }
272
273 return error;
274 }
275
276 int
277 lmsintr(arg)
278 void *arg;
279 {
280 struct lms_softc *sc = arg;
281 int iobase = sc->sc_iobase;
282 u_char hi, lo, buttons, changed;
283 char dx, dy;
284 u_char buffer[5];
285
286 if ((sc->sc_state & LMS_OPEN) == 0)
287 /* Interrupts are not expected. */
288 return 0;
289
290 outb(iobase + LMS_CNTRL, 0xab);
291 hi = inb(iobase + LMS_DATA);
292 outb(iobase + LMS_CNTRL, 0x90);
293 lo = inb(iobase + LMS_DATA);
294 dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
295 /* Bounding at -127 avoids a bug in XFree86. */
296 dx = (dx == -128) ? -127 : dx;
297
298 outb(iobase + LMS_CNTRL, 0xf0);
299 hi = inb(iobase + LMS_DATA);
300 outb(iobase + LMS_CNTRL, 0xd0);
301 lo = inb(iobase + LMS_DATA);
302 dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
303 dy = (dy == -128) ? 127 : -dy;
304
305 outb(iobase + LMS_CNTRL, 0);
306
307 buttons = (~hi >> 5) & 0x07;
308 changed = ((buttons ^ sc->sc_status) & 0x07) << 3;
309 sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
310
311 if (dx || dy || changed) {
312 /* Update accumulated movements. */
313 sc->sc_x += dx;
314 sc->sc_y += dy;
315
316 /* Add this event to the queue. */
317 buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
318 buffer[1] = dx;
319 buffer[2] = dy;
320 buffer[3] = buffer[4] = 0;
321 (void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
322
323 if (sc->sc_state & LMS_ASLP) {
324 sc->sc_state &= ~LMS_ASLP;
325 wakeup((caddr_t)sc);
326 }
327 selwakeup(&sc->sc_rsel);
328 }
329
330 return -1;
331 }
332
333 int
334 lmsselect(dev, rw, p)
335 dev_t dev;
336 int rw;
337 struct proc *p;
338 {
339 struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
340 int s;
341 int ret;
342
343 if (rw == FWRITE)
344 return 0;
345
346 s = spltty();
347 if (!sc->sc_q.c_cc) {
348 selrecord(p, &sc->sc_rsel);
349 ret = 0;
350 } else
351 ret = 1;
352 splx(s);
353
354 return ret;
355 }
356