wsmouse.c revision 1.3 1 /* $NetBSD: wsmouse.c,v 1.3 1998/07/25 20:02:21 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 static const char _copyright[] __attribute__ ((unused)) =
34 "Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.";
35 static const char _rcsid[] __attribute__ ((unused)) =
36 "$NetBSD: wsmouse.c,v 1.3 1998/07/25 20:02:21 augustss Exp $";
37
38 /*
39 * Copyright (c) 1992, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * This software was developed by the Computer Systems Engineering group
43 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
44 * contributed to Berkeley.
45 *
46 * All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Lawrence Berkeley Laboratory.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
53 * are met:
54 * 1. Redistributions of source code must retain the above copyright
55 * notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
59 * 3. All advertising materials mentioning features or use of this software
60 * must display the following acknowledgement:
61 * This product includes software developed by the University of
62 * California, Berkeley and its contributors.
63 * 4. Neither the name of the University nor the names of its contributors
64 * may be used to endorse or promote products derived from this software
65 * without specific prior written permission.
66 *
67 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 * SUCH DAMAGE.
78 *
79 * @(#)ms.c 8.1 (Berkeley) 6/11/93
80 */
81
82 /*
83 * Mouse driver.
84 */
85
86 #include <sys/param.h>
87 #include <sys/conf.h>
88 #include <sys/ioctl.h>
89 #include <sys/kernel.h>
90 #include <sys/proc.h>
91 #include <sys/syslog.h>
92 #include <sys/systm.h>
93 #include <sys/tty.h>
94 #include <sys/signalvar.h>
95 #include <sys/device.h>
96
97 #include <dev/wscons/wsconsio.h>
98 #include <dev/wscons/wsmousevar.h>
99 #include <dev/wscons/wseventvar.h>
100
101 #include "wsmouse.h"
102
103 struct wsmouse_softc {
104 struct device sc_dv;
105
106 const struct wsmouse_accessops *sc_accessops;
107 void *sc_accesscookie;
108
109 int sc_ready; /* accepting events */
110 struct wseventvar sc_events; /* event queue state */
111
112 u_int sc_mb; /* mouse button state */
113 u_int sc_ub; /* user button state */
114 int sc_dx; /* delta-x */
115 int sc_dy; /* delta-y */
116 };
117
118 int wsmouse_match __P((struct device *, struct cfdata *, void *));
119 void wsmouse_attach __P((struct device *, struct device *, void *));
120
121 struct cfattach wsmouse_ca = {
122 sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
123 };
124
125 #if NWSMOUSE > 0
126 extern struct cfdriver wsmouse_cd;
127 #endif /* NWSMOUSE > 0 */
128
129 cdev_decl(wsmouse);
130
131 /*
132 * Print function (for parent devices).
133 */
134 int
135 wsmousedevprint(aux, pnp)
136 void *aux;
137 const char *pnp;
138 {
139
140 if (pnp)
141 printf("wsmouse at %s", pnp);
142 return (UNCONF);
143 }
144
145 int
146 wsmouse_match(parent, match, aux)
147 struct device *parent;
148 struct cfdata *match;
149 void *aux;
150 {
151
152 return (1);
153 }
154
155 void
156 wsmouse_attach(parent, self, aux)
157 struct device *parent, *self;
158 void *aux;
159 {
160 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
161 struct wsmousedev_attach_args *ap = aux;
162
163 printf("\n");
164
165 sc->sc_accessops = ap->accessops;
166 sc->sc_accesscookie = ap->accesscookie;
167 sc->sc_ready = 0; /* sanity */
168 }
169
170 void
171 wsmouse_input(wsmousedev, btns, dx, dy)
172 struct device *wsmousedev;
173 u_int btns; /* 0 is up */
174 int dx, dy;
175 {
176 struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
177 struct wscons_event *ev;
178 int mb, ub, d, get, put, any;
179
180 /*
181 * Discard input if not ready.
182 */
183 if (sc->sc_ready == 0)
184 return;
185
186 sc->sc_mb = btns;
187 sc->sc_dx += dx;
188 sc->sc_dy += dy;
189
190 /*
191 * We have at least one event (mouse button, delta-X, or
192 * delta-Y; possibly all three, and possibly three separate
193 * button events). Deliver these events until we are out
194 * of changes or out of room. As events get delivered,
195 * mark them `unchanged'.
196 */
197 any = 0;
198 get = sc->sc_events.get;
199 put = sc->sc_events.put;
200 ev = &sc->sc_events.q[put];
201
202 /* NEXT prepares to put the next event, backing off if necessary */
203 #define NEXT \
204 if ((++put) % WSEVENT_QSIZE == get) { \
205 put--; \
206 goto out; \
207 }
208 /* ADVANCE completes the `put' of the event */
209 #define ADVANCE \
210 ev++; \
211 if (put >= WSEVENT_QSIZE) { \
212 put = 0; \
213 ev = &sc->sc_events.q[0]; \
214 } \
215 any = 1
216 /* TIMESTAMP sets `time' field of the event to the current time */
217 #define TIMESTAMP \
218 do { \
219 int s; \
220 s = splhigh(); \
221 TIMEVAL_TO_TIMESPEC(&time, &ev->time); \
222 splx(s); \
223 } while (0)
224
225 mb = sc->sc_mb;
226 ub = sc->sc_ub;
227 while ((d = mb ^ ub) != 0) {
228 /*
229 * Mouse button change. Find the first change and drop
230 * it into the event queue.
231 */
232 NEXT;
233 ev->value = ffs(d) - 1;
234
235 KASSERT(ev->value >= 0);
236
237 d = 1 << ev->value;
238 ev->type =
239 (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
240 TIMESTAMP;
241 ADVANCE;
242 ub ^= d;
243 }
244 if (sc->sc_dx) {
245 NEXT;
246 ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
247 ev->value = sc->sc_dx;
248 TIMESTAMP;
249 ADVANCE;
250 sc->sc_dx = 0;
251 }
252 if (sc->sc_dy) {
253 NEXT;
254 ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
255 ev->value = sc->sc_dy;
256 TIMESTAMP;
257 ADVANCE;
258 sc->sc_dy = 0;
259 }
260 out:
261 if (any) {
262 sc->sc_ub = ub;
263 sc->sc_events.put = put;
264 WSEVENT_WAKEUP(&sc->sc_events);
265 }
266 }
267
268 int
269 wsmouseopen(dev, flags, mode, p)
270 dev_t dev;
271 int flags, mode;
272 struct proc *p;
273 {
274 #if NWSMOUSE > 0
275 struct wsmouse_softc *sc;
276 int error, unit;
277
278 unit = minor(dev);
279 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
280 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
281 return (ENXIO);
282
283 if (sc->sc_events.io) /* and that it's not in use */
284 return (EBUSY);
285
286 sc->sc_events.io = p;
287 wsevent_init(&sc->sc_events); /* may cause sleep */
288
289 sc->sc_ready = 1; /* start accepting events */
290
291 /* enable the device, and punt if that's not possible */
292 error = (*sc->sc_accessops->enable)(sc->sc_accesscookie);
293 if (error) {
294 sc->sc_ready = 0; /* stop accepting events */
295 wsevent_fini(&sc->sc_events);
296 sc->sc_events.io = NULL;
297 return (error);
298 }
299
300 return (0);
301 #else
302 return (ENXIO);
303 #endif /* NWSMOUSE > 0 */
304 }
305
306 int
307 wsmouseclose(dev, flags, mode, p)
308 dev_t dev;
309 int flags, mode;
310 struct proc *p;
311 {
312 #if NWSMOUSE > 0
313 struct wsmouse_softc *sc;
314 int unit;
315
316 unit = minor(dev);
317 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
318 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
319 return (ENXIO);
320
321 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
322
323 sc->sc_ready = 0; /* stop accepting events */
324 wsevent_fini(&sc->sc_events);
325 sc->sc_events.io = NULL;
326 return (0);
327 #else
328 return (ENXIO);
329 #endif /* NWSMOUSE > 0 */
330 }
331
332 int
333 wsmouseread(dev, uio, flags)
334 dev_t dev;
335 struct uio *uio;
336 int flags;
337 {
338 #if NWSMOUSE > 0
339 struct wsmouse_softc *sc;
340 int unit;
341
342 unit = minor(dev);
343 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
344 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
345 return (ENXIO);
346
347 return (wsevent_read(&sc->sc_events, uio, flags));
348 #else
349 return (ENXIO);
350 #endif /* NWSMOUSE > 0 */
351 }
352
353 int
354 wsmouseioctl(dev, cmd, data, flag, p)
355 dev_t dev;
356 u_long cmd;
357 caddr_t data;
358 int flag;
359 struct proc *p;
360 {
361 #if NWSMOUSE > 0
362 struct wsmouse_softc *sc;
363 int unit, error;
364
365 unit = minor(dev);
366 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
367 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
368 return (ENXIO);
369
370 /*
371 * Try the generic ioctls that the wsmouse interface supports.
372 */
373 switch (cmd) {
374 case FIONBIO: /* we will remove this someday (soon???) */
375 return (0);
376
377 case FIOASYNC:
378 sc->sc_events.async = *(int *)data != 0;
379 return (0);
380
381 case TIOCSPGRP:
382 if (*(int *)data != sc->sc_events.io->p_pgid)
383 return (EPERM);
384 return (0);
385 }
386
387 /*
388 * Try the mouse driver for WSMOUSEIO ioctls. It returns -1
389 * if it didn't recognize the request.
390 */
391 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
392 data, flag, p);
393 return (error != -1 ? error : ENOTTY);
394 #else
395 return (ENXIO);
396 #endif /* NWSMOUSE > 0 */
397 }
398
399 int
400 wsmousepoll(dev, events, p)
401 dev_t dev;
402 int events;
403 struct proc *p;
404 {
405 #if NWSMOUSE > 0
406 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
407
408 return (wsevent_poll(&sc->sc_events, events, p));
409 #else
410 return (0);
411 #endif /* NWSMOUSE > 0 */
412 }
413