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