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