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