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