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