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