wsmouse.c revision 1.16.2.1 1 /* $NetBSD: wsmouse.c,v 1.16.2.1 2001/11/12 21:18:41 thorpej 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.16.2.1 2001/11/12 21:18:41 thorpej 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 "wsmouse.h"
85 #include "wsdisplay.h"
86 #include "wsmux.h"
87
88 #include <sys/param.h>
89 #include <sys/conf.h>
90 #include <sys/ioctl.h>
91 #include <sys/fcntl.h>
92 #include <sys/kernel.h>
93 #include <sys/proc.h>
94 #include <sys/syslog.h>
95 #include <sys/systm.h>
96 #include <sys/tty.h>
97 #include <sys/signalvar.h>
98 #include <sys/device.h>
99 #include <sys/vnode.h>
100
101 #include <dev/wscons/wsconsio.h>
102 #include <dev/wscons/wsmousevar.h>
103 #include <dev/wscons/wseventvar.h>
104 #include <dev/wscons/wsmuxvar.h>
105
106 #if defined(WSMUX_DEBUG) && NWSMUX > 0
107 #define DPRINTF(x) if (wsmuxdebug) printf x
108 #define DPRINTFN(n,x) if (wsmuxdebug > (n)) printf x
109 extern int wsmuxdebug;
110 #else
111 #define DPRINTF(x)
112 #define DPRINTFN(n,x)
113 #endif
114
115 #define INVALID_X INT_MAX
116 #define INVALID_Y INT_MAX
117 #define INVALID_Z INT_MAX
118
119 struct wsmouse_softc {
120 struct wsevsrc sc_base;
121
122 const struct wsmouse_accessops *sc_accessops;
123 void *sc_accesscookie;
124
125 u_int sc_mb; /* mouse button state */
126 u_int sc_ub; /* user button state */
127 int sc_dx; /* delta-x */
128 int sc_dy; /* delta-y */
129 int sc_dz; /* delta-z */
130 int sc_x; /* absolute-x */
131 int sc_y; /* absolute-y */
132 int sc_z; /* absolute-z */
133
134 int sc_refcnt;
135 u_char sc_dying; /* device is being detached */
136 };
137
138 static int wsmouse_match(struct device *, struct cfdata *, void *);
139 static void wsmouse_attach(struct device *, struct device *, void *);
140 static int wsmouse_detach(struct device *, int);
141 static int wsmouse_activate(struct device *, enum devact);
142
143 static int wsmouse_do_ioctl(struct wsmouse_softc *, u_long, caddr_t,
144 int, struct proc *);
145
146 #if NWSMUX > 0
147 static int wsmouse_mux_open(struct wsevsrc *, struct wseventvar *);
148 static int wsmouse_mux_close(struct wsevsrc *);
149 #endif
150
151 static int wsmousedoioctl(struct device *, u_long, caddr_t, int, struct proc *);
152
153 static int wsmousedoopen(struct wsmouse_softc *, struct wseventvar *);
154
155 struct cfattach wsmouse_ca = {
156 sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
157 wsmouse_detach, wsmouse_activate
158 };
159
160 #if NWSMOUSE > 0
161 extern struct cfdriver wsmouse_cd;
162 #endif /* NWSMOUSE > 0 */
163
164 cdev_decl(wsmouse);
165
166 #if NWSMUX > 0
167 struct wssrcops wsmouse_srcops = {
168 WSMUX_MOUSE,
169 wsmouse_mux_open, wsmouse_mux_close, wsmousedoioctl, NULL, NULL
170 };
171 #endif
172
173 /*
174 * Print function (for parent devices).
175 */
176 int
177 wsmousedevprint(void *aux, const char *pnp)
178 {
179
180 if (pnp)
181 printf("wsmouse at %s", pnp);
182 return (UNCONF);
183 }
184
185 int
186 wsmouse_match(struct device *parent, struct cfdata *match, void *aux)
187 {
188 return (1);
189 }
190
191 void
192 wsmouse_attach(struct device *parent, struct device *self, void *aux)
193 {
194 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
195 struct wsmousedev_attach_args *ap = aux;
196 #if NWSMUX > 0
197 int mux, error;
198 #endif
199
200 sc->sc_accessops = ap->accessops;
201 sc->sc_accesscookie = ap->accesscookie;
202
203 #if NWSMUX > 0
204 sc->sc_base.me_ops = &wsmouse_srcops;
205 mux = sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux;
206 if (mux >= 0) {
207 error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
208 if (error)
209 printf(" attach error=%d", error);
210 else
211 printf(" mux %d", mux);
212 }
213 #else
214 if (sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux >= 0)
215 printf(" (mux ignored)");
216 #endif
217
218 printf("\n");
219 }
220
221 int
222 wsmouse_activate(struct device *self, enum devact act)
223 {
224 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
225
226 if (act == DVACT_DEACTIVATE)
227 sc->sc_dying = 1;
228 return (0);
229 }
230
231 /*
232 * Detach a mouse. To keep track of users of the softc we keep
233 * a reference count that's incremented while inside, e.g., read.
234 * If the mouse is active and the reference count is > 0 (0 is the
235 * normal state) we post an event and then wait for the process
236 * that had the reference to wake us up again. Then we blow away the
237 * vnode and return (which will deallocate the softc).
238 */
239 int
240 wsmouse_detach(struct device *self, int flags)
241 {
242 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
243 struct wseventvar *evar;
244 int maj, mn;
245 int s;
246
247 #if NWSMUX > 0
248 /* Tell parent mux we're leaving. */
249 if (sc->sc_base.me_parent != NULL) {
250 DPRINTF(("wsmouse_detach:\n"));
251 wsmux_detach_sc(&sc->sc_base);
252 }
253 #endif
254
255 /* If we're open ... */
256 evar = sc->sc_base.me_evp;
257 if (evar != NULL && evar->io != NULL) {
258 s = spltty();
259 if (--sc->sc_refcnt >= 0) {
260 /* Wake everyone by generating a dummy event. */
261 if (++evar->put >= WSEVENT_QSIZE)
262 evar->put = 0;
263 WSEVENT_WAKEUP(evar);
264 /* Wait for processes to go away. */
265 if (tsleep(sc, PZERO, "wsmdet", hz * 60))
266 printf("wsmouse_detach: %s didn't detach\n",
267 sc->sc_base.me_dv.dv_xname);
268 }
269 splx(s);
270 }
271
272 /* locate the major number */
273 for (maj = 0; maj < nchrdev; maj++)
274 if (cdevsw[maj].d_open == wsmouseopen)
275 break;
276
277 /* Nuke the vnodes for any open instances (calls close). */
278 mn = self->dv_unit;
279 vdevgone(maj, mn, mn, VCHR);
280
281 return (0);
282 }
283
284 void
285 wsmouse_input(struct device *wsmousedev, u_int btns /* 0 is up */,
286 int x, int y, int z, u_int flags)
287 {
288 struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
289 struct wscons_event *ev;
290 struct wseventvar *evar;
291 int mb, ub, d, get, put, any;
292
293 /*
294 * Discard input if not open.
295 */
296 evar = sc->sc_base.me_evp;
297 if (evar == NULL)
298 return;
299
300 #if NWSMUX > 0
301 DPRINTFN(5,("wsmouse_input: %s mux=%p, evar=%p\n",
302 sc->sc_base.me_dv.dv_xname, sc->sc_base.me_parent, evar));
303 #endif
304
305 sc->sc_mb = btns;
306 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_X))
307 sc->sc_dx += x;
308 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Y))
309 sc->sc_dy += y;
310 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Z))
311 sc->sc_dz += z;
312
313 /*
314 * We have at least one event (mouse button, delta-X, or
315 * delta-Y; possibly all three, and possibly three separate
316 * button events). Deliver these events until we are out
317 * of changes or out of room. As events get delivered,
318 * mark them `unchanged'.
319 */
320 ub = sc->sc_ub;
321 any = 0;
322 get = evar->get;
323 put = evar->put;
324 ev = &evar->q[put];
325
326 /* NEXT prepares to put the next event, backing off if necessary */
327 #define NEXT \
328 if ((++put) % WSEVENT_QSIZE == get) { \
329 put--; \
330 goto out; \
331 }
332 /* ADVANCE completes the `put' of the event */
333 #define ADVANCE \
334 ev++; \
335 if (put >= WSEVENT_QSIZE) { \
336 put = 0; \
337 ev = &evar->q[0]; \
338 } \
339 any = 1
340 /* TIMESTAMP sets `time' field of the event to the current time */
341 #define TIMESTAMP \
342 do { \
343 int s; \
344 s = splhigh(); \
345 TIMEVAL_TO_TIMESPEC(&time, &ev->time); \
346 splx(s); \
347 } while (0)
348
349 if (flags & WSMOUSE_INPUT_ABSOLUTE_X) {
350 if (sc->sc_x != x) {
351 NEXT;
352 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_X;
353 ev->value = x;
354 TIMESTAMP;
355 ADVANCE;
356 sc->sc_x = x;
357 }
358 } else {
359 if (sc->sc_dx) {
360 NEXT;
361 ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
362 ev->value = sc->sc_dx;
363 TIMESTAMP;
364 ADVANCE;
365 sc->sc_dx = 0;
366 }
367 }
368 if (flags & WSMOUSE_INPUT_ABSOLUTE_Y) {
369 if (sc->sc_y != y) {
370 NEXT;
371 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Y;
372 ev->value = y;
373 TIMESTAMP;
374 ADVANCE;
375 sc->sc_y = y;
376 }
377 } else {
378 if (sc->sc_dy) {
379 NEXT;
380 ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
381 ev->value = sc->sc_dy;
382 TIMESTAMP;
383 ADVANCE;
384 sc->sc_dy = 0;
385 }
386 }
387 if (flags & WSMOUSE_INPUT_ABSOLUTE_Z) {
388 if (sc->sc_z != z) {
389 NEXT;
390 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Z;
391 ev->value = z;
392 TIMESTAMP;
393 ADVANCE;
394 sc->sc_z = z;
395 }
396 } else {
397 if (sc->sc_dz) {
398 NEXT;
399 ev->type = WSCONS_EVENT_MOUSE_DELTA_Z;
400 ev->value = sc->sc_dz;
401 TIMESTAMP;
402 ADVANCE;
403 sc->sc_dz = 0;
404 }
405 }
406
407 mb = sc->sc_mb;
408 while ((d = mb ^ ub) != 0) {
409 /*
410 * Mouse button change. Find the first change and drop
411 * it into the event queue.
412 */
413 NEXT;
414 ev->value = ffs(d) - 1;
415
416 KASSERT(ev->value >= 0);
417
418 d = 1 << ev->value;
419 ev->type =
420 (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
421 TIMESTAMP;
422 ADVANCE;
423 ub ^= d;
424 }
425 out:
426 if (any) {
427 sc->sc_ub = ub;
428 evar->put = put;
429 WSEVENT_WAKEUP(evar);
430 #if NWSMUX > 0
431 DPRINTFN(5,("wsmouse_input: %s wakeup evar=%p\n",
432 sc->sc_base.me_dv.dv_xname, evar));
433 #endif
434 }
435 }
436
437 int
438 wsmouseopen(dev_t dev, int flags, int mode, struct proc *p)
439 {
440 struct wsmouse_softc *sc;
441 struct wseventvar *evar;
442 int error, unit;
443
444 unit = minor(dev);
445 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
446 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
447 return (ENXIO);
448
449 #if NWSMUX > 0
450 DPRINTF(("wsmouseopen: %s mux=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
451 sc->sc_base.me_parent, p));
452 #endif
453
454 if (sc->sc_dying)
455 return (EIO);
456
457 if ((flags & (FREAD | FWRITE)) == FWRITE)
458 return (0); /* always allow open for write
459 so ioctl() is possible. */
460
461 #if NWSMUX > 0
462 if (sc->sc_base.me_parent != NULL) {
463 /* Grab the mouse out of the greedy hands of the mux. */
464 DPRINTF(("wsmouseopen: detach\n"));
465 wsmux_detach_sc(&sc->sc_base);
466 }
467 #endif
468
469 if (sc->sc_base.me_evp != NULL)
470 return (EBUSY);
471
472 evar = &sc->sc_base.me_evar;
473 wsevent_init(evar);
474 sc->sc_base.me_evp = evar;
475 evar->io = p;
476
477 error = wsmousedoopen(sc, evar);
478 if (error) {
479 DPRINTF(("wsmouseopen: %s open failed\n",
480 sc->sc_base.me_dv.dv_xname));
481 sc->sc_base.me_evp = NULL;
482 wsevent_fini(evar);
483 }
484 return (error);
485 }
486
487 int
488 wsmouseclose(dev_t dev, int flags, int mode, struct proc *p)
489 {
490 struct wsmouse_softc *sc =
491 (struct wsmouse_softc *)wsmouse_cd.cd_devs[minor(dev)];
492 struct wseventvar *evar = sc->sc_base.me_evp;
493
494 if (evar == NULL)
495 /* not open for read */
496 return (0);
497 sc->sc_base.me_evp = NULL;
498 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
499 wsevent_fini(evar);
500
501 return (0);
502 }
503
504 int
505 wsmousedoopen(struct wsmouse_softc *sc, struct wseventvar *evp)
506 {
507 sc->sc_base.me_evp = evp;
508 sc->sc_x = INVALID_X;
509 sc->sc_y = INVALID_Y;
510 sc->sc_z = INVALID_Z;
511
512 /* enable the device, and punt if that's not possible */
513 return (*sc->sc_accessops->enable)(sc->sc_accesscookie);
514 }
515
516 int
517 wsmouseread(dev_t dev, struct uio *uio, int flags)
518 {
519 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
520 int error;
521
522 if (sc->sc_dying)
523 return (EIO);
524
525 #ifdef DIAGNOSTIC
526 if (sc->sc_base.me_evp == NULL) {
527 printf("wsmouseread: evp == NULL\n");
528 return (EINVAL);
529 }
530 #endif
531
532 sc->sc_refcnt++;
533 error = wsevent_read(sc->sc_base.me_evp, uio, flags);
534 if (--sc->sc_refcnt < 0) {
535 wakeup(sc);
536 error = EIO;
537 }
538 return (error);
539 }
540
541 int
542 wsmouseioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
543 {
544 return (wsmousedoioctl(wsmouse_cd.cd_devs[minor(dev)],
545 cmd, data, flag, p));
546 }
547
548 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
549 int
550 wsmousedoioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
551 struct proc *p)
552 {
553 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
554 int error;
555
556 sc->sc_refcnt++;
557 error = wsmouse_do_ioctl(sc, cmd, data, flag, p);
558 if (--sc->sc_refcnt < 0)
559 wakeup(sc);
560 return (error);
561 }
562
563 int
564 wsmouse_do_ioctl(struct wsmouse_softc *sc, u_long cmd, caddr_t data,
565 int flag, struct proc *p)
566 {
567 int error;
568
569 if (sc->sc_dying)
570 return (EIO);
571
572 /*
573 * Try the generic ioctls that the wsmouse interface supports.
574 */
575 switch (cmd) {
576 case FIONBIO: /* we will remove this someday (soon???) */
577 return (0);
578
579 case FIOASYNC:
580 if (sc->sc_base.me_evp == NULL)
581 return (EINVAL);
582 sc->sc_base.me_evp->async = *(int *)data != 0;
583 return (0);
584
585 case TIOCSPGRP:
586 if (sc->sc_base.me_evp == NULL)
587 return (EINVAL);
588 if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
589 return (EPERM);
590 return (0);
591 }
592
593 /*
594 * Try the mouse driver for WSMOUSEIO ioctls. It returns -1
595 * if it didn't recognize the request.
596 */
597 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
598 data, flag, p);
599 return (error != -1 ? error : ENOTTY);
600 }
601
602 int
603 wsmousepoll(dev_t dev, int events, struct proc *p)
604 {
605 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
606
607 if (sc->sc_base.me_evp == NULL)
608 return (EINVAL);
609 return (wsevent_poll(sc->sc_base.me_evp, events, p));
610 }
611
612 #if NWSMUX > 0
613 int
614 wsmouse_mux_open(struct wsevsrc *me, struct wseventvar *evp)
615 {
616 struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
617
618 if (sc->sc_base.me_evp != NULL)
619 return (EBUSY);
620
621 return wsmousedoopen(sc, evp);
622 }
623
624 int
625 wsmouse_mux_close(struct wsevsrc *me)
626 {
627 struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
628
629 sc->sc_base.me_evp = NULL;
630 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
631
632 return (0);
633 }
634
635 int
636 wsmouse_add_mux(int unit, struct wsmux_softc *muxsc)
637 {
638 struct wsmouse_softc *sc;
639
640 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
641 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
642 return (ENXIO);
643
644 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
645 return (EBUSY);
646
647 return (wsmux_attach_sc(muxsc, &sc->sc_base));
648 }
649 #endif /* NWSMUX > 0 */
650