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