wsmouse.c revision 1.15 1 /* $NetBSD: wsmouse.c,v 1.15 2001/10/13 15:56:16 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 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: wsmouse.c,v 1.15 2001/10/13 15:56:16 augustss Exp $");
35
36 /*
37 * Copyright (c) 1992, 1993
38 * The Regents of the University of California. All rights reserved.
39 *
40 * This software was developed by the Computer Systems Engineering group
41 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
42 * contributed to Berkeley.
43 *
44 * All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Lawrence Berkeley Laboratory.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. All advertising materials mentioning features or use of this software
58 * must display the following acknowledgement:
59 * This product includes software developed by the University of
60 * California, Berkeley and its contributors.
61 * 4. Neither the name of the University nor the names of its contributors
62 * may be used to endorse or promote products derived from this software
63 * without specific prior written permission.
64 *
65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 * SUCH DAMAGE.
76 *
77 * @(#)ms.c 8.1 (Berkeley) 6/11/93
78 */
79
80 /*
81 * Mouse driver.
82 */
83
84 #include <sys/param.h>
85 #include <sys/conf.h>
86 #include <sys/ioctl.h>
87 #include <sys/fcntl.h>
88 #include <sys/kernel.h>
89 #include <sys/proc.h>
90 #include <sys/syslog.h>
91 #include <sys/systm.h>
92 #include <sys/tty.h>
93 #include <sys/signalvar.h>
94 #include <sys/device.h>
95 #include <sys/vnode.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 #include "wsmux.h"
103 #include "wsdisplay.h"
104 #include "wskbd.h"
105
106 #if NWSMUX > 0
107 #include <dev/wscons/wsmuxvar.h>
108 #endif
109
110 #define INVALID_X INT_MAX
111 #define INVALID_Y INT_MAX
112 #define INVALID_Z INT_MAX
113
114 struct wsmouse_softc {
115 struct device sc_dv;
116
117 const struct wsmouse_accessops *sc_accessops;
118 void *sc_accesscookie;
119
120 int sc_ready; /* accepting events */
121 struct wseventvar sc_events; /* event queue state */
122
123 u_int sc_mb; /* mouse button state */
124 u_int sc_ub; /* user button state */
125 int sc_dx; /* delta-x */
126 int sc_dy; /* delta-y */
127 int sc_dz; /* delta-z */
128 int sc_x; /* absolute-x */
129 int sc_y; /* absolute-y */
130 int sc_z; /* absolute-z */
131
132 int sc_refcnt;
133 u_char sc_dying; /* device is being detached */
134
135 #if NWSMUX > 0
136 struct wsmux_softc *sc_mux;
137 #endif
138 };
139
140 int wsmouse_match(struct device *, struct cfdata *, void *);
141 void wsmouse_attach(struct device *, struct device *, void *);
142 int wsmouse_detach(struct device *, int);
143 int wsmouse_activate(struct device *, enum devact);
144
145 int wsmouse_do_ioctl(struct wsmouse_softc *, u_long, caddr_t,
146 int, struct proc *);
147
148 int wsmousedoclose(struct device *, int, int, struct proc *);
149 int wsmousedoioctl(struct device *, u_long, caddr_t, int, struct proc *);
150
151 struct cfattach wsmouse_ca = {
152 sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
153 wsmouse_detach, wsmouse_activate
154 };
155
156 #if NWSMOUSE > 0
157 extern struct cfdriver wsmouse_cd;
158 #endif /* NWSMOUSE > 0 */
159
160 cdev_decl(wsmouse);
161
162 #if NWSMUX > 0
163 struct wsmuxops wsmouse_muxops = {
164 wsmouseopen, wsmousedoclose, wsmousedoioctl, 0, 0
165 };
166 #endif
167
168 /*
169 * Print function (for parent devices).
170 */
171 int
172 wsmousedevprint(void *aux, const char *pnp)
173 {
174
175 if (pnp)
176 printf("wsmouse at %s", pnp);
177 return (UNCONF);
178 }
179
180 int
181 wsmouse_match(struct device *parent, struct cfdata *match, void *aux)
182 {
183 return (1);
184 }
185
186 void
187 wsmouse_attach(struct device *parent, struct device *self, void *aux)
188 {
189 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
190 struct wsmousedev_attach_args *ap = aux;
191 #if NWSMUX > 0
192 int mux;
193 #endif
194
195 sc->sc_accessops = ap->accessops;
196 sc->sc_accesscookie = ap->accesscookie;
197 sc->sc_ready = 0; /* sanity */
198
199 #if NWSMUX > 0
200 mux = sc->sc_dv.dv_cfdata->wsmousedevcf_mux;
201 if (mux != WSMOUSEDEVCF_MUX_DEFAULT) {
202 wsmux_attach(mux, WSMUX_MOUSE, &sc->sc_dv, &sc->sc_events,
203 &sc->sc_mux, &wsmouse_muxops);
204 printf(" mux %d", mux);
205 }
206 #endif
207
208 printf("\n");
209 }
210
211 int
212 wsmouse_activate(struct device *self, enum devact act)
213 {
214 /* XXX should we do something more? */
215 return (0);
216 }
217
218 /*
219 * Detach a mouse. To keep track of users of the softc we keep
220 * a reference count that's incremented while inside, e.g., read.
221 * If the mouse is active and the reference count is > 0 (0 is the
222 * normal state) we post an event and then wait for the process
223 * that had the reference to wake us up again. Then we blow away the
224 * vnode and return (which will deallocate the softc).
225 */
226 int
227 wsmouse_detach(struct device *self, int flags)
228 {
229 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
230 struct wseventvar *evar;
231 int maj, mn;
232 int s;
233 #if NWSMUX > 0
234 int mux;
235 #endif
236
237 sc->sc_dying = 1;
238
239 #if NWSMUX > 0
240 mux = sc->sc_dv.dv_cfdata->wsmousedevcf_mux;
241 if (mux != WSMOUSEDEVCF_MUX_DEFAULT)
242 wsmux_detach(mux, &sc->sc_dv);
243 #endif
244
245 evar = &sc->sc_events;
246 if (evar->io) {
247 s = spltty();
248 if (--sc->sc_refcnt >= 0) {
249 /* Wake everyone by generating a dummy event. */
250 if (++evar->put >= WSEVENT_QSIZE)
251 evar->put = 0;
252 WSEVENT_WAKEUP(evar);
253 /* Wait for processes to go away. */
254 if (tsleep(sc, PZERO, "wsmdet", hz * 60))
255 printf("wsmouse_detach: %s didn't detach\n",
256 sc->sc_dv.dv_xname);
257 }
258 splx(s);
259 }
260
261 /* locate the major number */
262 for (maj = 0; maj < nchrdev; maj++)
263 if (cdevsw[maj].d_open == wsmouseopen)
264 break;
265
266 /* Nuke the vnodes for any open instances (calls close). */
267 mn = self->dv_unit;
268 vdevgone(maj, mn, mn, VCHR);
269
270 return (0);
271 }
272
273 void
274 wsmouse_input(struct device *wsmousedev, u_int btns /* 0 is up */,
275 int x, int y, int z, u_int flags)
276 {
277 struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
278 struct wscons_event *ev;
279 struct wseventvar *evar;
280 int mb, ub, d, get, put, any;
281
282 /*
283 * Discard input if not ready.
284 */
285 if (sc->sc_ready == 0)
286 return;
287
288 #if NWSMUX > 0
289 if (sc->sc_mux)
290 evar = &sc->sc_mux->sc_events;
291 else
292 #endif
293 evar = &sc->sc_events;
294
295 sc->sc_mb = btns;
296 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_X))
297 sc->sc_dx += x;
298 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Y))
299 sc->sc_dy += y;
300 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Z))
301 sc->sc_dz += z;
302
303 /*
304 * We have at least one event (mouse button, delta-X, or
305 * delta-Y; possibly all three, and possibly three separate
306 * button events). Deliver these events until we are out
307 * of changes or out of room. As events get delivered,
308 * mark them `unchanged'.
309 */
310 ub = sc->sc_ub;
311 any = 0;
312 get = evar->get;
313 put = evar->put;
314 ev = &evar->q[put];
315
316 /* NEXT prepares to put the next event, backing off if necessary */
317 #define NEXT \
318 if ((++put) % WSEVENT_QSIZE == get) { \
319 put--; \
320 goto out; \
321 }
322 /* ADVANCE completes the `put' of the event */
323 #define ADVANCE \
324 ev++; \
325 if (put >= WSEVENT_QSIZE) { \
326 put = 0; \
327 ev = &evar->q[0]; \
328 } \
329 any = 1
330 /* TIMESTAMP sets `time' field of the event to the current time */
331 #define TIMESTAMP \
332 do { \
333 int s; \
334 s = splhigh(); \
335 TIMEVAL_TO_TIMESPEC(&time, &ev->time); \
336 splx(s); \
337 } while (0)
338
339 if (flags & WSMOUSE_INPUT_ABSOLUTE_X) {
340 if (sc->sc_x != x) {
341 NEXT;
342 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_X;
343 ev->value = x;
344 TIMESTAMP;
345 ADVANCE;
346 sc->sc_x = x;
347 }
348 } else {
349 if (sc->sc_dx) {
350 NEXT;
351 ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
352 ev->value = sc->sc_dx;
353 TIMESTAMP;
354 ADVANCE;
355 sc->sc_dx = 0;
356 }
357 }
358 if (flags & WSMOUSE_INPUT_ABSOLUTE_Y) {
359 if (sc->sc_y != y) {
360 NEXT;
361 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Y;
362 ev->value = y;
363 TIMESTAMP;
364 ADVANCE;
365 sc->sc_y = y;
366 }
367 } else {
368 if (sc->sc_dy) {
369 NEXT;
370 ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
371 ev->value = sc->sc_dy;
372 TIMESTAMP;
373 ADVANCE;
374 sc->sc_dy = 0;
375 }
376 }
377 if (flags & WSMOUSE_INPUT_ABSOLUTE_Z) {
378 if (sc->sc_z != z) {
379 NEXT;
380 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Z;
381 ev->value = z;
382 TIMESTAMP;
383 ADVANCE;
384 sc->sc_z = z;
385 }
386 } else {
387 if (sc->sc_dz) {
388 NEXT;
389 ev->type = WSCONS_EVENT_MOUSE_DELTA_Z;
390 ev->value = sc->sc_dz;
391 TIMESTAMP;
392 ADVANCE;
393 sc->sc_dz = 0;
394 }
395 }
396
397 mb = sc->sc_mb;
398 while ((d = mb ^ ub) != 0) {
399 /*
400 * Mouse button change. Find the first change and drop
401 * it into the event queue.
402 */
403 NEXT;
404 ev->value = ffs(d) - 1;
405
406 KASSERT(ev->value >= 0);
407
408 d = 1 << ev->value;
409 ev->type =
410 (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
411 TIMESTAMP;
412 ADVANCE;
413 ub ^= d;
414 }
415 out:
416 if (any) {
417 sc->sc_ub = ub;
418 evar->put = put;
419 WSEVENT_WAKEUP(evar);
420 }
421 }
422
423 int
424 wsmouseopen(dev_t dev, int flags, int mode, struct proc *p)
425 {
426 #if NWSMOUSE > 0
427 struct wsmouse_softc *sc;
428 int error, unit;
429
430 unit = minor(dev);
431 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
432 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
433 return (ENXIO);
434
435 if (sc->sc_dying)
436 return (EIO);
437
438 if ((flags & (FREAD | FWRITE)) == FWRITE)
439 return (0); /* always allow open for write
440 so ioctl() is possible. */
441
442 #if NWSMUX > 0
443 if (sc->sc_mux != NULL) {
444 /* Grab the mouse out of the greedy hands of the mux. */
445 error = wsmouse_rem_mux(unit, sc->sc_mux);
446 if (error)
447 return (error);
448 }
449 #endif
450
451 if (sc->sc_events.io) /* and that it's not in use */
452 return (EBUSY);
453
454 sc->sc_events.io = p;
455 wsevent_init(&sc->sc_events); /* may cause sleep */
456
457 sc->sc_ready = 1; /* start accepting events */
458 sc->sc_x = INVALID_X;
459 sc->sc_y = INVALID_Y;
460 sc->sc_z = INVALID_Z;
461
462 /* enable the device, and punt if that's not possible */
463 error = (*sc->sc_accessops->enable)(sc->sc_accesscookie);
464 if (error) {
465 sc->sc_ready = 0; /* stop accepting events */
466 wsevent_fini(&sc->sc_events);
467 sc->sc_events.io = NULL;
468 return (error);
469 }
470
471 return (0);
472 #else
473 return (ENXIO);
474 #endif /* NWSMOUSE > 0 */
475 }
476
477 int
478 wsmouseclose(dev_t dev, int flags, int mode, struct proc *p)
479 {
480 #if NWSMOUSE > 0
481 return (wsmousedoclose(wsmouse_cd.cd_devs[minor(dev)],
482 flags, mode, p));
483 #else
484 return (ENXIO);
485 #endif /* NWSMOUSE > 0 */
486 }
487
488 #if NWSMOUSE > 0
489 int
490 wsmousedoclose(struct device *dv, int flags, int mode, struct proc *p)
491 {
492 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
493
494 if ((flags & (FREAD | FWRITE)) == FWRITE)
495 return (0); /* see wsmouseopen() */
496
497 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
498
499 sc->sc_ready = 0; /* stop accepting events */
500 wsevent_fini(&sc->sc_events);
501 sc->sc_events.io = NULL;
502 return (0);
503 }
504 #endif /* NWSMOUSE > 0 */
505
506 int
507 wsmouseread(dev_t dev, struct uio *uio, int flags)
508 {
509 #if NWSMOUSE > 0
510 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
511 int error;
512
513 if (sc->sc_dying)
514 return (EIO);
515
516 sc->sc_refcnt++;
517 error = wsevent_read(&sc->sc_events, uio, flags);
518 if (--sc->sc_refcnt < 0) {
519 wakeup(sc);
520 error = EIO;
521 }
522 return (error);
523 #else
524 return (ENXIO);
525 #endif /* NWSMOUSE > 0 */
526 }
527
528 int
529 wsmouseioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
530 {
531 #if NWSMOUSE > 0
532 return (wsmousedoioctl(wsmouse_cd.cd_devs[minor(dev)],
533 cmd, data, flag, p));
534 #else
535 return (ENXIO);
536 #endif /* NWSMOUSE > 0 */
537 }
538
539 #if NWSMOUSE > 0
540 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
541 int
542 wsmousedoioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
543 struct proc *p)
544 {
545 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
546 int error;
547
548 sc->sc_refcnt++;
549 error = wsmouse_do_ioctl(sc, cmd, data, flag, p);
550 if (--sc->sc_refcnt < 0)
551 wakeup(sc);
552 return (error);
553 }
554
555 int
556 wsmouse_do_ioctl(struct wsmouse_softc *sc, u_long cmd, caddr_t data,
557 int flag, struct proc *p)
558 {
559 int error;
560
561 if (sc->sc_dying)
562 return (EIO);
563
564 /*
565 * Try the generic ioctls that the wsmouse interface supports.
566 */
567 switch (cmd) {
568 case FIONBIO: /* we will remove this someday (soon???) */
569 return (0);
570
571 case FIOASYNC:
572 sc->sc_events.async = *(int *)data != 0;
573 return (0);
574
575 case TIOCSPGRP:
576 if (*(int *)data != sc->sc_events.io->p_pgid)
577 return (EPERM);
578 return (0);
579 }
580
581 /*
582 * Try the mouse driver for WSMOUSEIO ioctls. It returns -1
583 * if it didn't recognize the request.
584 */
585 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
586 data, flag, p);
587 return (error != -1 ? error : ENOTTY);
588 }
589 #endif /* NWSMOUSE > 0 */
590
591 int
592 wsmousepoll(dev_t dev, int events, struct proc *p)
593 {
594 #if NWSMOUSE > 0
595 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
596
597 return (wsevent_poll(&sc->sc_events, events, p));
598 #else
599 return (0);
600 #endif /* NWSMOUSE > 0 */
601 }
602
603 #if NWSMUX > 0
604 int
605 wsmouse_add_mux(int unit, struct wsmux_softc *muxsc)
606 {
607 struct wsmouse_softc *sc;
608
609 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
610 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
611 return (ENXIO);
612
613 if (sc->sc_mux || sc->sc_events.io)
614 return (EBUSY);
615
616 return (wsmux_attach_sc(muxsc, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
617 &sc->sc_mux, &wsmouse_muxops));
618 }
619
620 int
621 wsmouse_rem_mux(int unit, struct wsmux_softc *muxsc)
622 {
623 struct wsmouse_softc *sc;
624
625 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
626 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
627 return (ENXIO);
628
629 return (wsmux_detach_sc(muxsc, &sc->sc_dv));
630 }
631
632 #endif
633