lms.c revision 1.31 1 /* $NetBSD: lms.c,v 1.31 1997/10/19 19:00:24 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 #include <sys/poll.h>
39
40 #include <machine/cpu.h>
41 #include <machine/bus.h>
42 #include <machine/intr.h>
43 #include <machine/mouse.h>
44 #include <machine/conf.h>
45
46 #include <dev/isa/isavar.h>
47
48 #define LMS_DATA 0 /* offset for data port, read-only */
49 #define LMS_SIGN 1 /* offset for signature port, read-write */
50 #define LMS_INTR 2 /* offset for interrupt port, read-only */
51 #define LMS_CNTRL 2 /* offset for control port, write-only */
52 #define LMS_CONFIG 3 /* for configuration port, read-write */
53 #define LMS_NPORTS 4
54
55 #define LMS_CHUNK 128 /* chunk size for read */
56 #define LMS_BSIZE 1020 /* buffer size */
57
58 struct lms_softc { /* driver status information */
59 struct device sc_dev;
60 void *sc_ih;
61
62 bus_space_tag_t sc_iot; /* bus i/o space identifier */
63 bus_space_handle_t sc_ioh; /* bus i/o handle */
64
65 struct clist sc_q;
66 struct selinfo sc_rsel;
67 u_char sc_state; /* mouse driver state */
68 #define LMS_OPEN 0x01 /* device is open */
69 #define LMS_ASLP 0x02 /* waiting for mouse data */
70 u_char sc_status; /* mouse button status */
71 int sc_x, sc_y; /* accumulated motion in the X,Y axis */
72 };
73
74 int lmsprobe __P((struct device *, void *, void *));
75 void lmsattach __P((struct device *, struct device *, void *));
76 int lmsintr __P((void *));
77
78 struct cfattach lms_ca = {
79 sizeof(struct lms_softc), lmsprobe, lmsattach
80 };
81
82 struct cfdriver lms_cd = {
83 NULL, "lms", DV_TTY
84 };
85
86 #define LMSUNIT(dev) (minor(dev))
87
88 int
89 lmsprobe(parent, match, aux)
90 struct device *parent;
91 void *match, *aux;
92 {
93 struct isa_attach_args *ia = aux;
94 bus_space_tag_t iot = ia->ia_iot;
95 bus_space_handle_t ioh;
96 int rv;
97
98 /* Disallow wildcarded i/o base. */
99 if (ia->ia_iobase == IOBASEUNK)
100 return 0;
101
102 /* Map the i/o space. */
103 if (bus_space_map(iot, ia->ia_iobase, LMS_NPORTS, 0, &ioh))
104 return 0;
105
106 rv = 0;
107
108 /* Configure and check for port present. */
109 bus_space_write_1(iot, ioh, LMS_CONFIG, 0x91);
110 delay(10);
111 bus_space_write_1(iot, ioh, LMS_SIGN, 0x0c);
112 delay(10);
113 if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x0c)
114 goto out;
115 bus_space_write_1(iot, ioh, LMS_SIGN, 0x50);
116 delay(10);
117 if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x50)
118 goto out;
119
120 /* Disable interrupts. */
121 bus_space_write_1(iot, ioh, LMS_CNTRL, 0x10);
122
123 rv = 1;
124 ia->ia_iosize = LMS_NPORTS;
125 ia->ia_msize = 0;
126
127 out:
128 bus_space_unmap(iot, ioh, LMS_NPORTS);
129 return rv;
130 }
131
132 void
133 lmsattach(parent, self, aux)
134 struct device *parent, *self;
135 void *aux;
136 {
137 struct lms_softc *sc = (void *)self;
138 struct isa_attach_args *ia = aux;
139
140 printf("\n");
141
142 /* Other initialization was done by lmsprobe. */
143 sc->sc_iot = ia->ia_iot;
144 if (bus_space_map(sc->sc_iot, ia->ia_iobase, LMS_NPORTS, 0,
145 &sc->sc_ioh))
146 panic("lmsattach: couldn't map I/O ports");
147
148 sc->sc_state = 0;
149
150 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
151 IPL_TTY, lmsintr, sc);
152 }
153
154 int
155 lmsopen(dev, flag, mode, p)
156 dev_t dev;
157 int flag;
158 int mode;
159 struct proc *p;
160 {
161 int unit = LMSUNIT(dev);
162 struct lms_softc *sc;
163
164 if (unit >= lms_cd.cd_ndevs)
165 return ENXIO;
166 sc = lms_cd.cd_devs[unit];
167 if (!sc)
168 return ENXIO;
169
170 if (sc->sc_state & LMS_OPEN)
171 return EBUSY;
172
173 if (clalloc(&sc->sc_q, LMS_BSIZE, 0) == -1)
174 return ENOMEM;
175
176 sc->sc_state |= LMS_OPEN;
177 sc->sc_status = 0;
178 sc->sc_x = sc->sc_y = 0;
179
180 /* Enable interrupts. */
181 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0);
182
183 return 0;
184 }
185
186 int
187 lmsclose(dev, flag, mode, p)
188 dev_t dev;
189 int flag;
190 int mode;
191 struct proc *p;
192 {
193 struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
194
195 /* Disable interrupts. */
196 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0x10);
197
198 sc->sc_state &= ~LMS_OPEN;
199
200 clfree(&sc->sc_q);
201
202 return 0;
203 }
204
205 int
206 lmsread(dev, uio, flag)
207 dev_t dev;
208 struct uio *uio;
209 int flag;
210 {
211 struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
212 int s;
213 int error = 0;
214 size_t length;
215 u_char buffer[LMS_CHUNK];
216
217 /* Block until mouse activity occured. */
218
219 s = spltty();
220 while (sc->sc_q.c_cc == 0) {
221 if (flag & IO_NDELAY) {
222 splx(s);
223 return EWOULDBLOCK;
224 }
225 sc->sc_state |= LMS_ASLP;
226 error = tsleep((caddr_t)sc, PZERO | PCATCH, "lmsrea", 0);
227 if (error) {
228 sc->sc_state &= ~LMS_ASLP;
229 splx(s);
230 return error;
231 }
232 }
233 splx(s);
234
235 /* Transfer as many chunks as possible. */
236
237 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
238 length = min(sc->sc_q.c_cc, uio->uio_resid);
239 if (length > sizeof(buffer))
240 length = sizeof(buffer);
241
242 /* Remove a small chunk from the input queue. */
243 (void) q_to_b(&sc->sc_q, buffer, length);
244
245 /* Copy the data to the user process. */
246 if ((error = uiomove(buffer, length, uio)) != 0)
247 break;
248 }
249
250 return error;
251 }
252
253 int
254 lmsioctl(dev, cmd, addr, flag, p)
255 dev_t dev;
256 u_long cmd;
257 caddr_t addr;
258 int flag;
259 struct proc *p;
260 {
261 struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
262 struct mouseinfo info;
263 int s;
264 int error;
265
266 switch (cmd) {
267 case MOUSEIOCREAD:
268 s = spltty();
269
270 info.status = sc->sc_status;
271 if (sc->sc_x || sc->sc_y)
272 info.status |= MOVEMENT;
273
274 if (sc->sc_x > 127)
275 info.xmotion = 127;
276 else if (sc->sc_x < -127)
277 /* Bounding at -127 avoids a bug in XFree86. */
278 info.xmotion = -127;
279 else
280 info.xmotion = sc->sc_x;
281
282 if (sc->sc_y > 127)
283 info.ymotion = 127;
284 else if (sc->sc_y < -127)
285 info.ymotion = -127;
286 else
287 info.ymotion = sc->sc_y;
288
289 /* Reset historical information. */
290 sc->sc_x = sc->sc_y = 0;
291 sc->sc_status &= ~BUTCHNGMASK;
292 ndflush(&sc->sc_q, sc->sc_q.c_cc);
293
294 splx(s);
295 error = copyout(&info, addr, sizeof(struct mouseinfo));
296 break;
297
298 default:
299 error = EINVAL;
300 break;
301 }
302
303 return error;
304 }
305
306 int
307 lmsintr(arg)
308 void *arg;
309 {
310 struct lms_softc *sc = arg;
311 bus_space_tag_t iot = sc->sc_iot;
312 bus_space_handle_t ioh = sc->sc_ioh;
313 u_char hi, lo, buttons, changed;
314 char dx, dy;
315 u_char buffer[5];
316
317 if ((sc->sc_state & LMS_OPEN) == 0)
318 /* Interrupts are not expected. */
319 return 0;
320
321 bus_space_write_1(iot, ioh, LMS_CNTRL, 0xab);
322 hi = bus_space_read_1(iot, ioh, LMS_DATA);
323 bus_space_write_1(iot, ioh, LMS_CNTRL, 0x90);
324 lo = bus_space_read_1(iot, ioh, LMS_DATA);
325 dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
326 /* Bounding at -127 avoids a bug in XFree86. */
327 dx = (dx == -128) ? -127 : dx;
328
329 bus_space_write_1(iot, ioh, LMS_CNTRL, 0xf0);
330 hi = bus_space_read_1(iot, ioh, LMS_DATA);
331 bus_space_write_1(iot, ioh, LMS_CNTRL, 0xd0);
332 lo = bus_space_read_1(iot, ioh, LMS_DATA);
333 dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
334 dy = (dy == -128) ? 127 : -dy;
335
336 bus_space_write_1(iot, ioh, LMS_CNTRL, 0);
337
338 buttons = (~hi >> 5) & 0x07;
339 changed = ((buttons ^ sc->sc_status) & 0x07) << 3;
340 sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
341
342 if (dx || dy || changed) {
343 /* Update accumulated movements. */
344 sc->sc_x += dx;
345 sc->sc_y += dy;
346
347 /* Add this event to the queue. */
348 buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
349 buffer[1] = dx;
350 buffer[2] = dy;
351 buffer[3] = buffer[4] = 0;
352 (void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
353
354 if (sc->sc_state & LMS_ASLP) {
355 sc->sc_state &= ~LMS_ASLP;
356 wakeup((caddr_t)sc);
357 }
358 selwakeup(&sc->sc_rsel);
359 }
360
361 return -1;
362 }
363
364 int
365 lmspoll(dev, events, p)
366 dev_t dev;
367 int events;
368 struct proc *p;
369 {
370 struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
371 int revents = 0;
372 int s = spltty();
373
374 if (events & (POLLIN | POLLRDNORM))
375 if (sc->sc_q.c_cc > 0)
376 revents |= events & (POLLIN | POLLRDNORM);
377 else
378 selrecord(p, &sc->sc_rsel);
379
380 splx(s);
381 return (revents);
382 }
383